Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

PHP: getting latest Tweets and displaying them in HTML

 PHP: getting the latest Tweets and displaying them in HTML

This post shows how you can get the latest Tweets from a Twitter account and displaying them in HTML, with links for all the entities (urls, hashtags, user mentions, etc).

Step 1) Create a Twitter App

 

You must create a Twitter application and get a couple of tokens that will be used later. 

Step 2) Get latest Tweets

 

For retrieving the latest tweets from the Twitter APIs libraries are listed 

The API we need to use is the statuses/user_timeline and we’ll get the JSON returned by this API converted as PHP object. You can use the library you prefer, provided that you can get the PHP object from the JSON returned from the Twitter’s API.

Example
This is a simple code that use the TwitterAPIExchange library and gets the latest tweets from our Twitter account named : nikunjjoshiphp

// Require J7mbo's TwitterAPIExchange library (used to retrive the tweets)
// You can get this library from here: https://github.com/J7mbo/twitter-api-php
require_once('vendor/j7mbo/twitter-api-php/TwitterAPIExchange.php');

// Set here your twitter application tokens
$settings = array(
  'consumer_key' => 'CONSUMER_KEY',
  'consumer_secret' => 'CONSUMER_SECRET'
  
  // These two can be left empty since we'll only read from the Twitter's 
  // timeline
  'oauth_access_token' => '',
  'oauth_access_token_secret' => '',
);

// Set here the Twitter account from where getting latest tweets
$screen_name = 'nikunjjoshiphp';

// Get timeline using TwitterAPIExchange
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = "?screen_name={$screen_name}";
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
$user_timeline = $twitter
  ->setGetfield($getfield)
  ->buildOauth($url, $requestMethod)
  ->performRequest();

$user_timeline = json_decode($user_timeline);

Inside the $user_timeline we’ll have the API response as a PHP object, containing the latest tweets.

Format tweets in HTML with TwitterTextFormatter

The main problem with each library that wraps Twitter’s APIs is that the returned tweets are pure text and they don’t provide the HTML formatted text.

In order to get the tweet’s text formatted in HTML, with links for all the tweet’s entities, we use the PHP class

In contrast to commons methods, such class uses Twitter’s entities retrieved from the API response, instead of replacing URLs and entities using a regular expression that is an error-prone method.

Usage example

Referring the code listed before, we can use the TwitterTextFormatter class in this way:

// Require our TwitterTextFormatter library
// You can get this class from here: https://goo.gl/bTfdWS
require_once('TwitterTextFormatter.php');

// Use the class TwitterTextFormatter
use Netgloo\TwitterTextFormatter;

// Use the code above to fill the $user_timeline with latest tweets

// ...

// Print each tweet using TwitterTextFormatter to get the HTML text
echo "<ul>";
foreach ($user_timeline as $user_tweet) {
  echo "<li>";
  echo TwitterTextFormatter::format_text($user_tweet);
  echo "</li>";
}
echo "</ul>";

Appendix A: some useful fields from Twitter API

The following are some useful fields you can get from the tweet object returned by the API (e.g. the $user_tweet object in the code above). For most of them the name is self-descriptive:

  • created_at
  • retweet_count
  • user->screen_name
  • user->profile_image_url
  • retweeted_status : If this properties is set (check it with the PHP function isset) the current tweet is a “re-tweet” (and the fields below are available).
  • user->name
  • retweeted_status->user->name
  • retweeted_status->user->screen_name
  • retweeted_status->retweet_count

Example
This code will print also the tweet’s image:

// ...

  if (isset($user_tweet->entities->media)) {
    $media_url = $user_tweet->entities->media[0]->media_url;
    echo "<img src='{$media_url}' width='100%' />";
  }
  
  // ...

Try it yourself
You can get the code used in this post from our Github repository and try it by yourself:

If you have any query or suggestions, feel free to ask me via the comment section below.

must watch some other topics :

cake PHP training institute
cake PHP training course
cake php training online

Post a Comment

0 Comments