- Choosing a Library
- Registering with Providers
- Installing the HybridAuth Library
- Communicating with Twitter
- Conclusion
Communicating with Twitter
The HybridAuth library comes with several practical examples that I recommend you look at and play with. For a simple but useful start, let’s write out the code required to connect to a user’s Twitter account and then retrieve a modicum of information about that account (see Figure 3). As a reminder, the goal here is to have your site display information from the current user’s Twitter account.
Figure 3 The resulting page (on my site) that communicates with Twitter.
First, HybridAuth requires sessions to maintain the authentication, so your PHP script must include:
session_start();
Then you should identify the configuration file (which now tells HybridAuth how to connect to Twitter and the rest), and include the HybridAuth library:
$config = dirname(__FILE__) . '/hybridauth/config.php'; require_once('hybridauth/Hybrid/Auth.php');
Next, create the Hybrid_Auth object, providing it with the configuration file:
$ha = new Hybrid_Auth($config);
And log in with Twitter:
$t = $ha->authenticate('Twitter');
This one line actually serves two purposes. If the user is not already authenticated via Twitter, this code will redirect the browser to Twitter so that the user may allow authentication. But if the user has already performed authentication via Twitter, then this line just verifies the authentication, allowing this site (i.e., the rest of the code) to make use of that Twitter account.
Next, just to be safe, confirm that the user is logged in:
if ($t->isUserConnected()) {
This method returns a Boolean value, so it can be used in a conditional as such.
If the user is logged in, get her profile:
$profile = $t->getUserProfile();
This method will get the user’s profile information. It’s fetched into a custom HybridAuth object, which will be used in a moment.
Note that all of the code to this point is standardized in HybridAuth. Connecting to Facebook instead of Twitter merely requires changing the word “Twitter” to “Facebook” in the authenticate() call. Further, at this point in time, you would consider the user logged into your system, and you could use an aspect of the user’s profile as the representation of the user on your site.
Having fetched the user’s profile, display some of the information:
echo "<p><strong>Twitter Handle</strong>: <a href=\"{$profile->profileURL}\">{$profile->displayName}</a></p> <p><strong>Description</strong>: {$profile->description}</p>";
Again, $profile is an object, so object syntax is used. You can see the Twitter documentation to know what information would be returned, or just do print_r($profile) to see for yourself. For added flair, the user’s Twitter name is linked to the user’s Twitter profile page.
Next, let’s get some more of the user’s information and, in the process, show you how to perform more complicated actions using HybridAuth. The api()->get() method of the Hybrid_Auth object can be used to perform GET requests of the connected resource. Looking at the Twitter documentation, I can see that the users/show.json file will return information about a user if provided with the user’s identifier and screen name. This information was already returned by HybridAuth in the getUserProfile() method. Therefore, I pass to api()->get() two arguments: the resource to request and an array of parameters to send to that resource:
$account = $t->api()->get('users/show.json', array('user_id' => $profile->identifier, 'screen_name'=>$profile->displayName));
Now I can make use of the returned information:
echo "<p><strong>Following</strong>: {$account->friends_count}</p> <p><strong>Followers</strong>: {$account->followers_count}</p> <p><strong>Latest Tweet</strong>: {$account->status->text}</p>";
Again, HybridAuth turns everything into an object. Dumping out the value of $account shows me what’s there. This includes the number of followers, the number of people the user is following, and even the most recent tweet. That’s available through $account->status, and the specific text of the tweet (as opposed to its other properties) is in the text property.
Finally, you can close the Twitter connection:
$t->logout();
Note that this only closes the current connection to Twitter. So long as the user’s session on this site remains active, repeated loadings of this page will continue to work without re-authentication. (You can test that for yourself.)