使用curl从twitter流API检索数据时写入文件的数据不完整

Update: After some more digging around, I found that the solution was rather easy. All I had to do was replace curl_setopt($ch, CURLOPT_FILE, $fp2); with curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'writeCallback');. What WriteCallback does is simply as open the file to which data is to be written, write the data to the file and then close the file. I believe the reason the code stated below wasn't working as intended is because the curl opens a persistent connection with twitter api, and therefore never goes past the curl_close($ch) to the close($fp). Hope this helps anyone who might be facing the same problem.

Only recently have I got acquaintaned with curl library. I am currently attempting to use curl to keep a consistent connection with Twitter's streaming api.

Here is my code so far:

$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);

When I remove the curl_setopt($ch, CURLOPT_FILE, $fp2); line and run the file from the terminal I get the required response. However, If I keep it as shown in the example, I get a text file with inconsistent data. Meaning that data about a specific event (i.e. favoriting of a tweet or retweeting one) isn't fully written to the file unless another event happens, at which point the first event is written fully but the second one only partially.

This is an example of the file contents of the latest event:

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

Any help is much appreciated.

Regards.

This is probably an issue with PHP's write buffer, it will wait until a certain amount of data has been received before actually writing to a file. You may be able to work around this using stream_set_write_buffer:

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