PHP code example of overbeck / logistics

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

    

overbeck / logistics example snippets




use Overbeck\Logistics\Logistics;

$config = [
    /*
     * 全局 http 
     * 请求配置 参考 https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html
     */
    'http'         => [
        'timeout'         => 5.0,
        'connect_timeout' => 5.0
    ],

    /*
     * 默认网关配置,如果设置此项,则只会使用该网关请求,否则会循环 gateways 调用请求不同的网关
     */
    'default'      => '',
    
    /*
     * 禁用网关,默认情况下会循环调用  gateways 下的所有可用网关,你可以添加网关名称到此禁用
     */
    'disable'      => [],
    
    /*
     * 网关配置
     */
    'gateways'     => [
        'kuaidi100' => [
            'key'      => '12124564561', // key
            'customer' => 'sahdkjsadjashuidhasdbak', // customer
            /*
             * 可以单独为指定的网关配置 http 请求信息,未设置则读取全局
             */
            'http'         => [
                'timeout'         => 15.0,
                'connect_timeout' => 15.0
            ],
        ],
        'kdniao'    => [
            'appKey'      => '456das12-dda-s87-d9a-1d2a1-o-p9', // appKey
            'EBusinessID' => '123456789', // EBusinessID
        ],
        'juhe'=>[
            'appKey'      => '4p5as1d21d564a56d12ad165a4d6', // appKey
        ]
        // ...
    ],
    
    /*
     * 格外配置物流公司列表
     */
    'company_file' => []
];
$logistics = new Logistics($config);
$res       = $logistics->query('123456789','顺丰速运');

// 提供 query() 函数即时查询物流信息
$logistics->query(string $logisticNumber, ?string $company = null, ?string $phone = null, $gateways = []): array
// 示例
$logistics->query('123456789'); // 仅用快递单号查询,不清楚快递公司时可用
$logistics->query('123456789','圆通速递'); // 锁定快递公司,更快速的查询
// 如果是 顺丰速运 的物流单号查询必须传递寄件人手机号
$logistics->query('123456789','顺丰速运','13800138000');

// 格外配置物流公司列表文件
'company_file' => [__DIR__ . '/company1.php',__DIR__ . '/company1.json']

return [
    [
        'name' => '顺丰速运',
        'code' =>
            [
                'aliyun'    => 'SFEXPRESS',
                'juhe'      => 'sf',
                'kuaidi100' => 'shunfeng',
                'kdniao'    => 'SF',
            ],
    ],
    [
        'name' => '申通快递',
        'code' =>
            [
                'aliyun'    => 'STO',
                'juhe'      => 'sto',
                'kuaidi100' => 'shentong',
                'kdniao'    => 'STO',
            ],
    ]
];

$logistics->setCompanyList(array $companyList);

$logistics->getCompanyList();

$logistics->getDefaultCompanyList();

$logistics->query('123456789','申通快递', null, 'kuaidi100'); // 指定单个网关的时,可以直接传递字符串
$logistics->query('123456789','申通快递', null, ['kuaidi100', 'kuaidiniao']);

 // 禁用网关,默认情况下会循环调用  gateways 下的所有可用网关,你可以添加网关名称到此禁用
 'disable'      => [],

 // 默认配置,如果设置此项,就只会使用该网关请求,否则会循环 gateways 调用请求不同的网关
 'default'      => 'kuaidi100',

 [
  "kuaidi100" => {
      [
          "gateway" => "kuaidi100"
          "status" => "success" // 网关请求成功 success 发生异常 failure
          "result" => [ // 查询结果
              "code" => 1 // 1 表示有结果 list会有数据 0 是查无结果
              "status" => 4 // 统一的状态返回
              "status_name" => "已签收" // 状态名称描述
              "company_code" => "youzhengbk" // 快递公司code
              "company_name" => "邮政快递包裹" // 快递公司名称
              "tracking_number" => "1178858799304" // 快递单号
              "list" => [
                  // ...
                  [
                      "context" => "离开【中国邮政集团公司贵州省石阡县寄递事业部本部揽投部】,下一站【贵州石阡县中心】"
                      "date_time" => "2020-07-27 17:46:27"
                  ],
                  [
                      "context" => "铜仁市 【中国邮政集团公司贵州省石阡县寄递事业部本部揽投部】已收件,揽投员:胡俊,电话:13758785985"
                      "date_time" => "2020-07-27 17:26:49"
                  ],
                 // ...            
              ],
              "original_data" => "{...}" // json字符串 响应原数据信息
          ]
  	  ]
  }
  "kdniao" => {
    [
      "gateway" => "kdniao"
      "status" => "failure"
      "exception" => \Overbeck\Logistics\Exceptions\Exceptions // 错误响应对象
    ]
  }
     // ...
]


$res = $logistics->query('123456789','顺丰速运');

/** @var \Overbeck\Logistics\Supports\Collection $kd */
$kd  = $res['kuaidi100'];
echo $kd->get('status'); // 获取网关状态
echo $kd100->get('result.status'); // 获取快递状态
echo $kd100->get('result.status_name'); // 获取快递状态名称
dump($kd->get('result.list')); // 获取快递信息列表
// ....

'providers' => [
    // ...
    Overbeck\Logistics\Laravel\ServiceProvider::class,
],
'aliases' => [
    // ...
    'Logistics' => Overbeck\Logistics\Laravel\Logistics::class,
],

php artisan vendor:publish --provider="Overbeck\Logistics\Laravel\ServiceProvider"



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Overbeck\Logistics\Laravel\Logistics;

class LogisticsController extends Controller
{
    public function query(Request $request)
    {
        dd(Logistics::query($request->route('code'), $request->input('company')));
    }
}