Media entities not showing on most returned tweets



Hi, I’m using TwitterOAuth and search/tweets to pull images from a Twitter profile and display them in a gallery. However, the media entity isn’t displaying for most of the tweets. Here’s the code I’ve used:

				require 'includes/twitteroauth/autoload.php';
				use Abraham\TwitterOAuth\TwitterOAuth;
				$connection = new TwitterOAuth($consumerKey,$consumerSecret,$accessToken,$accessTokenSecret);
				$tweets = $connection->get('search/tweets', array('q'=>'from:JackAHarding filter:images','result_type'=>'mixed','count'=>24,'include_entities'=>true));
				<div class="gallery-container">
					<?php foreach ($tweets->statuses as $tweet) : ?>
						<?php //echo '<pre>';print_r($tweet);echo '</pre>'; ?>
						<div class="gallery-item" style="background-image: url('<?php echo $tweet->entities->media['0']->media_url; ?>')">
							<a title="<?php echo $tweet->text; ?>" href="<?php echo $tweet->entities->media['0']->expanded_url; ?>" target="_blank"></a>
					<?php endforeach; ?>

Any help would be greatly appreciated.

Many thanks.


Interestingly, the media entity is available for all of my tweets from my personal Twitter. However, when I use NASA, or the handle I actually need this for, it only shows one or two.


Try adding the tweet_mode=extended parameter to your API call.


Thanks very much Andy! Any that weren’t still showing were now available under retweeted_status.

However, this has unearthed a new problem - I’ve used ‘count’=>24 which works fine with NASA. However, only a few tweets are being returned for certain handles, for example using Bas Rutten’s handle, only three display when I can see he has many more tweets with images than that on his profile. This is my code:

$tweets = $connection->get('search/tweets', array('q'=>'from:BasRuttenMMA filter:images','result_type'=>'recent','tweet_mode'=>'extended','count'=>24,'include_entities'=>true));

Many thanks.


From a quick look, a lot of the images showing on that profile page come from cards (i.e. a URL was shared in the Tweet, and the image comes from the website behind it). Search only goes back 7 days. I haven’t checked to see how many Tweets with attached images there are for that handle within that period, but this could explain what you’re seeing.


Thanks Andy. That would explain it, I didn’t realise it only went back 7 days. Is there an alternative method I can use to retrieve more than that?


Using statuses/user_timeline can get up to 3200 of the most recent tweets from a specific user. Seeing as how you’re looking at individual users, this will probably be your best option.


That’s great, thanks! Do you know if there is a way to use this and still filter for images only?


No, you would need to filter them inside your own code (if JSON object contains extended entities then keep, else ignore)


very thank