Popular Post: jQuery Countdown

Send Tweets to your Twitter Account via PHP

The aim of TwitterAPIExchange class is simple. You need to:

You really can't get much simpler than that. Here is an example of how to use the class for a POST request to block a user, and at the bottom is an example of a GET request.

You can also see here:

Installation

Normally: If you don't use composer, don't worry - just include TwitterAPIExchange.php in your application.

Via Composer: If you do use composer, here's what you add to your composer.json file to have TwitterAPIExchange.php automatically imported into your vendor's folder:

{
    "require": {
        "j7mbo/twitter-api-php": "dev-master"
    }
}

Of course, you'll then need to run php composer.phar update.

How To Use

Include the class file

require_once('TwitterAPIExchange.php');

Set access tokens

$settings = array(
    'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
    'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
    'consumer_key' => "YOUR_CONSUMER_KEY",
    'consumer_secret' => "YOUR_CONSUMER_SECRET"
);

Choose URL and Request Method

$url = 'https://api.twitter.com/1.1/blocks/create.json';
$requestMethod = 'POST';

Choose POST fields

$postfields = array(
    'screen_name' => 'usernameToBlock', 
    'skip_status' => '1'
);

Perform the request!

$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
             ->setPostfields($postfields)
             ->performRequest();

GET Request Example

Set the GET field BEFORE calling buildOauth(); and everything else is the same:

$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
$requestMethod = 'GET';

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

Get a user's tweets

Official documentation: https://dev.twitter.com/docs/api/1.1/get/search/tweets

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=j7mbo';
$requestMethod = 'GET';

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

var_dump(json_decode($response));

Search global tweets for a hashtag

Official documentation: https://dev.twitter.com/docs/api/1.1/get/search/tweets

$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=#nerd';
$requestMethod = 'GET';

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

var_dump(json_decode($response));

Delete a tweet

Official documentation: https://dev.twitter.com/docs/api/1.1/post/statuses/destroy/%3Aid

$url = 'https://api.twitter.com/1.1/statuses/destroy/YOURIDHERE.json';
$postfields = array('id' => 'YOURIDHERE');
$requestMethod = 'POST';

$twitter = new TwitterAPIExchange($settings);
$response =  $twitter->buildOauth($url, $requestMethod)
    ->setPostfields($postfields)
    ->performRequest();

var_dump(json_decode($response));

Searching using a geocode

Official documentation: https://dev.twitter.com/docs/api/1.1/get/search/tweets

$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';

$getfield = '?q=test&geocode=37.781157,-122.398720,1mi&count=100";';

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

var_dump(json_decode($response));

Advanced Examples

Search for multiple users, multiple hashtags

Official documentation: https://dev.twitter.com/docs/using-search

$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';

$getfield = '?q=#hashtag1+OR+#hashtag2+from:username1+OR+from:username2';

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

var_dump(json_decode($response));

Post Tweet to your Twitter Account

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';

/** POST fields required by the URL above. See relevant docs as above **/
 $postfields = array('status' => "This is Test tweet by @Massivetechnolabs");
//$postField = 'status=testing';
/** Perform a POST request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
echo $Res = $twitter->buildOauth($url, $requestMethod)
             ->setPostfields($postfields)
             ->performRequest();

Note: If you get blank page when run this code so you need to add

CURLOPT_SSL_VERIFYPEER => false

in TwitterAPIExchange.php after Line No. 195 in performRequest Function

Demo Download

About The Massive Technolab

Avatar
Who We Are

Massive Technolab is a Leading IT services Company in India that expertise in web Development, Web Design and SEO at affordable rates.

Leave a Comment

comments powered by Disqus