📜 ⬆️ ⬇️

Developing an oAuth application for the Twitter API in PHP

In this article I will tell you about working with the Twitter API using the OAuth protocol in PHP.
The OAuth protocol provides the application with access to user data without passing on the user's login and password. New application authorization rules require the use of “OAuth” technology to work with Twitter starting from August 31.
The test application that comes out as a result will be able to display the user's message feed, the latest status of his followers, and by clicking on the button next to each follower or friend status, you can read the entire feed of this user.

So, first create a new application for this link.

Now we have the necessary data that should be saved in config.php

define('CONSUMER_KEY', 'Your consumer key');
define('CONSUMER_SECRET', 'You consumer secret');
define('OAUTH_CALLBACK', 'http://yousite.ru/callback.php');

On the connect.php twitter connection page, we connect the above data and check their availability:
if (CONSUMER_KEY === '' || CONSUMER_SECRET === '') {
echo 'You need a consumer key and secret to test the sample code. Get one from https://twitter.com/apps'; exit;}

Begin the connection process (go to the redirect.php page):

$content = 'Sign in with Twitter';
<?php print_r($content); ?>

We connect the library to work with the Twitter API and application account information on redirect.php


Create a TwitterOAuth object based on our application account:

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

Next we get temporary tokens from Twitter and save them:

$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

On the basis of a temporary token, we send the user to log in to Twitter:

$url = $connection->getAuthorizeURL($token);

If the received token is old, then we clean the session and send the user to the page to connect to Twitter:

if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
$_SESSION['oauth_status'] = 'oldtoken';
header('Location: ./clearsessions.php');

On the page clearsessions.php call:

header('Location: ./connect.php');

If everything went well, then we create a TwitterOAuth object based on the account of our application and time tokens:

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

Now we get the access key from Twitter, which should be saved for future use:

$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
$_SESSION['access_token'] = $access_token;

Temporary tokens are no longer needed:


Next, go to the page of our app:

header('Location: ./index.php');

On the index.php page, we save the user's token to a variable:

$access_token = $_SESSION['access_token'];

Create a TwitterOAuth object based on our application account and user tokens:

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

Now we can use various methods to get user data.
For example, get user account details.

$content_user = $connection->get('account/verify_credentials');
$array_user = (array)$content_user;

Based on the user's id, you can receive messages from its feed by specifying id in the parameters array (in the parameters array, you can also specify the number of records to be displayed):

$content_friends=$connection->get('statuses/friends_timeline',array('id' => $user_id));

Recent posts of all user followers:

$content_followers=$connection->get('statuses/followers', array('id' => $user_id));

Own tape of user messages (by default, only 20 records are displayed, the maximum number of output - 200).

$content_userline=$connection->get('statuses/user_timeline', array('id' => $user_id,'count'=>200));

Other methods can be invoked on the same principle.

Twitter feed: api.twitter.com/1/statuses/public_timeline.xml
Visitor messages you wrote to: api.twitter.com/1/statuses/mentions.xml?count=5&page=1
My retweets: api.twitter.com/1/statuses/retweeted_by_me.xml?count=5&page=3
Retweeted me: api.twitter.com/1/statuses/retweeted_to_me.xml?count=5&page=3
And other methods.

echo print_r($content_friends); //

The result when receiving a message feed will be an array of stdClass Object elements:
[0] => stdClass Object
[created_at] => Sat Sep 04 11:01:48 +0000 2010
[in_reply_to_screen_name] =>
[source] => IncredebleTB
[retweeted] =>
[truncated] =>
[in_reply_to_status_id] =>
[in_reply_to_user_id] =>
[contributors] =>
[place] =>
[coordinates] =>
[user] => stdClass Object
[show_all_inline_media] =>
[profile_background_image_url] => s.twimg.com/a/1283555538/images/themes/theme16/bg.gif
[favourites_count] => 0
[profile_image_url] => a0.twimg.com/profile_images/1105647496/robot_normal.jpg
[description] => Web design, web application development, website creation
[contributors_enabled] =>
[profile_sidebar_fill_color] => f2f2f2
[url] => dandreev.com
[geo_enabled] =>
[profile_background_tile] =>
[screen_name] => incredibleTB
[lang] => en
[created_at] => Mon Apr 26 20:32:14 +0000 2010
[profile_sidebar_border_color] => a8a8a8
[location] => Moscow
[verified] =>
[follow_request_sent] =>
[following] => 1
[profile_background_color] => 9AE4E8
[followers_count] => 99
[protected] =>
[profile_text_color] => 141414
[name] => Dmitry Andreev
[listed_count] => 0
[profile_use_background_image] => 1
[time_zone] => Quito
[friends_count] => 48
[id] => 137459247
[statuses_count] => 900
[notifications] =>
[utc_offset] => -18000
[profile_link_color] => 0084B4

[geo] =>
[retweet_count] =>
[id] => 22967688386
[favorited] =>
[text] => Craigslist Censored: Adult Section Comes Down bit.ly/9evRKN via (@techcrunch)

To access them, you can access for example:

$array_friends = (array)$content_friends;
foreach ($array_friends as $friends_sets)
$friends_sets=(array)$friends_sets; // stdClass Object
$friend_name=$friends_sets["user"]->name; // stdClass Object

The library for oAuth in php can be downloaded here .
The application, which turned out in the end, here . The application contains 3 tapes with pagination (followers, friends and personal tape of any user). Additionally, the jTweetsAnywhere library is enabled, allowing you to display information about each user (the Connect with Twitter button at the top).

Source: https://habr.com/ru/post/103548/

All Articles