أنت تعرض المستندات القديمة لواجهة برمجة التطبيقات Google Photos Library API.
بدء استخدام مكتبة عملاء PHP
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
لبدء استخدام Google Photos Library API مع مكتبة برامج PHP، عليك تنفيذ ما يلي:
إعداد مكتبة العملاء في بيئة التطوير التي تستخدمها.
وقبل إجراء ذلك، عليك ضبط مشروعك من خلال تفعيل واجهة برمجة التطبيقات.
عبر وحدة التحكم في واجهة Google API وإعداد معرِّف عميل OAuth 2.0.
يتفاعل تطبيقك مع "صور Google" نيابةً عن
مستخدم "صور Google" على سبيل المثال، عند إنشاء ألبومات في حساب
مكتبة "صور Google" أو تحميل ملفات الوسائط إلى حساب المستخدم
على "صور Google"، يفوّض المستخدم طلبات البيانات من واجهة برمجة التطبيقات هذه عن طريق
بروتوكول OAuth 2.0.
يتيح معرِّف عميل OAuth 2.0 لمستخدمي التطبيقات تسجيل الدخول والمصادقة
وبالتالي استخدام واجهة برمجة تطبيقات المكتبة. لا تتوافق واجهة برمجة تطبيقات المكتبة مع
حسابات الخدمة لاستخدام واجهة برمجة التطبيقات هذه، يجب أن يسجّل المستخدمون الدخول إلى حساب
الحساب.
تفعيل واجهة برمجة التطبيقات
قبل أن تتمكّن من استخدام Library API، يجب تفعيلها لمشروعك.
- انتقِل إلى وحدة التحكم في واجهة Google API.
- من شريط القوائم، اختَر مشروعًا أو أنشئ مشروعًا جديدًا.
- لفتح مكتبة Google API، من قائمة التنقل، حدد
واجهات برمجة التطبيقات الخدمات > المكتبة.
- ابحث عن "Google Photos Library API". حدد النتيجة الصحيحة وانقر على
تفعيل
طلب معرِّف عميل OAuth 2.0
اتّبِع الخطوات أدناه لطلب معرّف عميل OAuth وإعداده لحسابك.
التطبيق. يستخدم هذا المثال تطبيقًا حيث يكون مسار OAuth كاملاً
التعامل معها من جانب الخادم، مثل تلك الموجودة في العينات لدينا. قد تختلف عملية الإعداد
للتعرّف على سيناريوهات التنفيذ الأخرى.
- انتقِل إلى وحدة التحكم في واجهة Google API.
واختر مشروعك.
- من القائمة، حدد واجهات برمجة التطبيقات الخدمات > بيانات الاعتماد:
- في صفحة بيانات الاعتماد، انقر على إنشاء بيانات الاعتماد >. معرِّف عميل OAuth.
- اختَر نوع التطبيق. في هذا المثال، يكون نوع التطبيق
تطبيق الويب.
تسجيل المصادر التي يُسمح لتطبيقك من خلالها بالوصول إلى واجهات Google APIs
على النحو التالي:
- لتحديد معرِّف العميل، أدخِل اسمًا.
في الحقل مصادر JavaScript المعتمَدة، أدخِل مصدر
التطبيق. لا يسمح هذا الحقل باستخدام أحرف البدل.
يمكنك إدخال مصادر متعددة للسماح بتشغيل تطبيقك على
أو البروتوكولات أو النطاقات أو النطاقات الفرعية. يُسمح لعناوين URL التي تُدخلها
بدء طلب OAuth.
يوضح المثال التالي عنوان URL للتطوير المحلي (تستخدم النماذج لدينا
localhost:8080
) وعنوان URL خاص بالإنتاج.
http://localhost:8080
https://myproductionurl.example.com
إن حقل عنوان URI لإعادة التوجيه المعتمد هو نقطة النهاية التي تتلقى
الاستجابات من خادم OAuth 2.0. وعادةً، يتضمن ذلك
بيئة التطوير ويشير إلى مسار في تطبيقك.
http://localhost:8080/auth/google/callback
https://myproductionurl.example.com/auth/google/callback
انقر على إنشاء.
-
من مربّع حوار عميل OAuth الناتج، نزِّل ملف JSON.
يتضمن تكوين البرنامج. تتكون تفاصيل العميل من
التالي:
سيتم استخدام ملف JSON هذا لاحقًا للإعداد.
مكتبة مصادقة Google للغة PHP والتي تعمل مع مكتبة البرامج هذه.
قبل أن تتمكّن من تشغيل تطبيق عام يمكنه الوصول إلى واجهة برمجة تطبيقات المكتبة،
يجب أن تراجع Google تطبيقك. "تطبيق لم يتم التحقّق منه" تظهر في صفحة
الشاشة عند اختبار تطبيقك، إلى أن
التحقق منه.
إعداد مكتبة البرامج
تعالج مكتبة برامج PHP جميع طلبات البيانات من واجهة برمجة التطبيقات الخاصة بالخلفية، وتعرضها
للعمل بها، بما في ذلك عيّنات التعليمات البرمجية لبعض المهام الشائعة في واجهة برمجة التطبيقات.
أولاً، عليك تنزيل وتثبيت مكتبة برامج واجهة برمجة التطبيقات Google Photos Library API للغة PHP مع
والتبعيات من GitHub.
بعد ذلك، يمكنك إعداد بيانات اعتماد OAuth2 للغة PHP.
خيارات التنزيل
استخدام composer
لتضمين المكتبة كتبعية في بيئة التطوير الخاصة بك. تشغيل
التالي لإضافة المكتبة إلى إعدادات مشروعك وتنزيله
إلى دليل vendor/
.
composer require google/photos-library
بدلاً من ذلك، يمكنك أيضًا
استنساخ
المستودع أو
تنزيل ملف مضغوط
tarball.
إعداد بيانات اعتماد OAuth2 للغة PHP
تعمل مكتبة البرامج هذه مع
مكتبة مصادقة Google لـ
لغة PHP لمزيد من المعلومات
راجع استخدام OAuth 2.0 مع مكتبة برامج Google API للاطلاع على
لغة PHP
استخدام بيانات اعتماد المصادقة التي تعرضها مكتبة المصادقة عند الإعداد
PhotosLibraryClient
.
تجربة بعض النماذج
جرِّب الرمز أدناه لإجراء أول طلب بيانات من واجهة برمجة التطبيقات باستخدام مكتبة برامج PHP.
use Google\Auth\Credentials\UserRefreshCredentials;
use Google\Photos\Library\V1\PhotosLibraryClient;
use Google\Photos\Library\V1\PhotosLibraryResourceFactory;
try {
// Use the OAuth flow provided by the Google API Client Auth library
// to authenticate users. See the file /src/common/common.php in the samples for a complete
// authentication example.
$authCredentials = new UserRefreshCredentials( /* Add your scope, client secret and refresh token here */ );
// Set up the Photos Library Client that interacts with the API
$photosLibraryClient = new PhotosLibraryClient(['credentials' => $authCredentials]);
// Create a new Album object with at title
$newAlbum = PhotosLibraryResourceFactory::album("My Album");
// Make the call to the Library API to create the new album
$createdAlbum = $photosLibraryClient->createAlbum($newAlbum);
// The creation call returns the ID of the new album
$albumId = $createdAlbum->getId();
} catch (\Google\ApiCore\ApiException $exception) {
// Error during album creation
} catch (\Google\ApiCore\ValidationException $e) {
// Error during client creation
echo $exception;
}
هناك المزيد
عيّنات على GitHub
يمكنك تجربتها
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-08-29 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-08-29 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eTo use the Google Photos Library API with PHP, enable the API, request an OAuth 2.0 client ID, and set up the PHP client library.\u003c/p\u003e\n"],["\u003cp\u003eUsers must sign in with a Google Account as the API does not support service accounts, requiring OAuth 2.0 for authorization.\u003c/p\u003e\n"],["\u003cp\u003eSet up your OAuth2 credentials by configuring a web application in Google API Console with authorized origins and redirect URIs.\u003c/p\u003e\n"],["\u003cp\u003eDownload and install the Google Photos Library API client library for PHP using composer or by cloning the repository.\u003c/p\u003e\n"],["\u003cp\u003eAuthenticate users using the Google Auth Library for PHP, then utilize the PhotosLibraryClient to interact with the API and access its features.\u003c/p\u003e\n"]]],["To use the Google Photos Library API with PHP, first, enable the API in the Google API Console and create an OAuth 2.0 client ID, specifying your app's authorized JavaScript origins and redirect URIs. Download the client ID's JSON file, containing the client ID and secret. Next, install the PHP client library via Composer, and set up OAuth2 credentials using the downloaded JSON file. Use these credentials to set up the `PhotosLibraryClient` and try API calls.\n"],null,["# Get started with PHP client library\n\nTo start using the Google Photos Library API with the PHP client library, you will need to\n[set up the client library](#get-library) in your development environment.\nBefore you do that, [configure your project](#configure-app) by enabling the API\nvia the Google API Console and setting up an OAuth 2.0 client ID.\n\nYour application interacts with Google Photos on behalf of a\nGoogle Photos user. For instance, when you create albums in a user's\nGoogle Photos library or upload media items to a user's\nGoogle Photos account, the user authorizes these API requests via the\n[OAuth 2.0](/identity/protocols/OAuth2) protocol.\n\nThe OAuth 2.0 client ID allows your application users to sign in, authenticate,\nand thereby use the Library API. The Library API does not support\nservice accounts; to use this API, users must be signed in to a valid Google\nAccount.\n\nConfigure your app\n------------------\n\n### Enable the API\n\nBefore you can use the Library API, you must enable it for your project.\n\n1. Go to the [Google API Console](https://console.developers.google.com/apis/library).\n2. From the menu bar, select a project or create a new project.\n3. To open the Google API Library, from the Navigation menu, select **APIs \\& Services \\\u003e Library**.\n4. Search for \"Google Photos Library API\". Select the correct result and click **Enable**.\n\n### Request an OAuth 2.0 client ID\n\nFollow the steps below to request an OAuth client ID and configure it for your\napplication. This example uses an application where the entire OAuth flow is\nhandled server-side, such as the one in our samples. The setup process may vary\nfor other [implementation scenarios](/identity/protocols/OAuth2).\n\n1. Go to the [Google API Console](https://console.developers.google.com/apis/library) and select your project.\n2. From the menu, select **APIs \\& Services \\\u003e Credentials**.\n3. On the **Credentials** page, click **Create Credentials \\\u003e OAuth client ID**.\n4. Select your **Application type** . In this example, the application type is **Web application**.\n5. Register the origins from which your app is allowed to access the Google APIs\n as follows:\n\n 1. To identify the client ID, enter a name.\n 2. In the **Authorized JavaScript origins** field, enter the origin for your\n app. This field doesn't allow wildcards.\n\n You can enter multiple origins to allow your app to run on different\n protocols, domains, or subdomains. The URLs you enter are allowed to\n start an OAuth request.\n\n The following example shows a local development URL (our samples use\n `localhost:8080`) and a production URL. \n\n http://localhost:8080\n https://myproductionurl.example.com\n\n 3. The **Authorized redirect URI** field is the endpoint that receives\n responses from the OAuth 2.0 server. Typically, this includes your\n development environment and points to a path in your application.\n\n http://localhost:8080/auth/google/callback\n https://myproductionurl.example.com/auth/google/callback\n\n 4. Click **Create**.\n\n\u003c!-- --\u003e\n\n6. From the resulting OAuth client dialog, download the JSON file\n containing your client configuration. You client details consist of the\n following:\n\n - Client ID\n - Client secret\n\n This JSON file will be used later to [set up](#get-library)\n the Google Auth library for PHP which works with this client library.\n\nBefore you can launch a public application that accesses the Library API,\nyour app must be reviewed by Google. An \"Unverified app\" message appears on the\nscreen when you test your application, until it is\n[verified](https://support.google.com/cloud/answer/7454865).\n\nSet up the client library\n-------------------------\n\nThe PHP client library handles all the backend API calls for you, and exposes\nfriendly objects to work with, including code samples for some common API tasks.\nFirstly, download and install the Google Photos Library API client library for PHP along with\nthe dependencies from [GitHub](https://github.com/google/php-photoslibrary).\nThen, set up your OAuth2 credentials for PHP.\n\n### Download options\n\nUse [composer](https://github.com/google/php-photoslibrary#using-composer-require)\nto include the library as a dependency in your development environment. Run the\nfollowing command to add the library to your project configuration and download\nit to the `vendor/` directory. \n\n composer require google/photos-library\n\nAlternatively, you can also\n[clone the\nrepository](https://github.com/google/php-photoslibrary#using-git-clone) or\n[download a compressed\ntarball](https://github.com/google/php-photoslibrary#downloading-a-compressed-tarball).\n\n### Set up your OAuth2 credentials for PHP\n\nThis client library works with the\n[Google Auth Library for\nPHP](https://github.com/google/google-auth-library-php). For more information,\nrefer to [Using OAuth 2.0 with the Google API Client Library for\nPHP](https://developers.google.com/api-client-library/php/auth/web-app).\n\nUse the authentication credentials returned by the auth library when setting up\nthe `PhotosLibraryClient`.\n\nTry out some samples\n--------------------\n\nTry the code below to make your first API call using the PHP client library. \n\n use Google\\Auth\\Credentials\\UserRefreshCredentials;\n use Google\\Photos\\Library\\V1\\PhotosLibraryClient;\n use Google\\Photos\\Library\\V1\\PhotosLibraryResourceFactory;\n\n try {\n // Use the OAuth flow provided by the Google API Client Auth library\n // to authenticate users. See the file /src/common/common.php in the samples for a complete\n // authentication example.\n $authCredentials = new UserRefreshCredentials( /* Add your scope, client secret and refresh token here */ );\n\n // Set up the Photos Library Client that interacts with the API\n $photosLibraryClient = new PhotosLibraryClient(['credentials' =\u003e $authCredentials]);\n\n // Create a new Album object with at title\n $newAlbum = PhotosLibraryResourceFactory::album(\"My Album\");\n\n // Make the call to the Library API to create the new album\n $createdAlbum = $photosLibraryClient-\u003ecreateAlbum($newAlbum);\n\n // The creation call returns the ID of the new album\n $albumId = $createdAlbum-\u003egetId();\n } catch (\\Google\\ApiCore\\ApiException $exception) {\n // Error during album creation\n } catch (\\Google\\ApiCore\\ValidationException $e) {\n // Error during client creation\n echo $exception;\n }\n\nThere are more\n[samples](https://github.com/google/php-photoslibrary/tree/samples) on GitHub\nfor you to try."]]