[英]Facebook Graph API Slow for Larger Data Calls
這就是我在PHP中用來訪問Facebook上的一堆群帖。 我正在實施搜索功能來搜索這些帖子。
$url2 = 'https://graph.facebook.com/'. $group_id . '/feed' . '?limit=30&access_token=' . $_SESSION['access_token'] ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url2");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
$data = json_decode($json, TRUE);
所以我將其分解,並發現將圖形URL放入瀏覽器會產生緩慢的響應。 限制設置為30是可以的,但是它增加到300並且它很慢,增加到1000並且它爬行。
我看過分頁,但我想抓取大量數據,以便我可以搜索它。 緩存真的不起作用,因為加載初始數據仍然需要很長時間。
反正是為了加速這個還是我堅持Facebook Graph API的限制?
您可以批量處理您的請求,這樣您只需為批量請求設置一次卷曲,而不是通過大量卷曲查看http://developers.facebook.com/blog/post/2011/03/17/batch-requests-in-圖的API /
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_URL";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url);
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" . $app_secret . "&code=" . $code;
$access_token = file_get_contents($token_url);
$batched_request = '[{"method":"GET","relative_url":"me"},' . '{"method":"GET","relative_url":"me/friends?limit=50"}]';
$post_url = "https://graph.facebook.com/" . "?batch=" . $batched_request . "&access_token=" . $access_token . "&method=post";
echo $post_url;
$post = file_get_contents($post_url);
echo '<p>Response: <pre>' . $post . '</pre></p>';
您可以根據文檔使用CURLOPT_ENCODING
:
“Accept-Encoding:”標題的內容。 這使得能夠解碼響應。 支持的編碼是“identity”,“deflate”和“gzip”。 如果設置了空字符串“”,則發送包含所有支持的編碼類型的標頭。
這樣cURL就會告訴Facebook, “嘿,我理解壓縮數據,請發給我壓縮數據” 。
$url2 = 'https://graph.facebook.com/'. $group_id . '/feed' . '?limit=30&access_token=' . $_SESSION['access_token'] ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, ''); // enable compression, keep empty to send all supported types
$json = curl_exec($ch);
$data = json_decode($json, TRUE);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.