php-Facebook SDK(5.0)-图形API请求-分页(如何获取所有结果)
作者:互联网
我正在开发一个简单的Facebook应用程序,该应用程序需要获取用户所有的“页面喜欢”,以便将它们与作为匹配标识符的“页面ID”的预构建列表进行比较.
我可以得到一个喜欢的清单.但是,我只能在响应中获得“每页”赞100个.其中一些人有数千个赞,因此我需要确保可以从所有页面中收集所有数据.
一个简单的$fb-> next($response);会得到我的下一页(所以,而不是100个喜欢,我可以得到200个)..但是,当我将其放入循环中时,我的脚本陷入了无限循环,因为即使总有一个“下一页”,即使没有结果.
是否有任何内置方法可以轻松遍历这些结果,还是我必须在每次迭代中检查请求级别的数据结构,以了解这些项目是否已返回数据?
//this results in an infinite loop
$request = $fb->request('GET', '/'.$fid['id'].'/likes?limit=9999');
do {
$graphNode = $fb->getClient()->sendRequest($request);
$paging = $graphNode->getGraphEdge();
$graphEdge = $graphNode->getGraphEdge()->asArray();
$likes = array_merge($likes, $graphEdge);
} while ($fb->next($paging)); // ERROR IS HERE!! SET while($paging = $fb->next($paging)); INSTEAD
通过响应评论更新如下:
$request = $fb->request('GET', '/'.$fid['id'].'/likes?limit=9999');
$graphNode = $fb->getClient()->sendRequest($request);
$graphEdge = $graphNode->getGraphEdge();
if ($fb->next($graphEdge)) {
$likesArray = $graphEdge->asArray();
$lc = count($likesArray);
$totalLikes = array_merge($totalLikes, $likesArray);
while ($graphEdge = $fb->next($graphEdge)) {
$likesArray = $graphEdge->asArray();
$lic = count($likesArray);
$totalLikes = array_merge($totalLikes, $likesArray);
}
} else {
$likesArray = $graphEdge->asArray();
$totalLikes = array_merge($totalLikes, $likesArray);
}
解决方法:
这就是我一直使用的.通过将$likes变量更改为$data参数并通过该函数传递其他GraphEdge调用,您始终可以使用循环是一个函数.
$getPages = $this->fb->get('/me/likes?limit=100');
$likes = $getPages->getGraphEdge();
$totalLikes = array();
if ($this->fb->next($likes)) {
$likesArray = $likes->asArray();
$totalLikes = array_merge($totalLikes, $likesArray);
while ($likes = $this->fb->next($likes)) {
$likesArray = $likes->asArray();
$totalLikes = array_merge($totalLikes, $likesArray);
}
} else {
$likesArray = $likes->asArray();
$totalLikes = array_merge($totalLikes, $likesArray);
}
return $totalLikes;
标签:facebook,infinite-loop,facebook-php-sdk,php,facebook-graph-api 来源: https://codeday.me/bug/20191027/1942478.html