PHP code example of westng / oceanengine-sdk-php

1. Go to this page and download the library: Download westng/oceanengine-sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

westng / oceanengine-sdk-php example snippets




use Core\Exception\OceanEngineException;
use OceanEngineSDK\OceanEngineClient;

// 创建客户端
$client = new OceanEngineClient('your_access_token');

// 配置重试机制
$client->setRetryConfig(
    maxRetries: 3,
    retryDelay: 1000,
    enableRetry: true
);

// 调用 API
try {
    $response = $client->module('Account')
        ->AccountInfo
        ->AdvertiserInfo()
        ->setParams(['account_ids' => ['123456789']])
        ->send();

    $data = json_decode($response->getBody(), true);
    print_r($data);
} catch (OceanEngineException $e) {
    echo "错误: {$e->getErrorMessage()}";
}



use Core\Exception\OceanEngineException;
use OceanEngineSDK\OceanEngineAuth;

$auth = new OceanEngineAuth(APPID, SECRET);
$url = $auth->getAuthCodeUrl(CALLBACK_URL, '', 'auth_code', 'AD');
echo "授权链接: {$url}";



use Core\Exception\OceanEngineException;
use OceanEngineSDK\OceanEngineAuth;

$auth = new OceanEngineAuth(APPID, SECRET);

// 使用授权码获取 token
$tokenData = $auth->getAccessToken($authCode);

// 刷新 token
$newTokenData = $auth->refreshToken($refreshToken);



$client = new OceanEngineClient(TOKEN);

// 配置重试机制
$client->setRetryConfig(
    maxRetries: 3,
    retryDelay: 1000,
    retryableStatusCodes: [408, 429, 500, 502, 503, 504],
    enableRetry: true,
    retryableBusinessCodes: [40100, 40110, 50000]
);

// 动态控制重试开关
$client->setRetryEnabled(true);   // 启用重试
$client->setRetryEnabled(false);  // 禁用重试



use Core\Exception\OceanEngineException;
use OceanEngineSDK\OceanEngineClient;

try {
    $client = new OceanEngineClient(TOKEN);

    $response = $client->module('Account')
        ->AccountInfo
        ->AdvertiserInfo()
        ->setParams([
            'account_ids' => ['123456789'],
            'fields' => ['id', 'name', 'status']
        ])
        ->send();

    $data = json_decode($response->getBody(), true);
    print_r($data);
} catch (OceanEngineException $e) {
    echo "错误类型: {$e->getErrorType()}\n";
    echo "错误码: {$e->getErrorCode()}\n";
    echo "错误信息: {$e->getErrorMessage()}\n";
}

try {
    $response = $client->module('Account')->AccountInfo->AdvertiserInfo()
        ->setParams($args)
        ->send();
} catch (OceanEngineException $e) {
    // 处理业务错误
    echo "错误类型: {$e->getErrorType()}\n";
    echo "错误码: {$e->getErrorCode()}\n";
    echo "错误信息: {$e->getErrorMessage()}\n";
} catch (Exception $e) {
    // 处理其他错误
    echo "系统错误: {$e->getMessage()}\n";
}
bash
composer