PHP code example of hvenus / jingdong-api

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

    

hvenus / jingdong-api example snippets



// 创建实例
$jd = new JD();

// 设置系统参数
$jd->setConfig([
    'AppKey' => 'YOUR_APP_KEY',
    'AppSecret' => 'YOUR_APP_SECRET',
    'Url' => 'https://api.jd.com',
    'Uri' => 'routerjson',
    'Version' => '2.0',
    'Format' => 'json',
    'deptNos' => '事业部编号',
]);

// 设置应用参数
$jd->setParams([
    'access_token' => 'YOUR_ACCESS_TOKEN'
]);

// 获取京东快递运单号
$jd->ExpressGetWaybillCode([
    'preNum' => 1,
    'customerCode' => '商家编码',
    'orderType' => 0
]);

// 返回值(JSON):
{
    "jingdong_etms_waybillcode_get_responce": {
        "resultInfo": {
            "message": "成功",
            "code": "100",
            "deliveryIdList": [
                {
                    "deliveryIdList": "这里是快递号"
                }
            ]
        }
    }
}



// 先取出已经获得到授权数据
$auth_time = $config['jd:auth:time']; // token发放时间,单位毫秒
$auth_expires_in = $config['jd:auth:expires_in']; // 有效期时长, 单位秒
$auth_refresh_token = $config['jd:auth:refresh_token']; // refresh_token
$expires_time = $auth_time + $auth_expires_in * 1000; // 计算过期时间,单位毫秒。 token发放时间(毫秒) + 有效期时长(秒) * 1000
$diff = $expires_time/1000 - time(); // 取差值,表示还剩多少秒过期。
$deadline = 60*60*24; // 一天
if ($diff <= $deadline) { // 一天内过期
    // 刷新
    $param = [
      'client_id' => $config['AppKey'],
      'client_secret' => $config['AppSecret'],
      'refresh_token' => $config['refresh_token'],
      'state' => $Something, // 此参数内容原样返回
    ];
    $result = $jd->RefreshToken($param);
    if (false !== $result) {
        // 重新设置新token
        $config['jd:auth:access_token'] = $result['access_token']; // 新access_token
        $config['jd:auth:expires_in'] = $result['expires_in'];
        $config['jd:auth:refresh_token'] = $result['refresh_token']; // 京东文档说refresh_token是不变的。
        $config['jd:auth:time'] = $result['time'];
    }
}