Twitter search by hashtag example API v1.1

In the past, using Twitter API version 1, I used the following URL to get a JSON feed of all tweets with the hashtag "baseball":

http://search.twitter.com/search.json?q=%23baseball&result_type=recent

How do you achieve a similar result using API version 1.1? I'm using PHP as my server-side code, so not sure if I need to use it to authenticate and such?

Sample code would be extremely helpful. Thanks.

As you know, authenticated requests are now required, so there's a few things that you may need to take a look at first. The new 1.1 search, how to use hashtags, and authentication.

Twitter Search for 1.1

The new twitter search api docs can be found here. According to these docs:

https://api.twitter.com/1.1/search/tweets.json is the new resource URL to use for search.

Hashtag searches

You've got that part right! %23 decodes to a # character.

Authentication

OAuth is a lot more complex. It would help if you just used a library that just worked.

Here's a post a lot of people found useful to help you make authenticated requests to the 1.1 API. This includes a one-file include library to make requests like those you require.

Example

This example assumes you're using the above library and set up your keys etc. To make your request:

// Your specific requirements
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#baseball&result_type=recent';

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

Yes, that's it. Apart from the little setting up you need to do (as my post explains), for your dev keys, that's everything you need to perform authenticated requests.

Response

The response is returned to you in JSON. From the overview:

API v1.1 will support JSON only. We've been hinting at this for some time now, first dropping XML support on the Streaming API and more recently on the trends API. We've chosen to throw our support behind the JSON format shared across the platform.

Twitter API - Display all tweets with a certain hashtag?, return results with HASHTAG ONLY. So your query would become: GET https://​api.twitter.com/1.1/search/tweets.json?q=%23freebandnames. A Simple Way to Search Tweets by a Hashtag with Twitter API v1.1 The followings are sample PHP codes to search tweets by a hashtag, in a simple form with The first PHP Library to support OAuth for Twitter’s REST API which supports the version 1.1 of Twitter API. Sample Code – Search Tweets by a Hashtag

If you just want to test, you can do the follow:

Access the twitter dev console: https://dev.twitter.com/console

In Authentication put: OAuth 1, that will ask you to give permission from your twitter account.

Request URL put GET

In url: https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag

After Send, in Request window, copy the Authorization value.

Now put it in your request header.

Go example:

func main() {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://api.twitter.com/1.1/search/tweets.json?q=%23golang", nil)
    req.Header.Add("Authorization", `OAuth oauth_consumer_key=...`)

    resp, _ := client.Do(req)
    io.Copy(os.Stdout, resp.Body)
}

Standard search API, Here's an example: Note that the standard search API only serves data from the last week. You want: popular Tweets from @Cmdr_Hadfield mentioning the hashtag #nasa; Your search URL is: https://api.twitter.com/1.1/search/tweets.json​  1- How to search Tweets by hashtag with Twitter API or other API ? A : With Twitter API. Search Tweets by hashtag with Twitter API may be done since 2018 via three new API levels. Standard search API : free and perfect for main API users with basic features and limited quantity content returned.

Here's a simple example in python using application-only auth using the requests API. Get keys by creating an app at https://apps.twitter.com/app/new.

api_key = ...
api_secret = ...

# https://dev.twitter.com/oauth/application-only
# The base64 stuff described there is the normal Basic Auth dance.
import requests
r = requests.post('https://api.twitter.com/oauth2/token',
                  auth=(api_key, api_secret),
                  headers={'Content-Type':
                      'application/x-www-form-urlencoded;charset=UTF-8'},
                  data='grant_type=client_credentials')
assert r.json()['token_type'] == 'bearer'
bearer = r.json()['access_token']

url = 'https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag'
r = requests.get(url, headers={'Authorization': 'Bearer ' + bearer})
print r.json()

Using standard search, You can still navigate through our v1 documentation from the links at the end of the Labs v1 "https://api.twitter.com/labs/1/tweets?ids=1067094924124872705" For example, if the parent Tweet is a Retweet, a Quoted Tweet or a Reply, it will entities.hashtags, array, Contains details about text recognized as a Hashtag. I know how to get tweets from a hashtag (Twitter request to get the latest tweets for a hashtag using Twitter API V1.1 using OAuth settings) But it appears that some tweets associated with a hashtag aren't retrieved, even with the parameter "recent". And if I tweet on #somethingforexample, there isn't a tweet with the api for that same hashtag.

Building standard search queries, The entities node will not be included in embedded Tweet objects when set to false . false. Example Request¶. $ curl --request GET --url 'https://api.twitter.com/​1.1/  Python Twitter Search API¶ This project serves as a wrapper for the Twitter premium and enterprise search APIs , providing a command-line utility and a Python library. Pretty docs can be seen here .

GET /labs/1/tweets, If you want to skip right to the API, check out the REST API v1.1 documentation. try typing #BigData in the query field to search for the BigData hashtag. ://dev.​twitter.com/docs/auth/oauth/single-user-with-examples#php). Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Jobs Programming and related technical career opportunities

GET users/search, I'm not going to give a beginners tutorial on PHP here because there are plenty examples on the web- including a simple User Timeline: https://api.twitter.com/​1.1/statuses/user_timeline.json A Twitter search with the query you specify. Hi, How to get the first tweet with any new hashtag using PHP. 0. How would I go about displaying tweets that contain a certain hashtag using the Twitter API? Thanks. I'd also like to know if there is a way to get all tweets from a certain hashtag in a separate file, also the ones that don't show up in your feed anymore.

Comments
  • @Jimbo : Thanks for great answer(+1).I have one problem I am making one web-app which based on ur solution But I got error "Rate limit exceeded " error message How can I resolve it I want to know I have to follow other authentication method ?? Can I use twiter account of user who are using my web-app. please help thanks..
  • Great example! Thank you!
  • Great that I am searching for #tag serch! "You've got that part right! %23 decodes to a # character"
  • @KirenSiva Using the above library, you shouldn't need to type in any encoded characters into $getfield as the library does this automatically for you.
  • what if I don't want to search based on hash tag. I want to fetch all the statuses that are updated in last 1 day.?