Twilio cURL calling a phone

1.4k Views Asked by At

Based on the documentation of Twilio and Curl I have a php curl routine:

function twilio($mobile,$msg,$twoCode){
$url = 'https://api.twilio.com/2010-04-01/Accounts/'.TWILIO_ACCOUNT_SID.'/Calls.json';
$CallURL = 'http://Myweb.com/code/say/'.$twoCode;
$auth = TWILIO_ACCOUNT_SID.":".TWILIO_AUTH_TOKEN;
$fields = array(
 'To' =>  $mobile  ,
 'From' => '+16262471234'  , // My Number
 'Url' => urlencode( $CallURL ) ,
 'Method'=>'GET' ,
 'FallbackMethod'=>'GET',
 'StatusCallbackMethod'=>'GET',
 'Record'=>'false'
);
$post = http_build_query($fields);
$curl = curl_init($url);
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT , 'Mozilla 5.0');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_VERBOSE , true);
$resp = curl_exec($curl);
curl_close($curl);
}

It gives me an error:

{"code": 21213, "message": "No 'From' number is specified", "more_info": "https://www.twilio.com/docs/errors/21213", "status": 400}

I tried all the options, can anyone help?

I edited and added a "+" for the "From" number. But still the error remains the same.

Thanks in advance!

1

There are 1 best solutions below

1
On

Twilio Developer Evangelist here.

Your code is really close. Just two small changes should resolve your issue. First you need to remove this line: curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));

This was truncating your POST data which resulted in the "From" information not being sent.

Second, you don't need to urlencode the $CallURL because curl handles this for us.

Once you make both of these changes your code should look like this and run without an error:

function twilio($mobile,$msg,$twoCode){
  $url = 'https://api.twilio.com/2010-04-01/Accounts/'. TWILIO_ACCOUNT_SID.'/Calls.json';
  $CallURL = 'http://Myweb.com/code/say/'.$twoCode;
  $auth = TWILIO_ACCOUNT_SID .":". TWILIO_AUTH_TOKEN;
  $fields = array(
   'To' =>  $mobile  , 
   'From' => '+16262471234'  , // My Number
   'Url' => $CallURL,
   'Method'=>'GET' ,
   'FallbackMethod'=>'GET',
   'StatusCallbackMethod'=>'GET',
   'Record'=>'false'
  );
  $post = http_build_query($fields);
  $curl = curl_init($url);
  // Set some options - we are passing in a useragent too here
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_USERAGENT , 'Mozilla 5.0');
  curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  curl_setopt($curl, CURLOPT_USERPWD, $auth);
  curl_setopt($curl, CURLOPT_VERBOSE , true);

  $resp = curl_exec($curl);

  curl_close($curl);
}

Let me know if this gets your issues resolved.