使用twilio发送文本图片图像

时间:2017-07-25 21:00:54

标签: php curl sms twilio mms

我尝试使用php和curl使用twilio通过文本发送图像。 发送文本很好......效果很好。 我也尝试添加图像 - 似乎忽略了与图像相关的字段(没有错误 - 只是没有图像)。

以下是我正在使用的代码。

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块。

1 个答案:

答案 0 :(得分:-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