[英]How should I properly handle non-cursor pagination with the Facebook PHP SDK?
I am using a few of the Facebook Graph API methods that have pagination successfully using cursor-based pagination, similar to this: 我正在使用一些Facebook Graph API方法,这些方法已成功使用基于光标的分页进行分页,类似于:
echo '<ul>';
$params = array('limit' => 10);
do {
$groups = (new FacebookRequest(
$session, 'GET', '/me/groups', $params
))->execute()->getGraphObject();
if (null !== $groups->getProperty('paging') && null != $groups->getProperty('paging')->getProperty('next')) {
$params = array('limit' => 10, 'after' => $groups->getProperty('paging')->getProperty('cursors')->getProperty('after'));
} else {
$params = null;
}
foreach ($groups->getProperty('data')->asArray() as $group) {
echo '<li><a href="#" data-group-id="' . $group->id . '" class="group-id">' . $group->name . '</a></li>';
}
} while ($params !== null);
echo '</ul>';
This simple code will grab all the groups of the current user. 这个简单的代码将捕获当前用户的所有组。 It checks that the
paging
and paging
/ next
properties are present and if so uses the cursor
to setup another iteration of the loop. 它检查是否存在
paging
和paging
/ next
属性,如果存在,则使用cursor
设置循环的另一个迭代。 I realise now this could probably have been done better as the cursor isn't always available. 我意识到现在可能会做得更好,因为光标并不总是可用。 When I use the
/{group-id}/feed
API endpoint there are the previous and next links but no cursor. 当我使用
/{group-id}/feed
API端点时,存在上一个和下一个链接,但没有光标。
So, how am I supposed to make paginated requests when there is no cursor with the Facebook PHP SDK? 那么, 当Facebook PHP SDK没有光标时,我应该如何发出分页请求?
I see other answers suggesting using cURL
or even file_get_contents
to grab the next
and previous
URLs but that seems very silly considering I'm using the PHP SDK here - surely there's a built-in way? 我看到其他答案建议使用
cURL
或什至file_get_contents
来获取next
和previous
URL,但是考虑到我在这里使用PHP SDK,这似乎很愚蠢-当然有一种内置方法吗?
I'm using facebook/php-sdk-v4
with Composer
- there doesn't seem to be the (old?) $facebook->api(...)
functionality availble here either. 我在
Composer
使用facebook/php-sdk-v4
似乎这里没有(旧的) $facebook->api(...)
功能。
Have a look at 看一下
There is a method getRequestForNextPage()
in the PHP SDK v4.0.0. PHP SDK v4.0.0中有一个方法
getRequestForNextPage()
。
// A FacebookResponse is returned from an executed FacebookRequest
try {
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
// You can get the request back:
$request = $response->getRequest();
// You can get the response as a GraphObject:
$object = $response->getGraphObject();
// You can get the response as a subclass of GraphObject:
$me = $response->getGraphObject(GraphUser::className());
// If this response has multiple pages, you can get a request for the next or previous pages:
$nextPageRequest = $response->getRequestForNextPage();
$previousPageRequest = $response->getRequestForPreviousPage();
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
By looking at the source code at 通过查看以下源代码
it just handles the next
property: 它只是处理
next
属性:
return $this->handlePagination('next');
IMHO, using the next
property as a default should be fine, opposed to cursors
. 恕我直言,相对于
cursors
,使用next
属性作为默认值应该没问题。 Furthermore, I don't even see a cursors
property when querying a sample group's feed, so this might be obsolete. 此外,查询样本组的供稿时,我什至没有看到
cursors
属性,因此这可能已过时。
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.