2013-04-11 109 views
1

我的应用程序中有以下PHP代码,它可以在本地json文件中获取和存储单个推文,我可以使用它将推文推送到我的网页上,而无需继续敲击Twitter API。PHP不写数据到JSON文件

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/[email protected]&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)); 
} 

的代码是以前使用serializeunserizalize代替json_encodejson_decode和改变,因为我要保存的数据为纯JSON。

但是,由于更改它,数据不再保存到文件!我推测是因为我检查时间戳的行不正确,但如果我注释掉顶部if语句,它实际上并不保存数据。

也许是因为Twitter已经是JSON了?但是,如果回声出json_encode($数据)我得到以下几点:

{ 
    "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 
} 

它看起来像有效的JSON我...

看不清任何问题与写入的部分,因为它是工作之前。但是这样做:

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

而且我得到错误回声声明。不知道为什么?该位置是正确的,该文件应该是可写的和存在的。

但是这样做:

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

我得到的错误“不可写”,但权限设置为777

可能是什么造成的?

回答

1

尝试以下方法(更新):

<?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/[email protected]&rpp=1&screen_name=Twitter&count=1'); 

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

?> 
+0

还没写入文件......不知道为什么两种:/感谢清理第一个if语句虽然:)只需要弄清楚它为什么不写。 – Cameron 2013-04-11 22:14:18

+0

它为我工作。也许你没有足够的权限来写入文件? – 2013-04-11 22:47:27

+0

'is_writable()'需要一个文件名(字符串),而不是内容本身。另外,我不认为file_put_contents可以使用外部URL(在我的测试中,我删除了'get_site_url()')。为什么不用相对路径来替代文件呢?我现在会更新代码。 – 2013-04-11 23:12:44