JSON Twitter feed & caching error

I've recently wrote a little script that get's my Twitter feed and caches it to save on HTTP requests. Without WordPress it works great, as soon as I add it to WordPress everything goes wrong, which makes sense because file_get_contents doesn't particularly work correctly in WordPress. I tried using: wp_remote_get() instead but I just get the following error:

Fatal error: Cannot use object of type WP_Error as array in

which is this line:

$tweets = json_decode($contents['body']);

Code without adding to WordPress: (Which works fine)

$file = "tweets.txt";
if (@file_exists($file) and @filemtime($file)>=strtotime("-10 minutes")) {
    $tweets = json_decode(file_get_contents($file));
} else {
    $tweets = file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=jack&include_rts=true");
    $fh = fopen($file, 'w');
    fwrite($fh, $tweets);
    fclose($fh);
    $tweets = json_decode($tweets);
}
$i = 0;
foreach($tweets as $tweet) {
    $tweetText = preg_replace('#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#','<a href="$1">$1</a>', $tweet->text);
    $tweetText = preg_replace('/(^|\s)@([a-z0-9_]+)/i','$1<a href="http://www.twitter.com/$2">@$2</a>', $tweetText);
    $tweetText .= " - " . date('G:i F jS',strtotime($tweet->created_at));
    echo '<p>'.$tweetText.'</p>';
    ++$i;
    if ($i==3) {break;}
}   

Code when adding to WordPress:

$file = "tweets.txt";
if (@file_exists($file) and @filemtime($file)>=strtotime("-10 minutes")) {
    $contents = wp_remote_get($file);
    $tweets = json_decode($contents['body']);
} else {
    $tweets = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=jack&include_rts=true");
    $fh = fopen($file, 'w');
    fwrite($fh, $tweets);
    fclose($fh);
    $tweets = json_decode($contents['body']);
}
$i = 0;
foreach($tweets as $tweet) {
    $tweetText = preg_replace('#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#','<a href="$1">$1</a>', $tweet->text);
    $tweetText = preg_replace('/(^|\s)@([a-z0-9_]+)/i','$1<a href="http://www.twitter.com/$2">@$2</a>', $tweetText);
    $tweetText .= " - " . date('G:i F jS',strtotime($tweet->created_at));
    echo '<p>'.$tweetText.'</p>';
    ++$i;
    if ($i==3) {break;}
}   

I'm quite new to using HTTP requests like JSON. I'm sorry if I left anything out and/or this becomes a really easy solution. Thanks.

Answers


From the codex it says that wp_remote_get() returns results as an array

http://codex.wordpress.org/Function_API/wp_remote_get

As you can see in this answer - json_decode to array, you need to specify in json_decode() that it's an array rather than an object.

So I'm thinking it should be;

$tweets = json_decode($contents['body'], true);

Need Your Help

How to use my own classes as Core Data entities ? [Xcode]

objective-c oop core-data user-interface xcode4

I'm quite surprised that I haven't been able to find the answer using Google so I hope someone will be able to help !

Get accurate time from android/iphone to server

android iphone gps timestamp

We have an android(or iphone) client we are developing. The client allows the android user to send entries to a server which we also develop. If the client does not have data services (GPRS) at the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.