jakethecool1
jakethecool1

Reputation: 85

Twilio cURL request returns just a string of information

I am trying to get a list of all my Twilio numbers using cURL inside of a php function. I am getting the information I need back from Twilio however it is just a string of all the information with no whitespace or anything.

This is what I am getting back. I have removed my account number and sid:

string(1621) " {{ SID }}ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX(936) 585-6544+19365856544https://demo.twilio.com/welcome/sms/reply/POSTPOSTfalseWed, 11 May 2016 14:39:22 +0000Wed, 18 May 2016 15:41:25 +0000https://demo.twilio.com/welcome/sms/reply/POSTPOSTnonefalsetruetruetruePOST2010-04-01/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/IncomingPhoneNumbers/PN5e0ef24464a1fbbcf4e2bd127c471892"

Here is the function I am using to cURL Twilio.

function post_incoming($POST) {
    // resource url & authentication
    $uri = 'https://api.twilio.com/2010-04-01/Accounts/' . $this->sid . '/IncomingPhoneNumbers';
    $auth = $this->sid . ':' . $this->authtoken;

    $res = curl_init();

    // set cURL options
    curl_setopt( $res, CURLOPT_URL, $uri );
    curl_setopt( $res, CURLOPT_USERPWD, $auth ); // authenticate
    curl_setopt( $res, CURLOPT_RETURNTRANSFER, true ); // don't echo

    // send cURL
    $result = curl_exec( $res );

    var_dump($result);
    return $result;
}

I need to get the information back as a json array or some kind of array that way I can parse out the Twilio phone number for later.

Upvotes: 2

Views: 816

Answers (1)

ecorvo
ecorvo

Reputation: 3629

It is a lot easier if you use the PHP helper library. It woudl look something like this:

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$numbers_arr = array();
// Loop over the list of numbers and echo a property for each one
foreach ($client->account->incoming_phone_numbers as $number) {
    $numbers_arr[] = $number->phone_number;
}

return json_encode($numbers_arr);

}

This function uses the Twilio PHP SDK to get a list of all numbers in the account then puts them into an array variable $numbers_arr and returns that variable as JSON.

Upvotes: 1

Related Questions