2012-07-26 13 views
3

更新:經過一番更多的挖掘,我發現該解決方案相當簡單。我所要做的就是用curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'writeCallback');替換curl_setopt($ch, CURLOPT_FILE, $fp2);WriteCallback所做的僅僅是打開要寫入數據的文件,將數據寫入文件然後關閉文件。我相信下面陳述的代碼沒有按預期工作的原因是因爲curl與twitter api建立了持久連接,因此永遠不會超過curl_close($ch)close($fp)。希望這可以幫助任何可能面臨同樣問題的人。使用curl從twitter流式API檢索數據時寫入文件的不完整數據

直到最近我才熟悉捲曲庫。我目前正在嘗試使用curl與Twitter的streaming api保持一致的連接。

這是到目前爲止我的代碼:

$fp2 = fopen('file:///Users/KareemYousrii/dump.txt', "r+"); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER,0); 
curl_setopt($ch, CURLOPT_FILE, $fp2); 
curl_setopt($ch, CURLOPT_TIMEOUT, 99999999); 
curl_exec($ch); 
curl_close($ch); 
fclose($fp); 

當我刪除了curl_setopt($ch, CURLOPT_FILE, $fp2);線並運行終端,我得到所需的響應文件。但是,如果我按照示例所示保留它,則會得到數據不一致的文本文件。這意味着,除非發生另一事件,否則關於特定事件的數據(即傾向於推文或轉發推文)沒有完全寫入文件,此時第一個事件完全寫入,而第二個事件只是部分寫入。

這是最新事件的文件內容的示例:

{ 
    "target_object": { 
    "retweeted": false, 
    "retweet_count": 0, 
    "in_reply_to_user_id": 261119681, 
    "in_reply_to_status_id": 219191541426688001, 
    "in_reply_to_status_id_str": "219191541426688001", 
    "truncated": false, 
    "user": { 
    "id": 99786716, 
    "location": "", 
    "profile_use_background_image": true, 
    "profile_text_color": "333333", 
    "following": true, 
    "verified": false, 
    "id_str": "99786716", 
    "default_profile": true, 
    "utc_offset": 7200, 
    "profile_sidebar_border_color": "C0DEED", 
    "friends_count": 231, 
    "name": "kareem ahmed", 
    "profile_background_image_url_https": "https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png", 
    "notifications": false, 
    "protected": false, 
    "listed_count": 0, 
    "profile_background_tile": false, 
    "screen_name": "KareemYousrii", 
    "contributors_enabled": false, 
    "profile_sidebar_fill_color": "DDEEF6", 
    "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/1240332836\/40753_10150118794908242_529098241_7875682_6258916_n_normal.jpg", 
    "geo_enabled": true, 
    "followers_count": 107, 
    "description": "", 
    "statuses_count": 386, 
    "is_translator": false, 
    "show_all_inline_media": true, 
    "profile_background_color": "C0DEED", 
    "url": null, 
    "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/1240332836\/40753_10150118794908242_529098241_7875682_6258916_n_normal.jpg", 
    "lang": "en", 
    "follow_request_sent": false, 
    "default_profile_image": false, 
    "created_at": "Sun Dec 27 21:29:09 +0000 2009", 
    "profile_background_image_url": "http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png", 
    "time_zone": "Istanbul", 
    "favourites_count": 11, 
    "profile_link_color": "0084B4" 
    }, 
    "favorited": false, 
    "created_at": "Sat Jun 30 22:14:54 +0000 2012", 
    "in_reply_to_user_id_str": "261119681", 
    "in_reply_to_screen_name": "salmamostafa90", 
    "contributors": null, 
    "place": null, 
    "coordinates": null, 
    "geo": null, 
    "source": "web", 
    "id_str": "219192312905990146", 
    "id": 219192312905990146, 
    "text": " \u0635\u0648\u0631\u0629 \u0644\u0642\u0641\u0627 .. \u062c\u0627\u0645\u062f\u0629 \u062c\u062f\u0627" 
    }, 
    "tar 

任何幫助深表感謝。

問候。

+0

在你上面的代碼你正在關閉,而不是'$ fp2'一個'$ fp'。該文件將不完整,直到句柄關閉顯然是http://www.php.net/manual/en/function.curl-setopt.php#92522 – 2012-07-27 00:41:07

+0

@JohnC然而,你是對的,不小心改變句柄到$ fp2不能解決問題。我相信問題在於curl連接是持久的,所以fclose($ fp2)語句永遠不會到達,除非curl流由於某種原因而停止。 – BitRiver 2012-07-27 01:32:52

回答

1

這可能是PHP的寫入緩衝區的問題,它將等到實際寫入文件之前收到一定數量的數據。您可以來解決這個使用stream_set_write_buffer各地:

$fp2 = fopen('file:///Users/KareemYousrii/dump.txt', "r+"); 
stream_set_write_buffer($fp2, 0); 
$ch = curl_init(); 
+0

我不知道它是否使用捲曲工作,但與fwrite它像一個魅力 – 2013-03-13 20:24:27