admin管理员组文章数量:1327323
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 / 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: 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.
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:
Can anyone help me with this? or just drive me in the right direction? Thank you!
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/ 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/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.
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:
Can anyone help me with this? or just drive me in the right direction? Thank you!
Share Improve this question edited Aug 9, 2020 at 11:22 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Aug 9, 2020 at 9:50 myhappycodingmyhappycoding 1135 bronze badges 9 | Show 4 more comments1 Answer
Reset to default 2Am 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/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. :)
本文标签: plugin developmentDo I need to create an endpoint
版权声明:本文标题:plugin development - Do I need to create an endpoint? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742200910a2431941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
example/wp-json/real-estate-lite/v1/endpoint
? – Sally CJ Commented Aug 9, 2020 at 10:36