Twitter exposes its data via an Application Programming Interface (API) to read and write Twitter data. An application program interface (API) is code that allows two software programs to communicate with each other. We can connect with Twitter REST APIs with any language like PHP, Java, Node.JS, etc.., to built application with Twitter data. Here, In this article we are going to use PHP to connect with Twitter REST API.
Create Twitter Application
- Go to Twitter Application Management and login using your twitter account.
- Now, Create a New Application by clicking Create New App button.
- Enter your Application Name, Description, Website, Callback URL(Optional) and then confirm your application by accepting Twitter Developer Agreement.
- Submit the form by clicking the Create your Twitter Application
- After creating the Application you will find the details of Consumer API Key, Consumer API Secret, Owner, Owner ID, Access Token and Access Token Secret
Integrate Twitter With PHP Using REST APIs
REST APIs provide programmatic access to read and write Twitter data. Author a new Tweet, read author profile and follower data, and more. The REST API identifies Twitter applications and users using OAuth; responses are available in JSON.
You can view or download the source code on GitHub. You need to add your application details like Consumer API Key, Consumer API Secret, Owner, Owner ID, Access Token and Access Token Secret to configure your API with program.
// Twitter OAuth Config options $oauth_access_token = ''; // Access token $oauth_access_token_secret = ''; // Access token secret $consumer_key = ''; // API key $consumer_secret = ''; // API secret $user_id = ''; // Owner ID $screen_name = ''; // Owner
Sample program is for statuses/user_timeline to get most recent Tweets posted by the user(twitter feed). I also used pagination based on
since_id (Working with Timelines), So that you can get the required number of tweets per page. Sample code is written as service for mobile application, Hence I return it as JSON format. You can test this code with Postman for pagination. Please leave your valuable comments/suggestions/feedback on below comment box if any. Also contact me if your facing any issues on this code.
Thank You !!