Google มีไลบรารีของไคลเอ็นต์ PHP สําหรับการโต้ตอบกับ Ad Manager API เราขอแนะนําให้ใช้คลังไลบรารีไคลเอ็นต์กับ Composer
เริ่มต้นใช้งานโดยสร้างโปรเจ็กต์ใหม่ใน IDE ที่ต้องการ หรือเพิ่มข้อกำหนดในโปรเจ็กต์ที่มีอยู่ Google เผยแพร่อาร์ติแฟกต์ไลบรารีไคลเอ็นต์ไปยัง
Packagist ในรูปแบบ googleads/ad-manager
composer require googleads/ad-manager
กำหนดค่าข้อมูลเข้าสู่ระบบ
ไลบรารีไคลเอ็นต์ PHP ใช้ OAuth2 และข้อมูลเข้าสู่ระบบเริ่มต้นของแอปพลิเคชัน (ADC) เพื่อตรวจสอบสิทธิ์
ADC จะค้นหาข้อมูลเข้าสู่ระบบตามลําดับในตําแหน่งต่อไปนี้
- ตัวแปรสภาพแวดล้อม
GOOGLE_APPLICATION_CREDENTIALS
- ข้อมูลเข้าสู่ระบบของผู้ใช้ที่ตั้งค่าผ่าน Google Cloud CLI (gcloud CLI)
- เมื่อทำงานใน Google Cloud บัญชีบริการที่แนบอยู่กับทรัพยากร Google Cloud
ดูการสร้างและกําหนดค่าข้อมูลเข้าสู่ระบบ ADC ได้ที่การตรวจสอบสิทธิ์
ส่งคำขอแรก
แต่ละบริการจะมีออบเจ็กต์ ServiceClient
ที่มีเมธอดสำหรับเมธอด REST แต่ละรายการ ตัวอย่างต่อไปนี้จะอ่านออบเจ็กต์ Network
<?php
use Google\Ads\AdManager\V1\Client\NetworkServiceClient; use Google\Ads\AdManager\V1\GetNetworkRequest; use Google\Ads\AdManager\V1\Network; use Google\ApiCore\ApiException; /** * API to retrieve a Network object. * * @param string $formattedName Resource name of Network. * Format: networks/{network_code} * Please see {@see NetworkServiceClient::networkName()} for help formatting this field. */ function get_network_sample(string $formattedName): void { // Create a client. $networkServiceClient = new NetworkServiceClient(); // Prepare the request message. $request = (new GetNetworkRequest()) ->setName($formattedName); // Call the API and handle any network failures. try { /** @var Network $response */ $response = $networkServiceClient->getNetwork($request); printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString()); } catch (ApiException $ex) { printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); } } /** * Helper to execute the sample. * */ function callSample(): void { $formattedName = NetworkServiceClient::networkName('NETWORK_CODE'); get_network_sample($formattedName); }
ดูตัวอย่างวิธีการและแหล่งข้อมูลอื่นๆ ได้ที่ที่เก็บ GitHub
googleapis/php-ads-ad-manager
บันทึกคำขอ HTTP และการตอบกลับ
ไลบรารีไคลเอ็นต์ PHP รองรับโปรแกรมบันทึกที่เป็นไปตาม PSR-3 สำหรับการบันทึกคำขอและคำตอบ HTTP ระบบจะปิดใช้การบันทึกโดยค่าเริ่มต้น
หากต้องการเปิดใช้การบันทึกเริ่มต้นไปยังเอาต์พุตมาตรฐาน ให้ตั้งค่าตัวแปรสภาพแวดล้อม GOOGLE_SDK_PHP_LOGGING
เป็น true
ในโค้ด PHP หรือสภาพแวดล้อม
putenv('GOOGLE_SDK_PHP_LOGGING=true'); $client = new NetworkServiceClient();
export GOOGLE_SDK_PHP_LOGGING=true
หรือจะส่งบันทึกที่เป็นไปตาม PSR-3 เมื่อสร้างไคลเอ็นต์บริการก็ได้
use Monolog\Handler\StreamHandler; use Monolog\Level; use Monolog\Logger; $monologLogger = new Logger('sdk client'); $monologLogger->pushHandler(new StreamHandler('php://stdout', Level::Debug)); $client = new NetworkServiceClient([ 'logger' => $monologLogger ]);
จัดการข้อผิดพลาด
ในไลบรารีไคลเอ็นต์ PHP ข้อผิดพลาดทั้งหมดของ Ad Manager API จะแสดงข้อยกเว้นประเภท ApiException
ข้อผิดพลาดในการแยกวิเคราะห์
ช่องเหตุผลของข้อผิดพลาดจะระบุประเภทข้อผิดพลาดโดยไม่ซ้ำกัน ใช้ช่องนี้เพื่อกำหนดวิธีจัดการข้อผิดพลาด
try { $response = $networkServiceClient->getNetwork($formattedName); printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString()); } catch (ApiException $ex) { printf('Error message: %s' . PHP_EOL, $ex->getBasicMessage()); printf('Reason: %s' . PHP_EOL, $ex->getReason()); }
ข้อผิดพลาดของ Ad Manager API จะมี request_id
ที่ไม่ซ้ำกันซึ่งคุณสามารถส่งให้ฝ่ายสนับสนุนเพื่อขอความช่วยเหลือในการแก้ปัญหา ตัวอย่างต่อไปนี้จะดึงข้อมูล
request_id
$requestInfo = null; foreach ($ex->getMetadata() as $metadata) { if($metadata["@type"] === "type.googleapis.com/google.rpc.RequestInfo") { $requestInfo = $metadata; break; } } if ($requestInfo == null) { printf('Unexpected empty RequestInfo'); } else { printf('RequestId: %s' . PHP_EOL, $requestInfo['requestId']); }
สร้างชื่อทรัพยากร
ไลบรารีของไคลเอ็นต์มีคลาสตัวช่วยสำหรับสร้างชื่อทรัพยากรจากรหัส
use Google\Ads\AdManager\V1\Client\OrderServiceClient; // Constructs a String in the format: // "networks/{networkCode}/orders/{orderId}" $orderName = OrderServiceClient::orderName("NETWORK_CODE", "ORDER_ID");
ตั้งค่าพร็อกซี
ไลบรารีไคลเอ็นต์ PHP จะยึดตามการตั้งค่าสภาพแวดล้อม HTTP_PROXY
และ HTTPS_PROXY