I have created a simple PHP Twitter request that caches the response every 10 minutes. The request code looks like:
$twitter_result = false;
if (file_exists( 'twitter.json' )) {
$data = json_decode( file_get_contents( 'twitter.json' ));
if ($data->timestamp > (time() - 10 * 60) ) {
$twitter_result = $data->twitter_result;
}
}
if (!$twitter_result) {
$twitter_result = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?q=@SolidCAMUK&rpp=1&screen_name=SolidCAMUK&count=1');
$data = array ('twitter_result' => $twitter_result, 'timestamp' => time());
if(file_put_contents( 'twitter.json', json_encode($data) )) {
//echo 'success';
} else {
//echo 'error';
}
}
$file = file_get_contents('twitter.json');
header("content-type:application/json");
if($_GET['callback']) {
echo $_GET['callback'] . '(' . $file . ')';
} else {
echo $file;
}
exit;
And an example of the returned JSONP for this page looks like: http://dev.driz.co.uk/phptwitter/?callback=Test
And then I'm trying to use this JSONP in my test scenario here: http://dev.driz.co.uk/phptwitter/test.php
However the data isn't being displayed properly. I'm guessing that the JSON isn't formatted correctly, as when I console.log the response, it seems to be acting as a string rather than an actual object... Can anyone see any issues?