GET statuses/home_timeline Twitter API - php

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?

Related

Twitter API 1.1 error 215 upload on Altervista

I'm using the twitter API v1.1 and I'm trying to make a simple query on twitter (the list of followers of a user).
I've searched a lot on internet and I found many answers to this problem but I don't know what is my mistake.
I've look this thread on stack overflow Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
and this is the code I wrote on my php file and upload it on Altervista.
Of course on Altervista I have my TwitterAPIExchange.php file and the twitteroauth directory set properly. I try to run it but I got this message:
{"errors":[{"code":215,"message":"Bad Authentication data."}]}
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
$consumer_key = "*****",
$consumer_secret = "*****",
$access_token = "*****",
$access_token_secret = "*****"
);
$url = 'https://api.twitter.com/1.1/followers/list.json';
$getfield = '?username=J7mbo&skip_status=1';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
?>

var_dump(tweet->id); gives int(198231293) int(123981623) how to i retrieve one and store it on a variable?

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.

How to revoke twitter API request after rate limit exceed?

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".

How to decode twitter json with php?

Could someone help me finish script, its taking posts from my twitter page, I need now to make it take only "text": "" .
Here is json:
http://142.4.211.155/~bingsw/tw/
And here is code where I get json:
<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=MiDizajn&count=5';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
?>
If you want to get only text of the tweets try this:
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($response,true);
foreach ($tweets as $tweet) {
echo $tweet['text'];
}
php function json_decode will do it
Try:
json_decode($posts, TRUE) //it returns an associative array of values

Applying Twitter API using example but return Null

I am using twitter-api-php-master to search keywords through twitter but got Null return. Really not sure why it is like that.
My code is like:
<?php
#### Include the class file ####
require_once('TwitterAPIExchange.php');
#### Set access tokens ####
$settings = array(
'oauth_access_token' => "[something]",
'oauth_access_token_secret' => "[something]",
'consumer_key' => "[something]",
'consumer_secret' => "[something]"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=test&geocode=37.781157,-122.398720,1mi&count=100';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($response));
?>
I pretty much copy this from example from https://github.com/J7mbo/twitter-api-php/wiki/Twitter-API-PHP-Wiki. But the result is just Null. Someone knows what is the problem?

Categories