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.
Replace “https://twitter.com/search” with “https://api.twitter.com/1.1/search/tweets.json” and you will get: https://api.twitter.com/1.1/search/tweets.json?q=twitterdev%20new%20premium. Execute this URL to do the search in the API.
When you see the results, navigate to the 'Search filters' menu on the right, then click “Advanced search”. This will take you to the advanced search options. 3. If you're searching for a word, phrase, or hashtag used in a Tweet or bio — start with the “Words” section.
How to build a query. The best way to build a query and test if it's valid and will return matched Tweets is to first try it at twitter.com/search. As you get a satisfactory result set, the URL loaded in the browser will contain the proper query syntax that can be reused in the API endpoint.
Enter your search into the search bar on twitter.com. Click Advanced search, located underneath Search filters on the upper right of your results page, or click More options and then click Advanced search. Fill in the appropriate fields to refine your search results (see below for some helpful tips).
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.
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) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With