0

Turns out I'm trying to integrate witei's CRM into a website. According to the witei documentation, I must indicate a url (webhook), where I want to receive the information of the properties that has changed, so that I can keep the properties of my website updated.

The information is sent through the POST method and the service works because it returns the data in JSON format, testing through the https://beeceptor.com/ service (which generates a temporary endpoint).

The fact is that according to what I have read (or I understood), I have to create an endpoint in my Wordpress, following the steps indicated in the wordpress documentation: https://developer.wordpress.org/rest- api/extending-the-rest-api/adding-custom-endpoints/

My question is: Am I in the right direction? If so ... With my endpoint created, do I have to create any extra file? or just including the function 'register_rest_route', dont need to make anything else? I have tried following instructions but I always get error 404.

enter image description here

The code involved for that is:

//Testing webhook
add_action( 'rest_api_init', function () {
  register_rest_route( 'real-estate-lite/v1', '/endpoint', array(
    'methods'  => 'POST',
    'callback' => 'my_awesome_func',
  ) );
} );

function my_awesome_func( $request ) {
     $data = $request->get_json_params();
     error_log( print_r( $data, true ) );
}

And in Witei CRM I get this result:

enter image description here

Can anyone help me with this? or just drive me in the right direction? Thank you!

9
  • Try returning valid value. See developer.wordpress.org/rest-api/extending-the-rest-api/… Aug 9, 2020 at 9:56
  • Sorry, I also tried that, in fact, is the first thing i did but wasn't successful. I have just modified the code. Aug 9, 2020 at 9:59
  • Did you use the correct endpoint URL - example.com/wp-json/real-estate-lite/v1/endpoint ?
    – Sally CJ
    Aug 9, 2020 at 10:36
  • yes I do, and I have tried with several, but wasn't lucky Aug 9, 2020 at 10:37
  • 1
    Plus I don't think it makes any sense if I manually visit the url, because the endpoint is only executed when I modify anything in the CRM. So..., maybe that is why everytime I visit the url I get a 404 error, but every time I make any change in the CRM, I get a response Aug 9, 2020 at 18:01

1 Answer 1

2

Am I in the right direction?

Yes, and I'd also use the REST API if I were you.

I have tried following instructions but I always get error 404.

That is actually normal if the request method is not POST — e.g. using the browser to manually access/visit the endpoint at example.com/wp-json/real-estate-lite/v1/endpoint whereby the (HTTP) request method is (or defaults to) GET.

And why it's normal is because the methods of your endpoint is set to POST only (i.e. 'methods' => 'POST'), hence all other request methods like GET are not allowed and therefore WordPress throws the error 404 when the endpoint is accessed through GET or another disallowed (HTTP) method.

So if for example you want GET method to be allowed, then set the methods to an array like so:

register_rest_route( 'real-estate-lite/v1', '/endpoint', array(
    'methods'  => [ 'POST', 'GET' ], // allowed request methods
    'callback' => 'my_awesome_func',
) );

/* The above code is equivalent to:
register_rest_route( 'real-estate-lite/v1', '/endpoint', array(
    // Endpoint 1, for POST request method.
    array(
        'methods'  => 'POST',
        'callback' => 'my_awesome_func',
    ),

    // Endpoint 2, for GET request method.
    array(
        'methods'  => 'GET',
        'callback' => 'my_awesome_func', // * the same callback
    ),
) );
*/

But of course, you wouldn't want to do that in your case since the webhook is using the POST method to connect to your custom WordPress REST API endpoint. For testing purposes though, you can temporarily allow GET requests (and then just go to the endpoint using your browser).

And as you can see in the example, we can have two or more endpoints at a route, and each request method should have its own callback.

BTW, sorry for not really understanding your issue/question earlier. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.