编程语言
首页 > 编程语言> > php – Twilio rest api,发送短信

php – Twilio rest api,发送短信

作者:互联网

我正在尝试(并且必须)使用twilio rest api to send sms而不使用帮助程序php库.
下面是我的代码:我面临的问题是,它给我的错误是91xxxxxxxxxx不是有效的电话号码.但是我正在通过电话号码以”为前缀的api.

我正在使用试用帐户,并使用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"}"

它确实说错了号码(缺少”),但我没有到达哪里.

解决方法:

好吧缺少了什么,所以终于解决了.

这是代码

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

标签:php,twilio
来源: https://codeday.me/bug/20190901/1786028.html