简体   繁体   English

如何在Google相册API中使用可选参数?

[英]How to use optional parameters in Google Photos API?

I am working on project in php with Google Photos API. 我正在使用Google Photos API处理php项目。 i have an issue, if i pass optional parameters like pageSize, it doesn't work still get all images. 我有一个问题,如果我传递像pageSize这样的可选参数,它仍然无法获取所有图像。

$optParams = array(
  'pageSize' => 1,
);

$response = $photosLibraryClient->listMediaItems($optParams);
foreach ($response->iterateAllElements() as $item) {
    $id = $item->getId();
    $description = $item->getDescription();
    $mimeType = $item->getMimeType();
    $productUrl = $item->getProductUrl();
    $filename = $item->getFilename();

    echo '<br>';
    echo $filename;
}

I'm not a 100% sure of this , but it seems the iterateAllElements literally iterates over all elements available in the account, ignoring your specified pageSize (even the default pageSize) by requesting everything from the API without any boundaries. 我不是100%肯定这一点 ,但似乎iterateAllElements逐字地迭代帐户中可用的所有元素,忽略指定的pageSize(甚至是默认的pageSize),通过从API请求所有内容而没有任何边界。

You can iterate over the returned pages replacing iterateAllElements with iteratePages , but it also doesn't seems to work properly without a albumId, the API returns an irregular page sizing, like the example below: 您可以使用iteratePages迭代返回的页面替换iterateAllElements ,但是如果没有iteratePages它似乎也无法正常工作,API会返回不规则的页面大小,如下例所示:

$optParams = array(
  'pageSize' => 5 // Changed
);

$response = $photosLibraryClient->searchMediaItems($optParams); // Changed the method

foreach ($response->iteratePages() as $key => $page) {
    echo "Page #{$key}<br>";
    foreach ($page as $item) {
        $id = $item->getId();
        $description = $item->getDescription();
        $mimeType = $item->getMimeType();
        $productUrl = $item->getProductUrl();
        $filename = $item->getFilename();

        echo '<br>';
        echo $filename;
    }
}

if the search or list were called without providing a albumId , the example above would return something like this: 如果在没有提供albumId情况下调用搜索或列表,则上面的示例将返回如下内容:

[
    [{...},{...},{...}],
    [{...},{...},{...},{...},{...}],
    [{...}],
    [],
    [{...},{...},{...},{...},{...}],
    []
]

If you find a good solution for this specific problem, please let me know. 如果您找到针对此特定问题的良好解决方案,请告知我们。

Ps.: Their API behavior and it's documentation are very weird and confusing. Ps。:他们的API行为和它的文档非常奇怪和令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM