Twilio curl calls the phone

Based on the Twilio and Curl documentation, I have a curl php 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);
}

      

This 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 "+" for the "From" number. But still the error remains the same.

Thanks in advance!

+4


source to share


2 answers


Twilio Developer Evangelist is here.

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

This truncated your POST data , resulting in the "From" information not being sent.

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



Once you make both of these changes, your code should look like this and work without errors:

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 fixes your problems.

+3


source


curl -X POST http://textita.com/text \
       --data-urlencode phone='9087938341' \
       --data-urlencode message='Hello world' \
       -d key=textita

      



0


source







All Articles