Get statuses/show/ from twitter Api using OAuth call - twitter

I am currently using this simple function to get data from twitter API but my rate limit is 150 as i am not using OAuth:
public function getTweetData($idTweet){
$url = 'https://api.twitter.com/1/statuses/show/'.$idTweet.'.json';
$json = file_get_contents( $url );
return json_decode($json, true);
}
I was wondering how can i authenticate my app to get the 350 limit rate.
I am currently using CakePHP framework and i am using this plugin called "CakePHP-2.x-Twitter-Plugin" to work with the twitter API:
https://github.com/mishudark/CakePHP-2.x-Twitter-Plugin
They say that "This plugin does not support the "Twitter for Websites" Features (https://dev.twitter.com/docs/twitter-for-websites) like follow buttons or direct authentication."
So, is what i am looking for, a direct authentication?
If so, i wouldn't be able to work with this plugin, so, which library would you recommend me for PHP to make this auth call?
Thanks.

Im not sure how it would work with your plugin, but I downloaded the oAuth.zip from here. This answer also shows the code to authenticate each time. His $content = $connection shows how to get twitter to do things like update etc

Related

Searching through a Twitter feed for an Alexa Skill

I'm working on a fairly basic Alexa skill that, in essence, searches through a specific Twitter feed looking for a hashtag, parses that tweet, and reads it back. What's the simplest way to pull data from a Twitter feed? I've been having issues working with the Twitter API (see below) and scraping data from Twitter appears to be against the TOS.
... crawling the Services is permissible if done in accordance with the provisions of the robots.txt file, however, scraping the Services without the prior consent of Twitter is expressly prohibited.
To write an Alexa skill
Follow a tutorial.
Simple enough. Use a pretty interface to add an invocation, some intents, and a slot type with the data I want.
Write an AWS Lambda function to handle everything.
Python because I know it better than JavaScript. Pick one of the Python wrappers for the Twitter API, then realize that because Twitter, all of their API requires authentication - even basic searches.
Register an app with Twitter so I have keys and tokens.
Realize having authentication keys and tokens in plaintext in an application is a bad idea, and decide to figure out account linking for Alexa.
Try following the one tutorial around, twice. Have trouble and go to StackExchange.
Why the need for an external webapp?
...we need an OAuth implementation of our own to make the integration work correctly
What's wrong with the one provided by Twitter? Why can't any issues be fixed within the Lambda method, since the account integration isn't being touched otherwise AFAIK? Isn't having the tokens passed around via the URL a bad idea too? Their example code seems to require that the Consumer Secret be hard coded too.
Enter: “https://alexa-twitter-airport-info.herokuapp.com/oauth/request_token?vendor_id=XXXXXX&consumer_key=YYYYYY&consumer_secret=ZZZZZZ”.
At the very least, their webapp seems to be down for the time being, and it'd be nice to have an option that doesn't require paying money to host another copy.
I've seen this post discussing a Node.js OAuth implementation, but the necessity for such an implementation still escapes me.
I found twitter Node package useful doing this. I used search/tweets GET API for my purpose which is shown below. However you have various APIs which are available to work with the tweets. The code below only shows the parts needed to access Twitter API from within Node JS.
var Twitter = require('twitter');
var client = new Twitter({
// I added the keys as AWS Environment variables
consumer_key: process.env.consumer_key,
consumer_secret: process.env.consumer_secret,
access_token_key: process.env.access_token_key,
access_token_secret: process.env.access_token_secret
});
client.get('search/tweets', {q: <<your search query>>,count:<<if you want to receive specific number of tweets>>}, function(error, tweets, response) {
var noOfTweets = tweets.statuses.length;
if(error) {
speechOutput = "I could not find tweets for <<your reason>>"
self.emit(':tell', speechOutput);
} else if(noOfTweets === 0) {
speechOutput = "another speech output";
self.emit(':tell', speechOutput);
} else {
tweets.statuses.forEach(function(tweet) {
//process the tweet the way you want to.
});
}
});

Twitter api request

I have read the documentation of twitter , and created an app and have all the keys needed .
Now i am trying to understand that simple one line http request ,to get a user latest twits .
I have read this Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
but there is not one line code in there to make the request ( i don't know java script).
so , i have this :
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
Which will not work because my keys should be in this line, but i don't understand how to add them?? where and how i add my keys to this ?
This url is like a node or address so twitter server knows what kind of request you need.
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
Beside url you can pass many other parameter using http header, which is best to be used with server langunge programming, e.g PHP.
In PHP you can pass your credentials by changing header. While current twitter API are using Oauth, it's going to difficult to learn using Oauth this way, the easiest option is to use others' library. Check out 'twitter API library' (just google it)

How to use Twitter User Streams

I am new in Twitter API. I have a requirement like I have to pull the information of an authenticated twitter user to know the number of followers. I can do this using REST API but I have a problem here. So I have taken the way to use Twitter User Streams.
I am not able to integrate this functionality in the site. I am using twitter console( https://dev.twitter.com/console ). But there I am not able to get any kind of result, it actually hangs/shows gateway timeout. And that's for I am not able to configure that how to write the code for this or how to use this in site. I am not passing any kind of parameters.
So, any body can please help in this would be very grateful( a clean sample code for this or any link where to get the good documentation ). I have already checked with twitter dev documentations.
Thanks in advance :)

Get tweets for a specific user V1.1

I'm not really sure where to begin with this.
Prior to the upgrade to version 1.1 of the API I was able to request tweets in XML format using
$url = 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=' . $TWITTER_USERNAME;
A couple of things have changed, the return format is now JSON which isn't a problem but I'm not sure how to authenticate my app to request the tweets in JSON format.
I've read this link but am unsure how I'm supposed to implement it.
For instance, where do I get the consumer secret from ?
How do I request a bearer token in PHP ?
Any pointers would be great
you need to use plugin that is written for v1.1, In new version they have added security layer so you need to be authorized in order to get the tweets via API, here are the links that will be helpful for you
http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/ http://www.fullondesign.co.uk/coding/2516-how-use-twitter-oauth-1-1-javascriptjquery.htm

PHP to retrieve latest tweet

I am currently using the following code to retrieve the latest tweet from my Twitter feed:
<?php
$username='myUsername';
$format = 'json';
$tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
$latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
$latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', 'http://$1', $latestTweet);
$latestTweet = preg_replace('/#([a-z0-9_]+)/i', '#$1', $latestTweet);
echo '<p class="inset white">'.$latestTweet.'</p>';
?>
On MAMP, this works perfectly, however on my live site, I get nothing, not even a server side error message. I read on a similar post that this may be something to do with allow_url_fopen = On not being set in php.ini, but setting that appears to have made no difference.
Not withstanding the fact that Twitter may have changed their policy on usage of the API (which I don't believe they have), then I'm lost as to what could be the problem.
I'm using the same approach before, but unfortunately, it's using Twitter API v1 and v1 is no longer available (check https://dev.twitter.com/blog/api-v1-retirement-final-dates for more details). You need to have the OAuth consumer and user access now. GitHub themattharris' tmhOAuth library is handy for displaying Twitter feed.
You may check the question I've posted here at stackoverflow as a reference to your problem:
Display latest Twitter Feed "date created" using tmhOAuth and PHP
It already have the codes how to access the common information needed to be retrieved in Twitter.

Resources