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

时间:2012-07-26 23:13:37

标签: php curl twitter

更新:经过一番挖掘,我发现解决方案相当容易。我所要做的就是用curl_setopt($ch, CURLOPT_FILE, $fp2);替换curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'writeCallback');WriteCallback的作用就是打开要写入数据的文件,将数据写入文件然后关闭文件。我相信下面所说的代码没有按预期工作的原因是因为curl打开了与twitter api的持久连接,因此永远不会超过curl_close($ch)close($fp)。希望这有助于任何可能遇到同样问题的人。

直到最近我才知道curl库。我目前正在尝试使用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

非常感谢任何帮助。

问候。

1 个答案:

答案 0 :(得分:1)

这可能是PHP写缓冲区的一个问题,它会等到实际写入文件之前收到一定数量的数据。您可以使用stream_set_write_buffer

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