2013-04-11 313 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