Re: [請益] 噗浪機器人的問題...
※ 引述《guardlan (亞修)》之銘言:
: ================================================================
: 前略...
: $plurk = new plurk_api();
: $plurk->login($api_key, $username, $password);
: $result = $plurk->realtime_get_user_channel();
: $comet_server = preg_replace('/\&offset=\d+/', '', $result->comet_server);
: $offset = preg_match('/\&offset=(\d+)/', $result->comet_server, $matches) ? $matches[1] : NULL;
: sleep(50);
: $result = $plurk->realtime_get_commet_channel($comet_server, $offset);
: var_dump($result);
: =================================================================
: 是寫錯嗎~還請各位先進指教
: 另外我還有個問題想請教...
: 即使使用 RealTime API 也是需要每固定秒數去打 RealTime API 取得資料吧!?
50秒連一次,我想應該不會被擋吧~
只是另外還有polling API,不過不同API之間的合作... 沒研究過
我會提到polling API,主要是因為我怕太常出現新plurk或response了
不過似乎可以藉由自己sleep來解決,來控制最短request時間
$start_time = time();
$result = realtime_get_commet_channel($comet_server, $offset);
$diff_time = 5 - (time() - $start_time);
if ($diff_time > 0) {
sleep($diff_time);
}
: 這樣的話跟我想打 API 次數應該減差不了多少~?
我自己實際測試一下,然後觀察一下
http://code.google.com/p/php-plurk-api/
的原始碼...
有注意到幾點:
1.constant.php裡面的login,update,register沒有特別用https
建議改成https
2.plurk_api->plurk預設使用POST
不過其實也還好,似乎plurk api大部分沒特別寫要用POST或GET
3.實際上,realtime_get_user_channel回傳的comet_server的字串
包含&offset=0
不過也還好... 反正plurk看的懂就好
假設&offset=0&offset=3應該也不會炸(我猜的)
4.
plurk_api->realtime_get_commet_channel
還是使用POST request
然後根據API的說明: you do GET requests to this URL to get new data
我不知道這有沒有差...
5.這點大概是關鍵吧...
因為最久會等50秒,不知道curl預設會怎麼做,
這邊可能需要在realtime_get_commet_channel(plurk_api.php)裡面
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 80);
curl_setopt($ch, CURLOPT_USERAGENT, PLURK_AGENT);
加上CURLOPT_TIMEOUT那一行
(我不知道會多久啦,設定80秒只是為了保險,
根據php curl文件,設定為0是無限等待
Use 0 to wait indefinitely.)
不過查了一下,似乎有人說預設不會timeout,
所以如果還是無效的話,就再看看吧
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.231.172.67
討論串 (同標題文章)