我应该如何使用 Facebook PHP SDK 正确处理非光标分页?
How should I properly handle non-cursor pagination with the Facebook PHP SDK?
我正在使用一些 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>';
这个简单的代码将抓取当前用户的所有组。它检查 paging
和 paging
/next
属性是否存在,如果存在,则使用 cursor
设置循环的另一个迭代。我现在意识到这可能做得更好,因为光标并不总是可用。当我使用 /{group-id}/feed
API 端点时,有上一个和下一个链接但没有光标。
那么,当 Facebook PHP SDK 没有光标时,我应该如何发出分页请求?
我看到其他答案建议使用 cURL
甚至 file_get_contents
来获取 next
和 previous
URL,但考虑到我正在使用 PHP 这里有 SDK - 肯定有内置方法吗?
我正在使用 facebook/php-sdk-v4
和 Composer
- 似乎 没有(旧的?)$facebook->api(...)
功能可用这里也有。
看看
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();
}
通过查看源代码
它只处理 next
属性:
return $this->handlePagination('next');
恕我直言,使用 next
属性 作为默认值应该没问题,而不是 cursors
。此外,在查询示例组的提要时,我什至没有看到 cursors
属性,因此这可能已过时。
参考文献:
我正在使用一些 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>';
这个简单的代码将抓取当前用户的所有组。它检查 paging
和 paging
/next
属性是否存在,如果存在,则使用 cursor
设置循环的另一个迭代。我现在意识到这可能做得更好,因为光标并不总是可用。当我使用 /{group-id}/feed
API 端点时,有上一个和下一个链接但没有光标。
那么,当 Facebook PHP SDK 没有光标时,我应该如何发出分页请求?
我看到其他答案建议使用 cURL
甚至 file_get_contents
来获取 next
和 previous
URL,但考虑到我正在使用 PHP 这里有 SDK - 肯定有内置方法吗?
我正在使用 facebook/php-sdk-v4
和 Composer
- 似乎 没有(旧的?)$facebook->api(...)
功能可用这里也有。
看看
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();
}
通过查看源代码
它只处理 next
属性:
return $this->handlePagination('next');
恕我直言,使用 next
属性 作为默认值应该没问题,而不是 cursors
。此外,在查询示例组的提要时,我什至没有看到 cursors
属性,因此这可能已过时。
参考文献: