You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PHP调用Google Photos API?能否在PHP网站获取全部照片?

Using Google Photos API with PHP (Replacing Deprecated Picasa API)

Hey there! I know you hit a wall with the deprecated Picasa Web Albums API, so let’s walk through how to work with the Google Photos Library API using PHP instead, and answer your two core questions clearly.

1. How to Access Google Photos API with PHP

Here’s a step-by-step breakdown to get you up and running:

Prerequisites

  • First, go to the Google Cloud Console, create a new project (or use an existing one), and enable the Photos Library API for it.
  • Create OAuth 2.0 credentials (choose the "Web application" type for your website). Don’t forget to add your site’s redirect URI(s) in the credentials settings.
  • Install the official Google Client PHP library via Composer:
    composer require google/apiclient:^2.0
    

Basic Authorization & API Call Example

This code handles the OAuth flow and fetches album data (easily adaptable for photos):

<?php
require __DIR__ . '/vendor/autoload.php';

session_start();

$client = new Google\Client();
$client->setApplicationName('Your Website Name');
$client->setScopes([Google\Service\PhotosLibrary::PHOTOSLIBRARY_READONLY]);
$client->setAuthConfig('path/to/your/oauth-credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');

// Handle redirect after user grants permission
if (!isset($_GET['code'])) {
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    exit;
} else {
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    
    // Initialize the Photos Library service
    $photosService = new Google\Service\PhotosLibrary($client);
    
    // Fetch and display albums
    $albums = $photosService->albums->listAlbums();
    foreach ($albums->getAlbums() as $album) {
        echo "Album: " . $album->getTitle() . " (ID: " . $album->getId() . ")<br>";
    }
}
?>

2. Can I Fetch All Google Photos on My Own Website via PHP?

Absolutely—but there are a few key details to keep in mind:

  • Permissions: You’ll need the right OAuth scope. Use photoslibrary.readonly for read-only access, or photoslibrary if you need write capabilities. The user must grant this permission during the OAuth flow.
  • Pagination: The API doesn’t return all photos in one call. You’ll need to loop through pages using the pageToken parameter. Here’s how to fetch all accessible media items:
    $pageToken = null;
    $allPhotos = [];
    
    do {
        $response = $photosService->mediaItems->listMediaItems([
            'pageToken' => $pageToken,
            'pageSize' => 100 // Maximum allowed per page
        ]);
        
        if (!empty($response->getMediaItems())) {
            $allPhotos = array_merge($allPhotos, $response->getMediaItems());
        }
        
        $pageToken = $response->getNextPageToken();
    } while ($pageToken !== null);
    
    // Iterate through all fetched photos
    foreach ($allPhotos as $photo) {
        // Use baseUrl with parameters to resize (e.g., w800-h600)
        echo "Photo: " . $photo->getFilename() . "<br>URL: " . $photo->getBaseUrl() . "=w800-h600<br><br>";
    }
    
  • Rate Limits: Google enforces rate limits on the API, so make sure to handle errors gracefully and implement retries if needed.

A Quick Note on the Deprecated Picasa API

You’re totally correct—the Picasa Web Albums Data API is fully deprecated, and the old PHP client library download link is no longer active. There’s no reliable way to use that library anymore, so switching to the Photos Library API is the only supported path forward.

内容的提问来源于stack exchange,提问作者Chandan Kushwah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:06:50