1
votes

Did anyone used TWILIO api in curl with response in JSON foRMAT?

How to get the response in json format for send sms response using twilio api CURL

Here is code which is working but the response is not coming in formatted text.

$AccountSid = "SANDBOX_ACC_ID"; 
$AuthToken = "SANDBOX_TOKEN";
$url = "https://api.twilio.com/2010-04-01/Accounts/$AccountSid/SMS/Messages";
$from = "+MAGICNUMBER";
$to = "+XXXXXXXXXX"; // twilio trial verified number
$body = "Sending test SMS using Twilio Api";
$data = array (
        'From' => $from,
        'To' => $to,
        'Body' => $body,
    );
$post = http_build_query($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$AccountSid:$AuthToken");
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($curl);
curl_close($curl);
//var_dump($post);
echo "<pre>";
echo 'testing with curl '.$response;
echo "</pre>";
1

1 Answers

1
votes

From the Twilio docs:

"Twilio also supports returning resource representations as JSON. Simply add the .json extension to any resource URI."

As an example, here is how you would get a list of Messages in json format:

GET /2010-04-01/Accounts/AC228b9.../Messages/SM1f0e8ae6ade43cb3c0ce4525424e404f.json