So, well you are clear from the title as we are gonna use Twitter API. With Twitter API, you can build your own bot, tool, search tweets, timeline card, and many other things.
So today, we will learn how to use Twitter search API with examples in PHP and how you can embed your Twitter timeline on your website with coding.
Embedding a Twitter timeline on your website is a perfect way to engage more visitors on your site and they can follow you on twitter. That embed code will be simple as HTML and CSS code and you can use it anywhere on the webs page.
Let’s get started playing with Twitter API!
Twitter Developer Account and PHP Library
Before going forward, you must have twitter developer account. In the developer account, you will get your Consumer Key, Consumer Secret Key, Access Token, and Access Token Secret.
If you don’t have developer account then you can create from here. It will ask for your information and you have to follow the step and boom! Your account was created.
After creating the account, go to this page https://developer.twitter.com/en/portal/dashboard and you will see Projects & Apps at left sidebar. Click on it and create a new App and save your API keys on the notepad.
OK! Now you have all the API keys.
Now need a PHP library to work with them. There are many PHP library but I will recommend you to use TwitterOAuth by Abraham Williams. With this API you can access all the requests of twitter API.
To use this API, you need a composer. The composer is an application-level package manager. If you don’t know how to install then check this tutorial, which I have explained in the tutorial.
OK. Download this library as a zip file and extract it (you can also download it by git command). You will get the “twitteroauth” folder. Now run the composer update
command in this folder path and it will download all the dependencies. Now you are ready to hit your first API request.
Twitter API Example in PHP
Create a new file outside of the “twitteroauth” folder and put the following code in that file.
Note: Please replace the API keys with your API keys.
In the above code, first, we include the autoload.php file and then use the Abraham TwitterOAuth class. And then we declare our own API key (keys replace with yours).
Get User Timeline Using Twitter API
Now, let’s see! how we can create a new TwitterOAuth object to get the user timeline.
In the above code, we have created the new object of TwitterOAuth class and pass the get request to get the user timeline. You will get the object and you use them as per your requirements.
Example data like:
$tweet->id
$tweet->text
$tweet->user->screen_name
$tweet->created_at
You can print them on the webpage as you want.
Check here for more API request endpoints.
Twitter Search API Example in PHP
OK! Let’s try to search some tweets with a custom query like using specific keywords or hashtags or anything.
You can see, we used the “search/tweets” endpoint to get the latest tweets of the #newProfilePic hashtag and a limit of 10.
You can send any query to search tweet and display on your project. For more API references check here.
I hope you understand how you can use Twitter API and Search any Tweets and embed user timelines on your project or website. If you still have any questions then ask me in the comment section I’ll respond to you as soon as possible.