0
votes

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. 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. Any ideas

2

2 Answers

1
votes

I've come across this issue myself. Seeing as you're using the PHP library I can try resolve this issue for you. 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()

The returned timestamp can then be formatted with the date() function.

I hope this helps.

0
votes

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:

  • DateSent is actually date_sent ,
  • DateCreated is actually date_created and so on..

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,
    }]