Dan Myer Dan Myer - 1 month ago 12
PHP Question

What is correct Twilio AddOns syntax for php REST client?

Beginner to both Twilio & php here:

I have: Twilio php helper, Twilio account, Whitepages Pro AddOn enabled for Lookups and have successfully retrieved "basic" lookup data, ie, "Carrier->Type" (the "basic" lookup does not use the AddOn)

I need: to use Twilio Rest Client with Whitepages Pro AddOn to retrieve other data, ie, "standard_address_line1", for an individual phone number. I do not want the $0.07 per call AddOn enabled for all incoming calls, although I was able to receive this data from the AddOn that way.

Twilio API Documentation is scant. Shows output format, but not REST Client request syntax: WhitePagesPro AddOn Documentation

Here is what I tried:

<?php

require 'vendor/autoload.php';
use Twilio\Rest\Client;

$client = new Client(ACxxxxxxxxxx,Tokenxxxxxxxx);

$number = $client->lookups
->phoneNumbers("+1xxxxxxxxxx")
->fetch(
array("AddOns" => "whitepages_pro_caller_id")
);

echo $number->
results->
whitepages_pro_caller_id->
result->
results[0]->
associated_locations[0]->
standard_address_line1;

//This syntax works for 'basic' lookup
//Returns: "landline"
//
//$number = $client->lookups
// ->phoneNumbers("+1xxxxxxxxxx")
// ->fetch(
// array("type" => "carrier")
// );
//
//echo $number->carrier['type'];

?>


Throws error: Uncaught exception 'Twilio\Exceptions\TwilioException' with message 'Unknown property: results'

I'm way over my head, I don't know how to go about debugging this. Any Twilio experts?

Ideally I'd also like to know if it's possible to specify this particular data in the request vs traversing many levels of the response in order to get the data I need...

Answer

Twilio developer evangelist here.

You're very close with what you have there. The addOns results are actually returned in the addOns property of the number there. So, using your code, you can print the request SID of the call like this:

<?php

require 'vendor/autoload.php';
use Twilio\Rest\Client;

$client = new Client(ACxxxxxxxxxx,Tokenxxxxxxxx);

$number = $client->lookups
    ->phoneNumbers("+1xxxxxxxxxx")
    ->fetch(
        array("AddOns" => "whitepages_pro_caller_id")
    );

echo $number->addOns['results']['whitepages_pro_caller_id']['request_sid']

If you want to inspect the entire result, you can use var_dump to see the entire structure

var_dump($number->addOns['results']['whitepages_pro_caller_id'])

The structure will appear as it does in the documentation but it might be easier to see in the PHP output.

Let me know if that helps at all.