更新:经过一番挖掘,我发现解决方案相当简单。我所要做的就是替换curl_setopt($ch, CURLOPT_FILE, $fp2);
为curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'writeCallback');
. 所做的只是打开要写入数据的文件,WriteCallback
将数据写入文件,然后关闭文件。我相信下面所述的代码没有按预期工作的原因是因为 curl 打开了与 twitter api 的持久连接,因此永远不会curl_close($ch)
超过close($fp)
. 希望这可以帮助任何可能面临同样问题的人。
直到最近我才熟悉 curl 库。我目前正在尝试使用 curl 与 Twitter 的流 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
任何帮助深表感谢。
问候。