Google มีไลบรารีของไคลเอ็นต์ PHP สำหรับการโต้ตอบกับ Ad Manager API เราขอแนะนำให้ใช้ไลบรารีของไคลเอ็นต์กับ Composer
หากต้องการเริ่มต้นใช้งาน ให้สร้างโปรเจ็กต์ใหม่ใน IDE ที่คุณเลือกหรือเพิ่มทรัพยากร Dependency ลงในโปรเจ็กต์ที่มีอยู่ Google เผยแพร่อาร์ติแฟกต์ไลบรารีของไคลเอ็นต์ไปยัง Packagist เป็น googleads/ad-manager
composer.json
"require": {
"googleads/ad-manager": "^0.16.0"
}
บรรทัดคำสั่ง
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; /** * Retrieves 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