PHP code example of whalesky-labs / pdd-sdk-php

1. Go to this page and download the library: Download whalesky-labs/pdd-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/ */

    

whalesky-labs / pdd-sdk-php example snippets




declare(strict_types=1);

client = new PddClient(
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    options: ['accessToken' => 'merchant-access-token'],
);

$response = $client->pddOrderInformationGet()
    ->setParams(['order_sn' => '240101-123456'])
    ->send();

$response = $client->pddTimeGet()->send();

$response = $client->pddMallInfoGet()
    ->setAccessToken($requestAccessToken)
    ->send();

$request = $client->pddOrderListGet()
    ->setParams($queryParameters)
    ->mergeParams(['page' => 1]);

$response = $request->send();

$response = $client->rawRequest(
    type: 'pdd.order.status.get',
    parameters: ['order_sn' => '240101-123456'],
);

$response = $client->rawRequest(
    type: 'pdd.mall.info.get',
    parameters: [],
    accessToken: $accessToken,
    version: 'V1',
    dataType: 'JSON',
);

use PddSdk\Auth\AuthorizationType;

$merchantUrl = $client->oauth()->buildAuthorizeUrl(
    redirectUri: 'https://example.com/pdd/callback',
    state: 'random-csrf-state',
    type: AuthorizationType::Merchant,
);

$ddkUrl = $client->oauth()->buildAuthorizeUrl(
    redirectUri: 'https://example.com/pdd/ddk-callback',
    state: 'random-csrf-state',
    type: AuthorizationType::Ddk,
);

$token = $client->oauth()->getAccessToken(
    code: $_GET['code'],
    redirectUri: 'https://example.com/pdd/callback',
    state: $_GET['state'] ?? null,
);

$accessToken = $token->accessToken;
$refreshToken = $token->refreshToken;

$newToken = $client->oauth()->refreshAccessToken($refreshToken);

$client = new PddClient(
    clientId: $clientId,
    clientSecret: $clientSecret,
    options: [
        'accessToken' => $accessToken,
        'baseUrl' => 'https://gw-api.pinduoduo.com/api/router',
        'tokenUrl' => 'https://open-api.pinduoduo.com/oauth/token',
        'connectTimeout' => 5.0,
        'readTimeout' => 30.0,
        'autoDetectRuntime' => true,
        'userAgent' => 'MyApplication/1.0',
    ],
);

use PddSdk\Exception\ApiException;
use PddSdk\Exception\PddException;
use PddSdk\Exception\TransportException;
use PddSdk\Exception\ValidationException;

try {
    $response = $client->pddErpOrderSync()
        ->setParams($parameters)
        ->send();
} catch (ApiException $exception) {
    $platformCode = $exception->platformCode();
    $subCode = $exception->subCode();
    $platformPayload = $exception->responsePayload();
} catch (ValidationException $exception) {
    // 客户端配置、保留参数或已核验必填参数不合法
} catch (TransportException $exception) {
    // 网络失败或异常 HTTP 响应
} catch (PddException $exception) {
    // 其他 SDK 异常
}

$client = new PddClient(
    $clientId,
    $clientSecret,
    ['autoDetectRuntime' => false],
    httpClient: $applicationManagedGuzzleClient,
);
bash
# 从拼多多官方实时目录同步全部分类和接口
php scripts/sync-official-api-catalog.php

# 根据官方目录生成请求类、客户端方法和接口索引
composer generate:api

# 只检查生成结果,不修改文件
composer generate:api-check