Twilio cURL 呼叫电话

4

根据Twilio和Curl的文档,我有一个PHP Curl例程:

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);
}

它给我一个错误:

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

我尝试了所有的选项,有人能帮忙吗?

我编辑并在“发件人”号码前添加了一个“+”。但是错误仍然存在。

提前感谢!


1
似乎发送方号码中没有国家代码“+NN”或“0NN”。 - ScaisEdge
我编辑并在“From”数字上添加了“+”,错误仍然是相同的。 - Nilam Doctor
你添加了你的国家代码吗? - ScaisEdge
是的,我在 Twilio 的电话号码上添加了“+1”。 - Nilam Doctor
1个回答

4

我是Twilio开发者推广员。

你的代码非常接近正确,只需要做两个小改动就可以解决问题。首先,你需要删除这一行代码:curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));

这会截断你的POST数据,导致“From”信息无法发送。

其次,你不需要对$CallURL进行urlencode,因为curl已经为我们处理了。

当你完成这两个改动后,你的代码应该像这样,并且可以正常运行:

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);
}

如果这个解决了你的问题,请告诉我。


我可以问一个非常愚蠢的问题吗?这是否会导致两个电话号码之间的通话,还是你必须使用“我的”电话(就像我手上拿着一个真实的电话)来配置 Twilio。目前我只发送短信,但一直对通过 Twilio 进行真实电话呼叫感到非常困惑。似乎涉及 3 个号码;1. Twilio 号码 - 我从 Twilio 购买的号码,2. 目标号码和 3. 我的号码 - 一台我能够进行语音通话的手机……或者我想4可以通过我的电脑进行语音通话。抱歉这让我有些头疼。我更擅长使用穿孔卡和纸带!谢谢!Steve - BeNice

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接