PHP Classes
PHP Classes
elePHPant
Icontem

Get Twitter Direct Message Images in PHP with the OAuth API - PHP OAuth Library package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP OAuth Library PHP OAuth Library   Blog PHP OAuth Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog Get Twitter Direct Me...  
  Post a comment Post a comment   See comments See comments (3)   Trackbacks (0)  

Author:

Updated on: 2015-12-10

Posted on:

Package: PHP OAuth Library

Twitter API allows applications to access users' direct messages but if you want to download images or other media associated with those direct messages, there is no specific API call to do so.

Direct messages are private, so there is no public URL to access the direct messages' images without using OAuth or you are a real logged user accessing Twitter via a browser.

Read this article to learn how to download images that belong to direct messages using authenticated requests with this PHP OAuth client class.




Contents

Accessing Twitter Users' Direct Messages

Downloading Images or Media of Direct Messages

Conclusion


Accessing Twitter Users' Direct Messages 

Accessing direct messages sent or received by a given user with Twitter API it is easy. You just need to setup your application to have access to direct messages permissions before obtaining the oAuth token for the authenticated user.

You can set those permissions in the Permissions tab of your Twitter application page

Then you should call the direct_messages API call to retrieve the messages using the PHP OAuth API client class like this.

Take a look at the login_with_twitter.php file for a complete example script of usage of this class to access the Twitter API.

$client->CallAPI( 'https://api.twitter.com/1.1/' . 'direct_messages.json', 'GET', array('count'=>'1'), array( 'FailOnAccessError'=>true ), $messages);

The $messages return value returns all the details of the direct messages you requested.

Downloading Images or Media of Direct Messages

Direct messages are like public tweets, except that they are private. This implies that you can only access the contents of direct messages if the user that gave your application permission has access to those direct messages.

The same goes for any images or other media attached to the direct messages. You can only access those images using oAuth requests to the with a token granted by the user that has access to those direct messages.

All this may seems obvious but there are two problems that I realized when a user of the PHP OAuth API client class brought to my attention. He wanted to simply use file_get_contents on the media URL, but as I explained above, that does not work on direct messages because they are private.

The first problem is that there is no specific Twitter API call to retrieve media files attached to messages. Twitter API documentation does not hint about how to get attached media files. So I figured that the actual URL of the media must be derived from information in direct messages API call response.

The second problem is that the response lists several different media URL but they only work when you access them as authenticated user via your browser.

For instance if you check the entities urls elements you see several URLs like this and none can be used to download the images via OAuth.

[urls] => Array
(
  [0] => stdClass Object
    (
      [url] => https://t.co/z0xKAqzZCj
      [expanded_url] => https://twitter.com/messages/ media/647599433107046403
      [display_url] => pic.twitter.com/z0xKAqzZCj
      [indices] => Array
        (
          [0] => 8
          [1] => 31
        )
    )
)

The right place to look is in the media array part of the message response. You will see something like this:

[media] => Array
(
  [0] => stdClass Object
    (
      [id] => 647599433211842560
      [id_str] => 647599433211842560
      [indices] => Array
        (
          [0] => 8
          [1] => 31
        )

      [media_url] => https://ton.twitter.com/1.1/ton/ data/dm/647599433107046403/ 647599433211842560/wvYux6nk.png
      [media_url_https] => https://ton.twitter.com/1.1/ton/ data/dm/647599433107046403/ 647599433211842560/wvYux6nk.png
      [url] => https://t.co/z0xKAqzZCj
      [display_url] => pic.twitter.com/z0xKAqzZCj
      [expanded_url] => https://twitter.com/messages/ media/647599433107046403
      [type] => photo

You see the same URLs except for one named media_url. That is the one you need to use to download the image.

So if you want to accesss the first image of the first direct message you retrieved with the API call like the above, you need to use code like this:

$client->CallAPI($messages[0] -> entities -> media[0] -> media_url, 'GET', array(), array( 'FailOnAccessError'=>true ), $media);
When you make this call the $media variable will contain the binary data of the image you want. Then you can just use file_put_contents to save it to a file.

Conclusion

Unfortunately many Web APIs are not as well documented as they should be because the API providers did not use their APIs enough to stumble upon problems like this.

All it is left for use developers is to resort to help forums like the user I mentioned above did to figure a solution for this problem.

If you like this article or have questions this problem or other related with the PHP OAuth API class, post a comment here.




You need to be a registered user or login to post a comment

1,484,510 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

2. how to get direct messages which is replied by me - vishal Coderkube (2018-02-10 06:32)
give the code... - 1 reply
Read the whole comment and replies

2. how to get direct messages which is replied by me - vishal Coderkube (2018-02-10 06:32)
give the code... - 1 reply
Read the whole comment and replies



  Post a comment Post a comment   See comments See comments (3)   Trackbacks (0)  
  All package blogs All package blogs   PHP OAuth Library PHP OAuth Library   Blog PHP OAuth Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog Get Twitter Direct Me...