gpt4 book ai didi

php - Twilio cURL 打电话

转载 作者:可可西里 更新时间:2023-10-31 23:03:57 26 4
gpt4 key购买 nike

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

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

我编辑并为“发件人”号码添加了“+”。但错误仍然存​​在。

提前致谢!

最佳答案

此处为 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);
}

如果这能解决您的问题,请告诉我。

关于php - Twilio cURL 打电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com