admin管理员组文章数量:1129646
I'm kinda new to this part of programming, and I find it really confusing.
I have a script that makes some contact with a third party api. It basically adds a customer to their data.
What I heard is that they do something on the background, that checks if the newly added customer paid.
When their system notifies that, they're basically making an api call to a custom route of mine, and send a json object to that route.
I googled a bit, and came to this:
add_action('rest_api_init', function(){
register_rest_route('sportivity-callback', '/endpoint', [
'method' => 'GET',
'callback' => 'rest_api_init',
]);
});
function rest_api_init($request){
$json = $request->get_json_params();
return $json;
}
Am I explaining it right? Here I use the rest api init hook. I register a custom route, for the third party. I add a method to that route. that returns the json, that they've sent.
I'm kinda new to this part of programming, and I find it really confusing.
I have a script that makes some contact with a third party api. It basically adds a customer to their data.
What I heard is that they do something on the background, that checks if the newly added customer paid.
When their system notifies that, they're basically making an api call to a custom route of mine, and send a json object to that route.
I googled a bit, and came to this:
add_action('rest_api_init', function(){
register_rest_route('sportivity-callback', '/endpoint', [
'method' => 'GET',
'callback' => 'rest_api_init',
]);
});
function rest_api_init($request){
$json = $request->get_json_params();
return $json;
}
Am I explaining it right? Here I use the rest api init hook. I register a custom route, for the third party. I add a method to that route. that returns the json, that they've sent.
Share Improve this question asked Aug 5, 2022 at 9:57 JanJan 336 bronze badges 3- 1 Yes, this is a fairly common pattern e.g. payment providers, some of the oAuth2 flows. I'm a bit lost though: what are you asking / what do you need help with? What to do with the JSON they send you? I'd guess just update the customer / order / whatever you sent to them in your database. – Rup Commented Aug 5, 2022 at 10:10
- 1 I'd be surprised if their callback was a GET though. I'd expect it to be a POST and them to post the JSON content. – Rup Commented Aug 5, 2022 at 10:12
- @Rup I basically need to inserrt a membership whenever they retrieve a customer ID. But I'm not sure how to test this. I went to the route and it just gave me a 404. Also I changed it to a POST. – Jan Commented Aug 5, 2022 at 10:13
2 Answers
Reset to default 2I think I managed to fix it
add_action('rest_api_init', function(){
register_rest_route('endpoint', '/sportivity', [
'method' => 'POST',
'callback' => 'sportivity_endpoint',
]);
});
function sportivity_endpoint($request){
$json = $request->get_json_params();
$json['test'] = 'Jan';
return $json;
}
When I go to url/wp-json/endpoint/sportivity it returns the json now.
OKay used this code to work on my first webhook and sat for hours and could not get my custom code to work the way I wanted. And could NOT get POST to work in postman... and OMG Till I spotted one thing on another site
'method' => 'POST',
IS WRONG, It's Plural... Grrrr Plz be aware of this it's
'methods' => 'POST',
本文标签: phpUnderstanding webhooks
版权声明:本文标题:php - Understanding webhooks 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736717437a1949277.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论