【问题标题】:Twilio cURL calling a phoneTwilio cURL 拨打电话
【发布时间】:2015-06-13 20:29:03
【问题描述】:

根据 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}

我尝试了所有选项,有人可以帮忙吗?

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

提前致谢!

【问题讨论】:

  • 好像没有国家代码+NN0NN from number
  • 我编辑并为“From”编号添加了“+”,错误仍然保持不变,
  • 您添加了国家代码吗?
  • 是的,我在我的 Twilio 电话号码中添加了“+1”

标签: php curl twilio phone-call


【解决方案1】:

这里是 Twilio 开发人员宣传员。

您的代码非常接近。只需两个小改动即可解决您的问题。首先,您需要删除此行: curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));

这会截断您的 POST data,导致“发件人”信息未发送。

其次,您不需要对 $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. twilioNumber - 我从 Twilio 购买的号码,2. 目的地号码和 3. myNumber - 我可以与之通话的电话......或者我想 4 通过我的电脑通话。对不起,这一切让我有点头疼。我更擅长打孔卡片和纸带! TIA 史蒂夫
猜你喜欢
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多