PHP code example of wiewind / dhl-php-api

1. Go to this page and download the library: Download wiewind/dhl-php-api 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/ */

    

wiewind / dhl-php-api example snippets




use Wiewind\DHL\Shipment\Credentials;   // 运单
use Wiewind\DHL\Returns\Credentials;    // 退货
use Wiewind\DHL\Tracking\Credentials;   // 追踪

$cred = new Wiewind\DHL\Shipment\Credentials('sandbox');   // 或 'production'
$cred->setOrder($order);
$res = $cred->run();

if ($res['success']) {
    $res['shipmentNo'];   // 运单号
    $res['pdfLabel'];     // 运单 PDF(base64)
    $res['customsDoc'];   // 报关单 PDF(base64,仅出口时返回)
}

[
  'success'    => 1 | 0,
  'httpCode'   => int,
  'shipmentNo' => string,   // 首个运单(便捷字段)
  'pdfLabel'   => string,   // 首个运单的标签 base64
  'customsDoc' => string,   // 首个运单的报关单 base64
  'shipments'  => [ ['shipmentNo'=>.., 'pdfLabel'=>.., 'customsDoc'=>..], ... ],
  'errorMsg'   => string,   // 失败时
  'errorCode'  => int,      // 失败时
]

$cred = new Wiewind\DHL\Returns\Credentials('sandbox');   // 或 'production'
$cred->setOrder($order);
$res = $cred->run();

if ($res['success']) {
    $res['shipmentNo'];   // 退货运单号
    $res['pdfLabel'];     // 退货标签 PDF(base64)
    $res['qrLabel'];      // 退货二维码(base64,无纸化退货门店扫码用)
}

[
  'success'    => 1 | 0,
  'shipmentNo' => string,   // 退货运单号
  'pdfLabel'   => string,   // 退货标签 base64
  'qrLabel'    => string,   // 退货二维码 base64
  'errorMsg'   => string,   // 失败时
  'errorCode'  => int,      // 失败时
]



$cred = new Wiewind\DHL\Tracking\Credentials('production');  // 或 'sandbox'
$cred->setApiKey('your-dhl-api-key');
$cred->setTrackingNumber('00340434161094015902');
$cred->setService('parcel-de');   // 可选:指定服务,缩小范围
$res = $cred->run();

if ($res['success']) {
    foreach ($res['shipments'] as $shipment) {
        $shipment->getStatusCode();    // delivered / transit / pre-transit ...
        $shipment->getStatus();        // 状态文案
        $shipment->isDelivered();      // 是否已签收
        foreach ($shipment->getEvents() as $event) {
            $event->getTimestamp();
            $event->getDescription();
        }
    }
}

[
  'success'   => 1 | 0,
  'httpCode'  => int,
  'shipments' => Shipment[],   // Shipment 对象数组(见下方方法)
  'errorMsg'  => string,       // 失败时
  'errorCode' => int,          // 失败时
]

$cred = new Credentials('production');
$cred->setUsername('...');     // DHL 商业账户用户名
$cred->setPassword('...');     // DHL 商业账户密码
$cred->setApiKey('...');       // dhl-api-key(开发者门户申请)
bash
composer 
bash
php example/shipment.php
bash
php example/returns.php
bash
php example/tracking.php