Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Twitter API PHP Wiki

James Mallison edited this page Jun 18, 2013 · 7 revisions

The following are example requests to make, assuming you have set up your settings array appropriately and have followed the instructions on this page correctly.

Basic Examples

Get a user's tweets - Documentation Link

$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 - Documentation Link

$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 Documentation Link

$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 - Documentation Link

$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 - Documentation Link

$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));
Clone this wiki locally