phptwilio

Twilio PHP SDK - Did twilio receive my request?


We are using the Twilio PHP SDK and the Notify Client. When I make a request how do I get the status of that request. Did it return a good 2XX, an error 4XX? And to be clear, I don't mean, what is the status of the messages. I simply mean, did twilio get my API call?

When testing in Postman with the REST API I typically get a 200 or 201 response if everything went well.

$twilio = new Client($acct_sid, $token);

$Addresses = array("+12015551234");
$toBindingAttributes = array();

foreach ($Addresses as $Address) {
    array_push($toBindingAttributes, '{"binding_type":"sms","address":"' . $Address . '"}');
}

$notification = $twilio->notify->services($notify_sid)
                                   ->notifications->create([
      "toBinding" => $toBindingAttributes,
      "body" => "Twilio Test."
     ]);

I've tried to return $notification and I just get [Twilio.Notify.V1.NotificationInstance]

-----Edit-----

ok I realize now that [Twilio.Notify.V1.NotificationInstance] is an object. I was able to print_r($notification) and see that there is a statusCode property.

I tried to echo that property print_r(@notification->statusCode) but I get "Unknown Property".

Is it because it's "protected"? [statusCode:protected] => 201

Thanks


Solution

  • Since the result is the [Twilio.Notify.V1.NotificationInstance] object. The problem is all the properties within the object are protected we cannot access them directly.

    We were able to get to them with a bunch of strpos, substr, and regex but found a much easier way using a getter.

    By doing this way print($twilio->getHttpClient()->lastResponse->getStatusCode());

    More info in the Twilio-PHP library

    https://github.com/twilio/twilio-php/blob/650f42647c9b3039e03ae075785164ec97203e94/src/Twilio/Http/Response.php