Linuxx Linuxx - 13 days ago 5
PHP Question

PHP Twilio Can't filter messages

I am attempting to retrieve message history using the PHP helper library (latest from GitHub) and I can't seem to make the filter work properly. I want to only see messages after 2016-11-02. If I remove the "grater than" symbol, I see messages created on 2016-11-02. If I include the "grater than" symbol, I receive all messages (even those that are older than 2016-11-02). Here is my code;

Using this I see all messages (including messages before and after 2016-11-02)

$client = new Client($account_sid, $auth_token);
echo "<pre>\n";
foreach ($client->messages->read(array( 'dateSent>' => date('2016-11-02'))) as $message) {
echo $message->from . "\n";
echo $message->to . "\n";
echo $message->body . "\n";
echo $message->status . "\n";
echo $message->dateSent->format('Y-m-d H:i:s') . "\n";
echo "<br />\n";
}
echo "</pre>\n";


Using this I see messages only from 2016-11-02

$client = new Client($account_sid, $auth_token);
echo "<pre>\n";
foreach ($client->messages->read(array( 'dateSent' => date('2016-11-02'))) as $message) {
echo $message->from . "\n";
echo $message->to . "\n";
echo $message->body . "\n";
echo $message->status . "\n";
echo $message->dateSent->format('Y-m-d H:i:s') . "\n";
echo "<br />\n";
}
echo "</pre>\n";


I've tried;

array( 'dateSent>' => date('2016-11-02'))
array( 'dateSent >' => date('2016-11-02'))
array( 'DateSent>' => date('2016-11-02'))
array( 'DateSent >' => date('2016-11-02'))
array( 'date_sent>' => date('2016-11-02'))
array( 'date_sent >' => date('2016-11-02'))

Answer

I did some digging starting here https://www.twilio.com/docs/api/rest/message#list-get-filters

At first glance, it seems like at least one of what you tried should have worked. Specifically array( 'DateSent>' => date('2016-11-02')). However, two things seemed odd. First, that documentation mentions sending the filters as querystring parameters, which is not what's happening here. Second, it clearly says that the parameters are case-sensitive. However, the example that you did get to work had array( 'dateSent' => date('2016-11-02')), which does not match the indicated case of DateSent.

So the API documentation was a bust. Off to the actual code!

After following a trail of classes and methods, we end up here https://twilio.github.io/twilio-php/source-class-Twilio.Rest.Api.V2010.Account.MessageList.html#99-117. Now, the read() method itself is again rather unhelpful for solving this mystery. So we follow that to stream() and then to page(), which finally gives us what we need:

$params = Values::of(array(
            'To' => $options['to'],
            'From' => $options['from'],
            'DateSent<' => $options['dateSentBefore'],
            'DateSent' => $options['dateSent'],
            'DateSent>' => $options['dateSentAfter'],
            'PageToken' => $pageToken,
            'Page' => $pageNumber,
            'PageSize' => $pageSize,
        ));

Aha! You successfully used dateSent in your code, and here we see it being used! So maybe, if we're lucky enough, we found the array indexes that you need to use.

Ultimately, the answer to your question is that you need to use $client->messages->read(array( 'dateSentAfter' => '2016-11-02')) (there's really no need to call the date() function like you're doing in your original code)