简体   繁体   中英

How to use optional parameters in Google Photos API?

I am working on project in php with Google Photos API. i have an issue, if i pass optional parameters like pageSize, it doesn't work still get all images.

$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.

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:

$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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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