PHP Classes

Login from Twitter re: Downloading Images from Direct Messages

Recommend this page to a friend!

      PHP OAuth Library  >  All threads  >  Login from Twitter re: Downloading...  >  (Un) Subscribe thread alerts  
Subject:Login from Twitter re: Downloading...
Summary:Can't download direct message images to server
Messages:6
Author:Ouiji
Date:2015-09-25 17:38:20
 

  1. Login from Twitter re: Downloading...   Reply   Report abuse  
Picture of Ouiji Ouiji - 2015-09-25 17:38:20
I'm using Twitter OAuth to grab the most recent direct message from my account. I can get the tweet just fine and can get the image url with no trouble. Can't download the image once I have the url. Any suggestions as to how to do that?

Here's an excerpt of the code I'm using:

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


Then this:
if($success)
{
$imagetoget=$user[0]->entities->media[0]->media_url;
$imagetosave = file_get_contents($imagetoget);
file_put_contents('/filetosave.jpg', $imagetosave);
}

  2. Re: Login from Twitter re: Downloading...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2015-09-26 02:13:30 - In reply to message 1 from Ouiji
Actually I am not able to access direct messages with that call although my app has Read-Write-DirectMessages permissions. So I am not able to reproduce the problem. Do you have any idea why?

  3. Re: Login from Twitter re: Downloading...   Reply   Report abuse  
Picture of Ouiji Ouiji - 2015-09-26 02:32:32 - In reply to message 2 from Manuel Lemos
It may only get recent Direct Messages - not sure. But it should work. Here's the code that produces a url from the most recent DM for me:


require('http.php');
require('oauth_client.php');
$client = new oauth_client_class;
$client->debug = false;
$client->debug_http = true;
$client->server = 'Twitter';
$client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].
dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/login_with_twitter.php';
if(defined('OAUTH_PIN'))
$client->pin = OAUTH_PIN;
$client->client_id = ''; $application_line = __LINE__;
$client->client_secret = '';

if(strlen($client->client_id) == 0
|| strlen($client->client_secret) == 0)
die('Please go to Twitter Apps page https://dev.twitter.com/apps/new , '.
'create an application, and in the line '.$application_line.
' set the client_id to Consumer key and client_secret with Consumer secret. '.
'The Callback URL must be '.$client->redirect_uri.' If you want to post to '.
'the user timeline, make sure the application you create has write permissions');

if(($success = $client->Initialize()))
{
if(($success = $client->Process()))
{
if(strlen($client->access_token))
{
$success = $client->CallAPI(
'https://api.twitter.com/1.1/direct_messages.json',
'GET', array('IncludeEntities'=>true,'count'=>'1'), array('FailOnAccessError'=>true), $user);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;
if($success)
{

$imagetoget=$user[0]->entities->media[0]->media_url;
echo $imagetoget;

}

?>

  4. Re: Login from Twitter re: Downloading...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2015-09-26 02:59:54 - In reply to message 1 from Ouiji
I sent a new direct message and was able to see new messages. Old messages were not accessible. So I could reproduce the problem.

Assuming that you want to access the first image of the first direct message, you should retrieve the media data using the following URL that looks like:

ton.twitter.com/1.1/ton/data/dm/647 ...

$client->CallAPI( $messages[0]->entities->media[0]->media_url, 'GET', array(), array('FailOnAccessError'=>true), $media);

  5. Re: Login from Twitter re: Downloading...   Reply   Report abuse  
Picture of Ouiji Ouiji - 2015-09-26 04:16:37 - In reply to message 4 from Manuel Lemos
Manueal,

Holy krikes dude. You're a beast. That totally worked. Took me X # of days of fumbling and stumbling with no results and you just freakin' hammered it.

Many thanks.

  6. Re: Login from Twitter re: Downloading...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2015-09-26 18:27:29 - In reply to message 5 from Ouiji
Yes, it was not obvious and it is something not documented in Twitter documentation but I found a comment in Twitter forums of somebody that tried all URLs until he figure the right one.

I guess I will probably write an article about this.