【发布时间】:2017-07-25 21:00:54
【问题描述】:
我正在尝试使用 twilio 使用 php 和 curl 通过文本发送图像。 发送文本很好......效果很好。 我也在尝试添加图像 - 似乎忽略了图像相关字段(没有错误 - 只是没有图像)。
下面是我正在使用的代码。
function send_sms( $sid, $token, $to, $from, $body ) {
$uri = 'https://api.twilio.com/2010-04-01/Accounts/' . $sid . '/SMS/Messages';
$auth = $sid . ':' . $token;
$postfields = Array(
'From'=>$from,
'To'=>$to,
'Body'=>$body,
'MediaUrl'=>'http://imageurl.jpg',
'MediaContentType'=>'image/jpeg'
);
// start cURL
$res = curl_init();
// set cURL options
curl_setopt( $res, CURLOPT_POST, true);
curl_setopt( $res, CURLOPT_URL, $uri );
curl_setopt( $res, CURLOPT_POSTFIELDS, $postfields );
curl_setopt( $res, CURLOPT_USERPWD, $auth );
curl_setopt( $res, CURLOPT_RETURNTRANSFER, true );
// send cURL
$result = curl_exec( $res );
return $result;
}
更新:问题已解决:问题是我使用了错误的 API URL! 我需要消除“/ SMS” - 它本身有效,但仅适用于 纯文本消息。一旦我只使用了 $sid.'/Messages' - 它工作得很好。
此外,/Messages 将返回 XML 状态。如果您使用 /Messsages.json 它 将返回一个 JSON 块。
【问题讨论】:
-
您的
http://imageurl.jpg可以通过网络访问吗? Twilio 需要能够检索它 -
是的,图片网址是可以访问的。最初,该图像是一个发送图像的 .php 程序 - 效果很好。但是,为了确保不是这样 - 我将其更改为实际的 .jpg 图像。它的大小也小于 1MB。
-
已解决:我需要使用以下 API URL: $uri = 'api.twilio.com/2010-04-01/Accounts' 。 $sid 。 '/消息'; (删除 /SMS)。