fedrick
fedrick

Reputation: 341

Twilio not working with curl php

im working on TWILIO SMS, I'm using trial account everything is working with pure php library, but i had a problem when using CURL & PHP

require "Services/Twilio.php";
$AccountSid = "SANDBOX_ACC_ID"; 
$AuthToken = "SANDBOX_TOKEN";
$client = new Services_Twilio($AccountSid, $AuthToken);
$message = $client->account->messages->create(array(
"From" => "+MAGICNUMBER",
"To" => "+XXXXXXXXXX",// twilio trial verified number
"Body" => "Test message 2 from Fedrick!",
));
// Display a confirmation message on the screen
echo "testing with php message {$message->sid}";

pure php output:

testing with php message SMxxxxxxxxxxxxxxxxxxxx

Code i have tried using CURL & PHP (which is not working)

$url = "https://api.twilio.com/2010-04-01/Accounts/SANDBOX_ACC_ID/SMS/Messages.json";
$from = "+MAGICNUMBER";
$to = "+XXXXXXXXXX"; // twilio trial verified number
$body = "using twilio rest api from Fedrick";
$id = "SANDBOX_ACC_ID";
$token = "SANDBOX_TOKEN";
$data = array (
        'From' => $from,
        'To' => $to,
        'Body' => $body,

    );
$post = http_build_query($data);
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
var_dump($post);
$y = curl_exec($x);
var_dump($y);
curl_close($x);

OUTPUT using Curl php :

bool(false) 

what wrong i have done in curl code... is there any solution without using pure php library, i want to use short and simple code just like above CURL code

Upvotes: 4

Views: 11947

Answers (4)

SIAMWEBSITE
SIAMWEBSITE

Reputation: 195

Curl Twilio PHP7+

$account_sid = 'account_sid';
$auth_token = 'auth_token';

$url = "https://api.twilio.com/2010-04-01/Accounts/$account_sid/SMS/Messages";
$to = "+xxxxxx";
$from = "+xxxxxx"; // twilio trial verified number
$body = "using twilio rest api from Fedrick";
$data = array (
    'From' => $from,
    'To' => $to,
    'Body' => $body,
);
$post = http_build_query($data);
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_USERPWD, "$account_sid:$auth_token");
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
curl_close($x);

//var_dump($post);
//var_dump($y);

Upvotes: 2

Kae Cyphet
Kae Cyphet

Reputation: 185

Here is a PHP - Curl implementation in a callable function

function send_sms($number,$body)
{
    $ID = '1234567890abcdef1234567890abcdef12';
    $token = '1234567890abcdef1234567890abcdef';
    $service = 'AB1234567890abcdef1234567890abcdef';
    $url = 'https://api.twilio.com/2010-04-01/Accounts/' . $ID . '/Messages.json';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

    curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD,$ID . ':' . $token);

    curl_setopt($ch, CURLOPT_POST,true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        'To=' . rawurlencode('+' . $number) .
        '&MessagingServiceSid=' . $service .
        //'&From=' . rawurlencode('+18885550000') .
        '&Body=' . rawurlencode($body));

    $resp = curl_exec($ch);
    curl_close($ch);
    return json_decode($resp,true);
}

$ID and $token can be found under SMS / Dashboard / 'Show API Credentials' https://www.twilio.com/console/sms/dashboard

(Optional) $service can be found under SMS / Messaging Services / 'SID' https://www.twilio.com/console/sms/services

Comment out 'MessagingServiceSid=' and uncomment 'From=' to use direct sending from a single phone number

Finally, key information can be found buried in the kb here https://www.twilio.com/docs/sms/send-messages#send-a-message-with-an-image-url

Upvotes: 5

mati ullah
mati ullah

Reputation: 29

You have to use :

https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages.json

for json response

https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages.xml

for xml response

Upvotes: 0

hindmost
hindmost

Reputation: 7195

First of all, since API's URL is secured, you have to disable SSL peer verification by setting CURLOPT_SSL_VERIFYPEER option to false.

Furthermore, according to API docs, URL of request is account-dependent, i.e. it should be built based on your account sandbox ID:

https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages

So the code should look like this:

$id = "SANDBOX_ACC_ID";
$token = "SANDBOX_TOKEN";
$url = "https://api.twilio.com/2010-04-01/Accounts/$id/SMS/Messages";
$from = "+MAGICNUMBER";
$to = "+XXXXXXXXXX"; // twilio trial verified number
$body = "using twilio rest api from Fedrick";
$data = array (
    'From' => $from,
    'To' => $to,
    'Body' => $body,
);
$post = http_build_query($data);
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
curl_close($x);
var_dump($post);
var_dump($y);

Upvotes: 13

Related Questions