Closed whatily closed 11 years ago
What exactly are you trying to achieve? You have got your data, the var_dump() is just so you can see what data you have to work with.
Usually people loop their data with a foreach() and echo out exactly what they want...
I'm trying to display 1 latest tweet from my account...
You should check out the excellent php help and perhaps stackoverflow for json_decode however this is what you roughly need to do...
$arrResults = json_decode($strJson,true); // note the true instead of your false and this creates a PHP associative array
foreach ($arrResults['statuses'] as $arrSearchResult) { // loop through each tweet
$strTweet = $arrSearchResult['text'] ; // retrieve the text of the tweet
print_r("<div class='tweet'>$strTweet</div>"); // tell the world
}
}
also consider var_dump as an excellent tool to work out what you need when commencing this...
print_r("<pre>");
$arrResults = json_decode($strJson,true);
var_dump($arrResults);
print_r("</pre>");
Hi EwenH,
I modified your code to
$arrResults = json_decode($response,true); // note the true instead of your false and this creates a PHP associative array
foreach ($arrResults['statuses'] as $arrSearchResult) { // loop through each tweet $strTweet = $arrSearchResult['text'] ; // retrieve the text of the tweet print_r("$strTweet"); // tell the world }
And here's the return:
Warning: Invalid argument supplied for foreach() in /home/cinnabar/www/www/wp-content/themes/Cinnabar/footer.php on line 42
whatily, I am searching for tweets, you are looking at your time line. It means that my array will have different content to yours. Use the var_dump that I mentioned and then re-purpose my loop to what you need.
print_r("<pre>");
$arrResults = json_decode($strJson,true);
var_dump($arrResults);
print_r("</pre>");
I'm sorry I don't quite get it, EwenH. I put in the code as this:
/** 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=CinnabarCA&count=1'; $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($settings); $response = $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(); print_r(""); $arrResults = json_decode($response,true); var_dump($arrResults); print_r(""); foreach ($arrResults['statuses'] as $arrSearchResult) { // loop through each tweet $strTweet = $arrSearchResult['text'] ; // retrieve the text of the tweet print_r("$strTweet"); // tell the world }
It returns all the information of the twitter account, as I know that's what var_dump does. And then it tells me that the loop argument is invalid.. I am only retrieving 1 tweet, so I guess there's not really a need for a loop? All I want to display is the tweet content... Sorry I'm not quite knowledgable about PHP...
Thanks!
whatily, I am not going to write the code for you. In the var_dump you will see how the array is structured and you will identify what parts of the array you need to retrieve like the text and the date created perhaps. It's all in the array and you need to study this and perhaps stackoverflow to extract the items. As a hint I need to go down two levels to see what the UTC offset is. UTC_offset is indented under "user" if you look at it in a non technical way so I reference this as...
$offset = $arrSearch['user']['utc_offset']
@whatily Unfortunately, basics like looping around objects and printing out are all over the internet - but you can always ask these things on StackOverflow :)
Hi, I finally got the print_r I need. But it turns out the tweet is all plain text. Is there a way that it prints out hyperlinks for the twitter feed? Thanks!
@whatily What do you mean? Can you give us code examples?
$twitter = new TwitterAPIExchange($settings); $response = $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(); $arrResults = json_decode($response,true); // note the true instead of your false and this creates a PHP associative array
foreach ($arrResults as $tweet) { // loop through each tweet $strTweet = $tweet['text'] ; // retrieve the text of the tweet echo $strTweet; // tell the world
So this is the code I'm using. And the website is http://cinnabar.com, at the footer. Thanks!
You're going to have to search your tweets for any t.co links and, for each one you find, wrap it in < a > tags.
I can't help you with the regular expression for that, try StackOverflow.
Hi, I am using your code and when I use var_dump(json_decode($response, false)); it returns this whole string of information. How do I solve this? Here's the site link: http://cinnabar.com
Thank you very much!
Bin