├── LICENSE ├── README.md ├── callback.php ├── get-recent-user-tweets.php ├── index.php ├── post-tweet.php └── tweet-with-media.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sohaib Ilyas (Roomi) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # twitter-api-php 2 | All examples are related to Twitter API using PHP. 3 | -------------------------------------------------------------------------------- /callback.php: -------------------------------------------------------------------------------- 1 | oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); 12 | $_SESSION['access_token'] = $access_token; 13 | // redirect user back to index page 14 | header('Location: ./'); 15 | } 16 | -------------------------------------------------------------------------------- /get-recent-user-tweets.php: -------------------------------------------------------------------------------- 1 | oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); 14 | $_SESSION['oauth_token'] = $request_token['oauth_token']; 15 | $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 16 | $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); 17 | echo $url; 18 | } else { 19 | $access_token = $_SESSION['access_token']; 20 | $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 21 | 22 | // getting basic user info 23 | $user = $connection->get("account/verify_credentials"); 24 | 25 | // printing username on screen 26 | echo "Welcome " . $user->screen_name . '
'; 27 | 28 | // getting recent tweeets by user 'snowden' on twitter 29 | $tweets = $connection->get('statuses/user_timeline', ['count' => 200, 'exclude_replies' => true, 'screen_name' => 'snowden', 'include_rts' => false]); 30 | $totalTweets[] = $tweets; 31 | $page = 0; 32 | 33 | for ($count = 200; $count < 500; $count += 200) { 34 | $max = count($totalTweets[$page]) - 1; 35 | $tweets = $connection->get('statuses/user_timeline', ['count' => 200, 'exclude_replies' => true, 'max_id' => $totalTweets[$page][$max]->id_str, 'screen_name' => 'snowden', 'include_rts' => false]); 36 | $totalTweets[] = $tweets; 37 | $page += 1; 38 | } 39 | 40 | // printing recent tweets on screen 41 | $start = 1; 42 | foreach ($totalTweets as $page) { 43 | foreach ($page as $key) { 44 | echo $start . ':' . $key->text . '
'; 45 | $start++; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); 13 | $_SESSION['oauth_token'] = $request_token['oauth_token']; 14 | $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 15 | $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); 16 | echo $url; 17 | } else { 18 | $access_token = $_SESSION['access_token']; 19 | $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 20 | $user = $connection->get("account/verify_credentials"); 21 | echo $user->screen_name; 22 | } 23 | -------------------------------------------------------------------------------- /post-tweet.php: -------------------------------------------------------------------------------- 1 | oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); 13 | $_SESSION['oauth_token'] = $request_token['oauth_token']; 14 | $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 15 | $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); 16 | echo $url; 17 | } else { 18 | $access_token = $_SESSION['access_token']; 19 | $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 20 | 21 | // getting basic user info 22 | $user = $connection->get("account/verify_credentials"); 23 | 24 | // printing username on screen 25 | echo $user->screen_name; 26 | 27 | // posting tweet on user profile 28 | $post = $connection->post('statuses/update', array('status' => 'https://sohaibilyas.com my website')); 29 | 30 | // displaying response of $post object 31 | print_r($post); 32 | } 33 | -------------------------------------------------------------------------------- /tweet-with-media.php: -------------------------------------------------------------------------------- 1 | oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); 13 | $_SESSION['oauth_token'] = $request_token['oauth_token']; 14 | $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 15 | $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); 16 | echo $url; 17 | } else { 18 | $access_token = $_SESSION['access_token']; 19 | $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 20 | 21 | // getting basic user info 22 | $user = $connection->get("account/verify_credentials"); 23 | 24 | // printing username on screen 25 | echo "Welcome " . $user->screen_name; 26 | 27 | // uploading media (image) and getting media_id 28 | $tweetWM = $connection->upload('media/upload', ['media' => 'https://pbs.twimg.com/profile_images/695720184464740353/lnOGP0Z8_400x400.jpg']); 29 | 30 | // tweeting with uploaded media (image) using media_id 31 | $tweet = $connection->post('statuses/update', ['media_ids' => $tweetWM->media_id, 'status' => 'tweeting with image file']); 32 | print_r($tweet); 33 | } 34 | --------------------------------------------------------------------------------