简体   繁体   English

Twilio PHP API如何获取消息时间戳记DateSent?

[英]Twilio PHP API how to get message timestamp DateSent?

I have this code block iterating through the Twilio Message list.. but I keep getting null for DateSent (or DateCreated ), I'm looking to get bakc the timestamp of the message. 我有遍历Twilio消息列表的此代码块。.但我一直为DateSent (或DateCreated )获取空值 ,我希望获取bakc消息的时间戳。 Everything else (the other fields , from, to , body all work fine) 其他所有内容(其他字段,从到,身体都很好)

$client = new Services_Twilio($twilio['sid'],$twilio['token']);     

// Loop over the list of messages echo each key property
foreach ($client->account->messages as $message) {
  $list_sms_messages[]=array('timestamp'=>$message->dateSent,
'from'=>$message->from ,
'to'=>$message->to, 
'body'=> $message->body );
}

According to the API DateSent or (DateCreated) should be in the message list object. 根据API,DateSent或(DateCreated)应该在消息列表对象中。 Any ideas 有任何想法吗

I figured it out by poking around TWILIO site examples, it turns out that their API guide for their TWIL formatted JSON ( and XML) uses different property names than their TWILIO-PHP wrapper library.. Here's the typical Message JSON when using the PHP library ( i omitted some fields for privacy reasons), but as you can see: 我通过浏览TWILIO网站示例来弄清楚了,事实证明,其TWIL格式JSON(和XML)的API指南使用的属性名称与TWILIO-PHP包装器库的属性名称不同。这是使用PHP库时的典型Message JSON。 (出于隐私原因,我省略了一些字段),但是如您所见:

  • DateSent is actually date_sent , DateSent实际上是date_sent
  • DateCreated is actually date_created and so on.. DateCreated实际上是date_created ,依此类推。

Once I plugged that into my code it worked as expected... 一旦将其插入我的代码中,它就会按预期工作...

  "messages": [{
          "sid": "SM37e1d0d26f2ac513fbb30024a10e98fc",
          "date_created": "Thu, 19 Mar 2015 20:14:22 +0000",
          "date_updated": "Thu, 19 Mar 2015 20:14:22 +0000",
          "date_sent": "Thu, 19 Mar 2015 20:14:22 +0000",
          "account_sid": "AC2a0f5850342e7c43785ab72742e0bec0",
          "to": "+17324918525",
          "from": "+19733438312",
          "body": "Si",
          "status": "received",
          "num_segments": "1",
          "num_media": "0",
          "direction": "inbound",
          "api_version": "2010-04-01",
          "price": "-0.00750",
          "price_unit": "USD",
          "error_code": null,
          "error_message": null,
    }]

I've come across this issue myself. 我自己遇到了这个问题。 Seeing as you're using the PHP library I can try resolve this issue for you. 看到您正在使用PHP库时,我可以尝试为您解决此问题。 In this section: 在这个部分:

// Loop over the list of messages echo each key property
foreach ($client->account->messages as $message) {
    $list_sms_messages[]=array(
        'timestamp'=>$message->dateSent,
        'from'=>$message->from ,
        'to'=>$message->to, 
        'body'=> $message->body
    );
}

The $message->dateSent is actually a PHP DateObject so you can fetch the timestamp in Epoch format by change it to: $message->dateSent->getTimestamp() $message->dateSent实际上是一个PHP DateObject,因此您可以通过将其更改为以下格式来获取Epoch格式的时间戳: $message->dateSent->getTimestamp()

The returned timestamp can then be formatted with the date() function. 然后可以使用date()函数格式化返回的时间戳。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM