Search code examples
phptwiliotwilio-php

When using twilio's php notify API, how do you set your callback URL?


I have the following code and it sends SMS notifications to my phone:

$notification = $twilio->notify->services($serviceSid)
                                    ->notifications->create([
                                        'toBinding' => $batch,
                                        'body' => $txt,
                                        'statusCallback' => 'http://postb.in/b/jarblegarble' // <-- this doesn't work
                                    ]);

However, even though the sending works, I can't seem to figure out their callbacks.

I'm scouring through their docs and I can't find how to set the callback URL. I see some of their resources use "url" while others use "statusCallback" (heck, one seems to use "redirect"). That being said, I can't seem to post to postb.in using them -- there must be a way to check the status of my notification.


Solution

  • So it turns out I was wrong on two fronts.

    1) The callback URL needs to be passed to your messaging service this way:

    $notification = $twilio->notify->services($serviceSid)
        ->notifications->create([
            'toBinding' => $bindings,
            'body' => $txt,
            'sms' => ['status_callback' => 'http://your_callback_url' ]
        ]);
    

    2) postb.in wasn't working! I was testing the code above, after being assured by twilio support that it was valid, I decided to try and post to my own server and just capture the POSTed content. Sure enough, it was working as they suggested.

    Edit: It wasn't clear to me at the time but the callback URL will be called for each SMS sent out for each status update. So that means queued, sent, and delivered. I initially thought that I'd just get a status update for the batch itself as I don't necessarily care for the status of up to 10,000 txt messages.