How can I make a simple search on Twitter through PHP, Java, or CURL JSON?
Fetching JSON with Curl:
curl --data-urlencode "q=query here" http://search.twitter.com/search.json
See Twitter's API Reference for more details.
Twitter has a nice REST API for searching. The docs are here:
http://apiwiki.twitter.com/Twitter-Search-API-Method:-search
To search and get JSON back it's as easy as this:
$ curl http://search.twitter.com/search.json?q=stackoverflow
{"results":[{"text":"Saturday night and I'm reading http:\/\/stackoverflow.com\/ #dorkbot","to_user_id":.......
The simplest way? http://twitter.com/#search?q=whatever - then substitute "whatever". You can use HttpClient's HttpGet for that in Java
You can add variations like "search API for user CNN" which would be
http://twitter.com/#search?q=source%3AAPI%20#cnn
If want RSS instead of HTML use (for above query)
http://search.twitter.com/search.atom?q=source%3AAPI%20#cnn
And there's the whole Spritzer API.
$curl 'http://search.twitter.com/search.json?q=ladygaga&rpp=10'
is the simplest twitter command line option ever
$curl 'http://search.twitter.com/search.json?q=ladygaga&rpp=10' > filename.ext
this "pipes" it into a previously non-existent file called filename.ext (I like to think of it as filename.txt "eating" the output).
First, you must register to be a twitter developer to gain access token, access token secret, consumer key, and consumer secret.
After that you can use OAuth to perform curl to the given REST API for search that twitter provide to us.
documentation for search API in dev twittter
Also, you could learn at my simple project on github : Integrate-Services-With-Twitter-API-cURL-PHP
Related
I am using the YouTube api v3 to retrieve a list of videos using the example at https://developers.google.com/youtube/v3/code_samples/php#retrieve_my_uploads
I open the page, the app asks for authorisation. I click the link to authorise, select my gmail account and I get the listing.
The problem is when i go back to the app even only a few seconds later, I have to authorise the app again.
I thought once the app was authorised you could exchange a token for a refresh token.
Is there anywhere that shows some code how to get a refresh token as the documentation or any reference to it online is very poor.
I really need some help getting this working as i've been trying for the last couple weeks and getting nowhere.
I have a long-winded answer, but it should be a big help. I had the same issue with the poor documentation and I am also trying to get a list of videos from a playlist for my project. The API v3 just magically started working for me after struggling for the past couple days, here is what I did.
First was actually getting the API key. I'm sure you've been in the Google Developers Console by now, but just in case here is what to do for this step:
Create a project in Google Developers Console
Enable the YouTube Data API v3 API
Under "Credentials", if you had the same issue as me, you can create a new Client ID but not an API key.
This is a major problem because you need an API key to make simple data requests without authorization, e.g., getting a list of videos from a playlist. To get an API key, go back to the "APIs" section, click on "YouTube Data API v3", and here is the screen with the loading indicator that never loads anything. However you can click on "Quota" here and it takes you to the older version of the Developers Console.
From this older version, you can go to "API Access" and add your Simple API Access keys (the API keys work on this older version, but not on the new version of Developers Console). I could be wrong about this but I think "Create new Server key..." is for server-side languages like PHP, and "Create new Browser key..." is for client-side languages like Javascript. There are also buttons for Android and iOS keys, but I assume you don't need those. Anyway, I'm using cURL in PHP and the Server key worked for me, I have it set to allow any referrers while my project is still in development.
Once you get your Key for server apps under Simple API Access, it might be a while before any requests using the key actually work. For me, my script didn't work for about 4-5 hours (I think Google is having real issues with their servers this week, which is also why I had to "trick" it into giving me an API key. And it's probably why the "Credentials" page doesn't load anything).
Now use the tool on https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlists.list to help create your GET request. Enter "snippet" in the part field and an integer for maxResults if necessary. Then get the ID of a playlist from the URL on Youtube. For example, Conan's "Clueless Gamer" series is https://www.youtube.com/playlist?list=PLVL8S3lUHf0RqD7TZ6hohWk8Sd3asaqnY, so the ID is PLVL8S3lUHf0RqD7TZ6hohWk8Sd3asaqnY. Then click Execute.
Now it will give you the GET Request, something like
GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=PLVL8S3lUHf0RqD7TZ6hohWk8Sd3asaqnY&maxResults=20&key={YOUR_API_KEY}
X-JavaScript-User-Agent: Google APIs Explorer
Just take the URL after the word GET and replace {YOUR_API_KEY} with your API key.
Now you can use this URL in a cURL request, like so, where $request_url is the URL from above with your API key in it:
//http://codular.com/curl-with-php
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $request_url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
Now $resp will hold a JSON string that you can parse through and get the data for that playlist's videos. I'll leave the JSON parsing up to you, but that's how you get data similar to the old gdata way in v2. And no authentication :)
It might report an error back that your API key isn't authorized, in which case you'll need to wait several hours. Again, I think Google has been having real server issues lately so be patient; it's not your script that doesn't work, it's Google ;)
I will note that
$resp = file_get_contents($request_url);
seems to work as well, but I honestly don't know which method is "better" between cURL and file_get_contents().
Took me ages to work this out as I couldn't find any examples on how to do this easily.
It turned out that I need to create a POST request to get an access code and refresh token and this was achieved with using Curlfrom the command line
url --data "code=AUHROISATON_CODE&client_id=CLIENT_ID.apps.googleusercontent.com&client_secret=CLINET_SECRET&redirect_uri=http://www.example.com&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
After a few errors such as 'Missing grant_type', 'Invalid code' it now works.
I revoked access to the app, ran the following GET request (taken from https://developers.google.com/accounts/docs/OAuth2WebServer#formingtheurl) to get the code and was able to use the code in the above Curl statement
https://accounts.google.com/o/oauth2/auth?
scope=https://www.googleapis.com/auth/youtube&
state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&
redirect_uri=https%3A%2F%2Foauth2-login-demo.appspot.com%2Fcode&,
response_type=code&
client_id=812741506391.apps.googleusercontent.com&
approval_prompt=force
I am trying to figure out how to implement the example located on this page:
http://www.docusign.com/p/RESTAPIGuide/RESTAPIGuide.htm
Using PHP and cURL. I'm having a lot of trouble... no matter what I try, it will not work. Can someone please provide a specific PHP cURL example of how to create an envelope from a document via PHP using DocuSign's REST endpoint?
Thank you!
Have you seen the code walkthroughs? Check out the PHP one that uses curl right there:
http://iodocs.docusign.com/APIWalkthrough/requestSignatureFromDocument
The Quick Start section of the DocuSign Dev Center contains some useful resources for this. The first call you make on a new dev account is the Login API call - which retrieves your account's baseUrl and accountId. With those you can then start making signature requests and any other subsequent API calls.
The Quick Start - Your First API Call section has a full cURL command line example that shows you how to make the Login call. It looks like this:
curl --request GET 'https://demo.docusign.net/restapi/v2/login_information'
--header 'Content-Type:application/json'
--header 'Accept:application/json'
--header 'X-DocuSign-Authentication:string'
And the next page of the Quick Start section, titled Request A Signature has a PHP code Gist that uses curl to demonstrate a signature request on a document:
Request A Signature Tutorial
The top of the page describes the request you will be making and if you scroll down you'll see the code gist further below.
The other answer from Mike that references the API Code Walkthroughs is also a great resource:
DocuSign API Tools Page
I have to convert long urls to short urls on server side using bit.ly APIs. I was thinking of using some existing PHP libraries to do so like : https://github.com/tijsverkoyen/Bitly/blob/master/bitly.php
This API requires login and api key to shorten urls. But after reading the bit.ly API docs, they suggest to use oauth access token to make calls instead of API key/login. Whats the right way to do this?
Using OAuth and an access token is the right way, given the choice. There are at least two other Bitly PHP clients that support OAuth:
https://github.com/Falicon/BitlyPHP
https://github.com/jsocol/bitly-api-php
(The latter is mine, and only support OAuth.)
So I'm trying to get the latest tweets posted by a user using a PHP script (and caching the results into a text file). I'm using the request https://api.twitter.com/1/statuses/user_timeline.json?count=5&screen_name=google and it's been working fine as a public client, but in order to get better control over my API hit limit I need to authenticate my requests.
I've tried the method shown at https://dev.twitter.com/docs/auth/application-only-auth , but after I successfully connect I get the error "Your credentials do not allow access to this resource" (why ?). So I've tried to use the OAuth method (which looks frighteningly complicated for a hobbyist like me), and downloaded the TwitterOAuth library. But I'm still having trouble connecting ! I put my credentials in the config.php file, but I have no idea where to go from there. The documentation seems to be hinting that I need to manually login to Twitter to authorize the application's connection token... or something. Apparently I need to redirect to some callback URL (what even ?!) and get a short-term connection to enable a long-term connection password which I have to store for later ? What is this madness ?
I was initially under the impression that I could just fetch data from Twitter's RSS feeds but those can't be accessed from scripts for some reason (unlike the Google News RSS feed which fetches just fine). It seems I've gotten myself into something much more complicated than what I signed for. Isn't there an easier (and saner) way of doing this ? Or is nothing decidedly simple ?
I apologize for being such an easily-confused dullard, but my head is spinning.
Thanks !
Edit : after digging around some more I decided to just use PHP curl to fetch the raw page, and then do some Xpath voodoo to get the tweets and the time they were posted at. Of course, this is CPU-intensive, far from stable, and probably not a practice that pleases the folks at Twitter; it also only returns the last 20 tweets (which is thankfully enough for my needs).
However, Cormac Driver's response below about Temboo is certainly a method I'll be investigating next time I need to do something like that.
Temboo provides a simple way of using OAuth to authenticate with Twitter. The process is broken down into two steps:
InitializeOauth. This step returns a Twitter authorization URL that Twitter account holders can visit to grant access to your script.
FinalizeOauth. This step returns the access token that your script will need to make authenticated requests to the Twitter API on behalf of the user.
Full details on how to do this with PHP here: https://www.temboo.com/library/Library/Twitter/OAuth/
You can see an example of how Temboo handles OAuth for Facebook on this page. The PHP source code is provided, and it's almost identical to the code you'll need to do the same thing for Twitter.
(Full disclosure: I work at Temboo)
The Epi Twitter API files here do all but allow for the oauth_callback URL to be set. Does anyone know the code to add this?
There are plenty of tutorials out there that can show you how to integrate EpiTwitter into your code. What you want to do is pass
"oauth_callback" => "http://www.callbackurl.com"
to the HttpRequest() function as a parameter. I suggest looking through the EpiOAuth.php file and see if you can find where the parameters are passed (hint: look in the function arguments for $params).
Please refer this link or I suggest you to use the Abraham twitter oauth class
Otherwise do following steps:
Write your consumer key and consumer secret in secret.php
Write your Registered OAuth Callback URL in your twitter application (if testing on localhost then write http://127.0.0.1/yourfoldername )
Then run start.php and see the response