I am working with Twitter API 1.1 and I am fetching tweets of a user.
I am using this code
<?php
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$cursor = isset($cursor) ? $cursor : "-1";
$getfield = "?cursor=" . $cursor . "&user_id=" . $user . "&count=10";
$twitter = new TwitterAPIExchange($settings);
$string = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($string, 1);
echo '<pre>';print_r($string);echo '</pre>';
?>
Is there way to recieve since_id after a particualr request so that I can requeue my function for the next request starting from the last recieved id?
For Example: In a single request I am fetching 0-1000 records, On my next request I want start from 1001th record.
Is it possible in twitter?
If you made a request to a timeline without using since_id, and the most recent tweet you got back had the ID "123" then, after an interval, you can use that most recent tweet ID "123" as your since_id value to user_timeline. You would then get (up to 200) of the next most recent tweets posted by that user "since" they posted tweet "123".
Related
Below code gives all tweets in array format for particular twitter account / user
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "your twitter account access_token",
'oauth_access_token_secret' => "your twitter access_token_secret",
'consumer_key' => "your twitter consumer_key",
'consumer_secret' => "your twitter consumer_secret"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = 'GET';
$getfield = '?q=+from:test_account';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") { echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
echo "<pre>";
print_r($string);
echo "</pre>";.
But I want to do following 2 things.
Whenever I click / open any twitter account page on my website, it should give real time tweets for that twitter account. Means whenever new tweet(s) for that account are posted, they should be displayed immediately on my website.
Currently I need to generate oAuth data (access token, access token secret, consumer key, consumer secret) for each twitter account. So is there any way that I can fetch tweets of particular twitter account and display them on my website without generating above 4 mentioned values?
Thank you,
First, you don't have to make new app for your user.
Second, the url you are using is just to access you timeline so that you can retrieve tweets from your account. Only for searching from user's tweets, you will need their screen name like #name.
try this,
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=from:test_account';
$twitter = new TwitterAPIExchange($settings);
$tweet = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($tweet,1);
As suggested in documents, here
https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
I am trying to retrieve Twitter Timeline statuses using API 1.1:
$url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
$getfield = '?count=10';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$tweets = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
I expect when I receive the response and use json_decode($tweets, true);, size of result be 10. But it is 9!
Is there any reason for this?
So i am trying to get that tweet id of a retweeted id and since twitter recently allows you to retweet your own tweet, when i try to get the retweeted tweet's id of my own tweet, it gives me two variables when i use var_dump i want only one of it how do i extract?
i used
var_dump(tweet->id);
in the code and when i execute it, it shows like int(12381239) int(29384121)
i want to take the first int variable that is 12381239 and store it in a variable, i tried all i can but failed can anyone help me?
UPDATE : Here is the code
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "xxxxxx-asdsaldjasd",
'oauth_access_token_secret' => "xxxxxx",
'consumer_key' => "xxxxxxx",
'consumer_secret' => "xxxxxxxxxx"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$getfield = '?screen_name=username';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($response);
foreach($tweets as $tweet)
{
if ($tweet->retweeted) // here is the part where i did var_dump
{
$url = "https://api.twitter.com/1.1/statuses/destroy/$tweet->id.json";
$requestMethod = 'POST';
$postfields = array(
'id' => '$tweet->id'
);
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
$response;
}
}
what i'm trying to do is undo the retweets, which was working fine until twitter let the accs rt themselves.
I'm using TwitterOAuth lib to query for tweets and for people who retweeted the tweets.
The first query (get the tweets itself) works like a charm, but the second query (get the people who retweeted each tweet) is always returning Sorry, that page does not exist, error 34.
This is how I'm calling the lib:
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
return $twitter->get('statuses/retweets/' . $id . '.json', $query);
Why is Twitter's API returning this error?
TwitterOAuth adds the '.json' bit on for you. Try:
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
return $twitter->get('statuses/retweets/' . $id, $query);
How to get my followers count number with PHP.
I found this answer here: Twitter follower count number, but it is not working because API 1.0 is no longer active.
I have also tried with API 1.1 using this URL: https://api.twitter.com/1.1/users/lookup.json?screen_name=google but is is showing an error(Bad Authentication data).
Here is my code:
$data = json_decode(file_get_contents('http://api.twitter.com/1.1/users/lookup.json?screen_name=google'), true);
echo $data[0]['followers_count'];
If do it without auth (replace 'stackoverflow' with your user)
$.ajax({
url: "https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow"
dataType : 'jsonp',
crossDomain : true
}).done(function(data) {
console.log(data[0]['followers_count']);
});
with php
$tw_username = 'stackoverflow';
$data = file_get_contents('https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names='.$tw_username);
$parsed = json_decode($data,true);
$tw_followers = $parsed[0]['followers_count'];
Twitter API 1.0 is deprecated and is no longer active. With the REST 1.1 API, you need oAuth authentication to retrieve data from Twitter.
Use this instead:
<?php
require_once('TwitterAPIExchange.php'); //get it from https://github.com/J7mbo/twitter-api-php
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=REPLACE_ME';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count=$data[0]['user']['followers_count'];
echo $followers_count;
?>
Parsing the XML might be easier in some cases.
Here's a solution (tested):
<?php
$xml = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml->followers_count;
?>
Hope this helps!