0

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;
}

1 Answers1

0

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.

Kaciano Ghelere
  • 393
  • 1
  • 4
  • 14