I want to use this service OnWebChange to monitor when a website changes and change mine accordingly. It would notify me via a URL callback to my website URL with a HTTP POST method. What tools/API/plugins do I use to “catch” those POST’s? Have searched a lot and found only how to make POST, but not “catch” them when POST comes from another website. If I had access to apache I could install a module to dump POST, however I’m using WordPress with no access to it.

1 Answer
1

That’s pretty simple, just use your website home url 🙂

After that, just fire an action when the page is loaded via POST HTTP method and hook with a callback.

add_action( 'wp_loaded', function() {
   if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
      // fire the custom action
      do_action('onchangeapi', new PostListener($_POST));
   }
} );

And now the listener class

class PostListener {

   private $valid = false;

   /**
    * @param array $postdata $_POST array
    */
   public function __construct(array $postdata) {
       $this->valid = $this->validatePostData($postdata);
   }

   /**
    * Runs on 'onchangeapi' action
    */
   public function __invoke() {
      if ($this->valid) {
          // do whatever you need to do 
          exit();
      }
   }

   /**
    * @param array $postdata $_POST array
    * @return bool
    */
   private function validatePostData(array $postdata) {
      // check here the $_POST data, e.g. if the post data actually comes
      // from the api, autentication and so on
   } 
}

Leave a Reply

Your email address will not be published. Required fields are marked *