【问题标题】:using twilio to send text picture image使用twilio发送文字图片图片
【发布时间】: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)。

标签: php curl sms twilio mms


【解决方案1】:

根据他们的 API 文档:

The URL of the media you wish to send out with the message. gif , png and jpeg content is currently supported and will be formatted correctly on the recipient's device. Other types are also accepted by the API. The media size limit is 5MB. If you wish to send more than one image in the message body, please provide multiple MediaUrls values in the POST request. You may include up to 10 MediaUrls per message.

您指定的是 .jpg 而不是 .jpEg。可能是问题所在。

https://www.twilio.com/docs/api/rest/sending-messages

【讨论】:

  • 还要检查您对图像的权限,并确保您可以在浏览器中查看它。
  • 不,文件结尾和文件类型并不总是完全匹配。请不要发布实际上不是答案而是问题的答案。
猜你喜欢
  • 2020-07-01
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 2016-11-29
  • 2016-09-14
  • 1970-01-01
相关资源
最近更新 更多