Twilio:Rest API - 发送消息 - 如何获取消息sid?

时间:2017-03-24 13:04:28

标签: twilio twilio-php twilio-api

我想使用Twilio REST API发送彩信。消息已成功发送但是,我无法获取已发送消息的唯一MessageSid

我已尝试使用$result->sid$result->messageSid

当前代码

$result= $client->account->messages->create(
    $to, array('from' => $from,
               'body' => $message,
               'mediaUrl' => $mediaurl
              ));               

if (isset($result->messageSid))
    return $result->messageSid;
else
    return "";

我有什么问题吗?

1 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

如果检查first code sample on the documentation page for sending messages with Twilio的输出,那么您将看到返回的JSON应具有sid属性。像这样:

{
  "sid": "MMc781610ec0b3400c9e0cab8e757da937",
  "date_created": "Mon, 19 Oct 2015 07:07:03 +0000",
  "date_updated": "Mon, 19 Oct 2015 07:07:03 +0000",
  "date_sent": null,
  "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "to": "+15558675309",
  "from": "+15017250604",
  "body": "This is the ship that made the Kessel Run in fourteen parsecs?",
  "status": "queued",
  "num_segments": "1",
  "num_media": "1",
  "direction": "outbound-api",
  "api_version": "2010-04-01",
  "price": null,
  "price_unit": "USD",
  "error_code": null,
  "error_message": null,
  "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MMc781610ec0b3400c9e0cab8e757da937.json",
  "subresource_uris": {
    "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MMc781610ec0b3400c9e0cab8e757da937/Media.json"
  }
}

我刚刚使用了类似的代码(我没有发送媒体网址),可以回复$result->sid

$result = $client->account->messages->create(
    'MY_NUMBER',
    array(
        'from' => 'MY_TWILIO_NUMBER',
        'body' => "Hey Jenny! Good luck on the bar exam!"
    )
);

echo $result->sid;

如果有帮助,请告诉我。