1

I have the following PHP code in my app that gets and stores a single tweet in a local json file that I can use to pull tweets onto my webpage without having to keep hammering the Twitter API.

if (file_exists( get_site_url() . '/twitter.json' )) {
    $data = json_decode( file_get_contents( get_site_url() . '/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=@Twitter&rpp=1&screen_name=Twitter&count=1');

   $data = array ('twitter_result' => $twitter_result, 'timestamp' => time());
   file_put_contents( get_site_url() . '/twitter.json', json_encode($data));
}

The code was previously using serialize and unserizalize instead of json_encode and json_decode and was changed because I want the saved data as pure JSON.

However since changing it, the data is no longer saved to the file! I'd presumed because the line where I'm checking the timestamp is incorrect but it's not actually even saving the data if I comment out the top if statement.

Perhaps because Twitter is already JSON? But if echo out the json_encode($data) I get the following:

{
    "twitter_result": "[{\"created_at\":\"Thu Apr 11 21:11:23 +0000 2013\",\"id\":322456869178310656,\"id_str\":\"322456869178310656\",\"text\":\"We\\u2019re bringing Trends to over 160 new locations. To learn more, check out our blog post: http:\\\/\\\/t.co\\\/2Vn2JRmHV5\",\"source\":\"\\u003ca href=\\\"http:\\\/\\\/itunes.apple.com\\\/us\\\/app\\\/twitter\\\/id409789998?mt=12\\\" rel=\\\"nofollow\\\"\\u003eTwitter for Mac\\u003c\\\/a\\u003e\",\"truncated\":false,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"user\":{\"id\":783214,\"id_str\":\"783214\",\"name\":\"Twitter\",\"screen_name\":\"twitter\",\"location\":\"San Francisco, CA\",\"url\":\"http:\\\/\\\/blog.twitter.com\\\/\",\"description\":\"Your official source for news, updates and tips from Twitter, Inc.\",\"protected\":false,\"followers_count\":17715897,\"friends_count\":120,\"listed_count\":76561,\"created_at\":\"Tue Feb 20 14:35:54 +0000 2007\",\"favourites_count\":22,\"utc_offset\":-28800,\"time_zone\":\"Pacific Time (US & Canada)\",\"geo_enabled\":true,\"verified\":true,\"statuses_count\":1571,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"ACDED6\",\"profile_background_image_url\":\"http:\\\/\\\/a0.twimg.com\\\/profile_background_images\\\/657090062\\\/l1uqey5sy82r9ijhke1i.png\",\"profile_background_image_url_https\":\"https:\\\/\\\/si0.twimg.com\\\/profile_background_images\\\/657090062\\\/l1uqey5sy82r9ijhke1i.png\",\"profile_background_tile\":true,\"profile_image_url\":\"http:\\\/\\\/a0.twimg.com\\\/profile_images\\\/2284174758\\\/v65oai7fxn47qv9nectx_normal.png\",\"profile_image_url_https\":\"https:\\\/\\\/si0.twimg.com\\\/profile_images\\\/2284174758\\\/v65oai7fxn47qv9nectx_normal.png\",\"profile_banner_url\":\"https:\\\/\\\/si0.twimg.com\\\/profile_banners\\\/783214\\\/1347405327\",\"profile_link_color\":\"038543\",\"profile_sidebar_border_color\":\"EEEEEE\",\"profile_sidebar_fill_color\":\"F6F6F6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"default_profile\":false,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null},\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"retweet_count\":134,\"favorite_count\":63,\"favorited\":false,\"retweeted\":false,\"possibly_sensitive\":false,\"lang\":\"en\"}]",
    "timestamp": 1365717135
}

Which looks like valid JSON to me...

Cant't see any problems with the writing part as it was working before. BUT doing this:

if(file_put_contents( get_site_url() . '/twitter.json', json_encode($data) )){
        echo 'success';
    }
    else
    {
        echo 'error';
    }

And I get the error echo statement. not sure why? the location is correct and the file SHOULD be writable and exists.

However doing this:

$file = file_get_contents( get_site_url() . '/twitter.json');
echo is_writable($file) ? "is writable<br>" : "not writable<br>";

I get the error 'not writable', but the permissions are set to 777

What could be causing this?

1 Answer 1

1

Try the following (updated):

<?php

$twitter_result = false;
$file = 'path/to/file/twitter.json';

if (file_exists( $file )) {
    $data = json_decode( file_get_contents( $file ) );
    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=@Twitter&rpp=1&screen_name=Twitter&count=1');

   $data = array ('twitter_result' => $twitter_result, 'timestamp' => time());
   file_put_contents( $file, json_encode($data));
}

?>
Sign up to request clarification or add additional context in comments.

3 Comments

Still not writing to the file... No idea why either :/ Thanks for cleaning up the first if statement though :) just need to figure out why it's not writing.
It worked for me. Perhaps you don't have enough permissions to write to file?
is_writable() expects a filename (string), not the content itself. In addition, I don't think that file_put_contents can work with external URL (in my testing, I deleted get_site_url()). Why don't you use relative path to file instead? I will update code now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.