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
Add a comment  | 

2 Answers 2

Reset to default 2

I 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