我正在尝试(并且必须)使用twilio rest api to send sms而不使用帮助程序php库。 下面是我的代码:我面临的问题是,它给我的错误是91xxxxxxxxxx不是有效的电话号码。但我正在通过电话号码传递电话号码以“+”为前缀。
我正在使用试用帐户,并使用twilio验证该号码,并使用帮助程序库进行测试。
<?php
$url = "https://api.twilio.com/2010-04-01/Accounts/MYID/SMS/Messages.json";
$from = "MY_TWILIO_NUMBER without + prefix";
$to = "+91xxxxxxxxxx"; // twilio trial verified number
$body = "using twilio rest api";
$id = "MY_ID";
$token = "MY_TOKEN";
$data = array (
'From' => $from,
'To' => $to,
'Body' => $body,
);
$post = '';
foreach ($data as $parameter => $value)
{
$post .= "&$parameter=$value";
}
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
var_dump($y);
curl_close($x);
?>
卷曲请求的完整转储
string(157) "{"status":400,"message":"The 'To' number 91xxxxxxxxxx is not a valid phone number.","code":21211,"more_info":"http://www.twilio.com/docs/errors/21211"}"
它确实说错了号码(缺少'+'),但我没有到达那里。
答案 0 :(得分:5)
很遗憾,最终解决了。
这是代码
<?php
$url = "https://api.twilio.com/2010-04-01/Accounts/MYID/SMS/Messages.json";
$from = "MY_TWILIO_NUMBER without + prefix";
$to = "+91xxxxxxxxxx"; // twilio trial verified number
$body = "using twilio rest api";
$id = "MY_ID";
$token = "MY_TOKEN";
$data = array (
'From' => $from,
'To' => $to,
'Body' => $body,
);
$post = http_build_query($data);
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
var_dump($post);
$y = curl_exec($x);
var_dump($y);
curl_close($x);
?>