How do I grab - or more specifically, iterate over - the contents of a JSON user_timeline object in PHP? I have it retrieved, I'm just trying to access the fields in the object. All of the other tutorials/answers don't apply to the new 1.1 API. Here's where I am:
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=testyMcTesterton';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
If you posted the contents of the object itself it would help, but can you not just json_decode($obj)? From that do something like:
$new = json_decode($obj);
$data = $new->data; // (this would access Object->data->*)
That's how a JSON array is accessed, though I don't actually know how the Twitter user_timeline object looks so I can't tell you exactly how to access the elements you want specifically.
Related
I'm trying to display tweets for a specific hashtag. The code below works perfectly what ever the hashtag is, but when i try with the specific one, it doesn't work. For the last month, it worked perfectly with the right hashtag but since yesterday it's broken. (i didn't put my tokens in the code)
require_once('twitter-api-php-master/TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "my_tokens",
'oauth_access_token_secret' => "my_tokens",
'consumer_key' => "my_tokens",
'consumer_secret' => "my_tokens"
);
$hashtag = 'smartspend_eu';
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = "?q=#$hashtag";
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
The response is :
{"statuses":[],"search_metadata":{"completed_in":0.053,"max_id":1105019731759656961,"max_id_str":"1105019731759656961","query":"%23SMARTSPEND_EU","refresh_url":"?since_id=1105019731759656961&q=%23SMARTSPEND_EU&include_entities=1","count":15,"since_id":0,"since_id_str":"0"}}
What is the problem, and what can i do to fix it ?
I finaly found what's wrong. My code was right from the begining. The problem is that it's impossible to get the tweets from more than 7 days ago. And no tweets has been written for my hashtag for the last 7 days.
How do you decode the tweets.json string returned from a twitter search API request?
I have looked through answers to similar questions. Those answers do show how to make a call, and how to display the data returned, but those don't deal with the issue of dealing with the structure of the data that is returned from the tweets.json API call.
Here's the code - it uses the twitter API. It requests search results.
<?php
require_once('../TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "......",
'oauth_access_token_secret' => "......",
'consumer_key' => "......",
'consumer_secret' => "......"
);
$requestMethod = 'GET';
//$url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; // I can decode output from this
//$getfield = "?screen_name=J7mbo&count=5"; // I can decode output from this
$url = "https://api.twitter.com/1.1/search/tweets.json"; // I can NOT decode output from this
$getfield = "?q=%23J7mbo&result_type=recent"; // I can NOT decode output from this
$twitter = new TwitterAPIExchange($settings);
$string = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(); // from stackOverflow
$string = json_decode($string, $assoc = TRUE); // seems i cannot use json_decode for output from tweets.json
if ($string["errors"][0]["message"] != "")
{
echo "twitter error message:" . $string[errors][0]["message"];
exit();
}
foreach ($string as $items)
{
echo "tweet text =[". $items['text']."]<br />";
}
?>
If I was using a twitter API timeline call, I could use json_decode and access $items['text'] for each of the returned tweets
But I want to use the twitter API search call (tweets.json). json_decode does not properly decode the data from this search call, it only returns two empty $items['text']
So what's the best way to decode the tweets.json string returned from a twitter API request?
You need to iterate through the $items array and get the text property off of there.
foreach ($items as $item) {
echo "tweet text =[". $item['text']."]<br />";
}
After examining the data I noticed that is consisted of two JSON encoded strings, named statuses and search_metadata. I extracted the statuses string and was able to decode it using json_decode.
I've tried almost every solution I could find - nothing is working. Basically, the status gets updated but the media_ids parameter gets ignored.
I'm using the TwitterAPIExchange.php library. Here is my code:
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$postfields = array(
"media_ids" => 586556953049956353,
"status" => "So we keep truly! testing!"
);
$requestMethod = 'POST';
echo $twitter->setPostfields($postfields)
->buildOauth($url, $requestMethod)
->performRequest();
I am using the Twitter Search API 1.1 to search for tweets on a particular keyword and display the results as a search results page at motherpipe.co.uk.
I have managed to get the Oauth working with the help of James Mallison at http://github.com/j7mbo/twitter-api-php.
I am stuck at the stage of taking the actual response I get from Twitter and display it normally in divs in HTML (using PHP). This is the reply I get: https://motherpipe.co.uk/staging/index2.php.
I just can't seem to write a simple loop to display only the 'screen_name' and 'text'.
So far I have tried different versions of this:
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=sweden&result_type=recent';
$twitter = new TwitterAPIExchange($settings);
echo $twitter ->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($twitter);
foreach($response as $tweet)
{
echo "{$tweet->screen_name} {$tweet->text}\n";
}
Any feedback or ideas on how to loop through this correctly would be much appreciated.
Cheers
I dont use james code but I get it working other way round
<ul>
<?php $get_tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
foreach($get_tweets as $tweet) {
$tweet_desc = $tweet->text;
?>
<li><?php echo $tweet_desc ?></li>
<?php }?>
</ul>
You can see a working Twitter Api V1.1 at the footer of this page. If you want something like this or further customisation let me know .
If you would do a var_dump of $response, you would probably get a better idea of the structure. Looks like you need to loop over $response->statuses. The user information is under $tweet->user->screen_name and the tweet text is under $tweet->text. So:
$response = json_decode($twitter);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}\n";
}
The user information is under $tweet->user->screen_name and the tweet text is under $tweet->text
$response = json_decode($twitter);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}<br />";
}
OR also you can use this too
$response = json_decode($twitter, true);
$counter = 0;
foreach($response['statuses'] as $tweet)
{
echo "{$tweet[$counter]['user']['screen_name'] {$tweet[$counter]['text']}<br />";
$counter +=1;
}
So the answer (thanks to Prisoner, Jonathan Kuhn and Mubin Khalid) is, in order to display the response from the Twitter Search API, assign a variable to the Twitter JSON response. This will enable you to loop through and display whatever you want from the response.
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=sweden&result_type=recent';
$twitter = new TwitterAPIExchange($settings);
$api_response = $twitter ->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($api_response);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}\n";
}
using below code, TWITTER API and PHP, i have successfully fetch/get my twitter timeline.
$url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
$getfield = '?screen_name=twitter';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
my concern is, the data/results displayed fills up my whole window screen with text. im not sure how to apply CSS or HTML on it.
[{"created_at":"Sun Dec 09 05:21:29 +0000 2012","id":2232376342522313442,"id_str":"277231523253442","text":"http:\/\/t.co\/ro74jh4","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"
the tutorial im following didn't mentioned how to arrange the fetched data.
You'll need to parse the JSON data that you got from object from Twitter. Here is an example. You can use the array syntax to find which values you want to look up.
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$obj = json_decode($response, true);
echo "<div><ul>";
foreach ($obj["statuses"] as $index => $result) {
$tweet = $result['text'];
$user = $result['user']['screen_name'];
$profile_image = $result['user']['profile_image_url'];
echo "<li>";
echo "<img src=\"".$profile_image."\" width=\"25px\" height=\"25px\" />";
echo "#$user";
echo " $tweet";
echo "</li>";
}
echo "</ul></div>";