圆通电子面单demo

Posted 1156184981651a

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了圆通电子面单demo相关的知识,希望对你有一定的参考价值。

自己写的,比较简单,没什么好多说的

config.php

<?php
return [
    ‘Url‘=>‘请求地址‘,
    ‘CustomerCode‘=>‘客户编码‘,
    ‘CheckCode‘=>‘密钥‘,
    ‘SenderName‘=>‘发件人名‘,
    ‘SenderProvince‘=>‘发件人省‘,
    ‘SenderCity‘=>‘发件人市,发件人区‘,
    ‘SenderAddress‘=>‘发件人详细地址‘,
    ‘SenderPhone‘=>‘发件人电话‘
];

demo.php

<?php
include ‘Yt.php‘;
$GoodsName=‘商品名称‘;
$GoodsNumber=2;
$ReceiverName=‘张三‘;
$ReceiverProvince=‘湖北省‘;
//城市和区用英文逗号隔开
$ReceiverCityArea=‘武汉市,武昌区‘;
$ReceiverAddress=‘光谷广场未知区域‘;
$ReceiverMobile=‘15555555555‘;
$Config=include ‘config.php‘;
$YT=new YTYT($Config);
$Result=$YT->create($GoodsName,$GoodsNumber,$ReceiverName,$ReceiverProvince,$ReceiverCityArea,$ReceiverAddress,$ReceiverMobile);
var_dump($Result);

 YT.php

<?php

namespace YT;
/**
 * 圆通电子面单
* Class YT
 * @package YT
 * @author gcud
 * @version 20200427
 */
class YT
{
    private $config;

    public function __construct($Config)
    {
        $this->config = $Config;
    }

    /**
     * 签名
* @param $Xml
     * @return string
     */
private function sign($Xml)
    {
        return base64_encode(md5($Xml . $this->config[‘CheckCode‘], true));
    }

    /**
     * 创建请求数据的Xml
     * @param $Data
     * @return string
     */
private function createXml($Data)
    {
        $Xml = ‘<RequestOrder>‘;
        foreach ($Data as $Key => $Item) {
            $Xml .= "<$Key>";
            if (is_array($Item)) {
                foreach ($Item as $InlineKey => $Inline) {
                    $Xml .= "<$InlineKey>$Inline</$InlineKey>";
                }
            } else {
                $Xml .= $Item;
            }
            $Xml .= "</$Key>";
        }
        $Xml .= ‘</RequestOrder>‘;
        return $Xml;
    }

    /**
     * 创建电子面单
* @param string $GoodsName 商品名
* @param int|string $GoodsNumber 商品数量
* @param string $ReceiverName 收货人名
* @param string $ReceiverProvince 收货人省
* @param string $ReceiverCityArea 收货人城市和区,两者用英文逗号隔开
* @param string $ReceiverAddress 收货人详细地址
* @param string $ReceiverMobile 收货人电话
* @return mixed
     */
public function create($GoodsName, $GoodsNumber, $ReceiverName, $ReceiverProvince, $ReceiverCityArea, $ReceiverAddress, $ReceiverMobile)
    {
        $Sender = [‘name‘ => $this->config[‘SenderName‘], ‘prov‘ => $this->config[‘SenderProvince‘], ‘city‘ => $this->config[‘SenderCity‘], ‘address‘ => $this->config[‘SenderAddress‘], ‘phone‘ => $this->config[‘SenderPhone‘]];
        $Receiver = [‘name‘ => $ReceiverName, ‘prov‘ => $ReceiverProvince, ‘city‘ => $ReceiverCityArea, ‘address‘ => $ReceiverAddress, ‘mobile‘ => $ReceiverMobile];
        $Parameters[‘clientID‘] = $this->config[‘CustomerCode‘];
        $Parameters[‘logisticProviderID‘] = ‘YTO‘;
        $Parameters[‘customerId‘] = $this->config[‘CustomerCode‘];
        $Parameters[‘txLogisticID‘] = ‘hongkun‘ . date(‘YmdHis‘) . str_pad(mt_rand(0, 99999), 6, ‘0‘);
        $Parameters[‘tradeNo‘] = $this->config[‘CustomerCode‘];
        $Parameters[‘orderType‘] = 1;
        $Parameters[‘serviceType‘] = 0;
        $Parameters[‘sender‘] = $Sender;
        $Parameters[‘receiver‘] = $Receiver;
        $Parameters[‘itemName‘] = $GoodsName;
        $Parameters[‘number‘] = $GoodsNumber;
        $SendData[‘logistics_interface‘] = $this->createXml($Parameters);
        $SendData[‘data_digest‘] = $this->sign($SendData[‘logistics_interface‘]);
        $SendData[‘clientId‘] = $this->config[‘CustomerCode‘];
        $SendData[‘type‘] = ‘offline‘;
        //进行请求
return json_decode(json_encode(simplexml_load_string($this->curl_request($this->config[‘Url‘], $SendData))),true);
    }

    /**
     * curl请求
* @param string $Url  地址
* @param array $Data  数据,请求方法为get时所有参数必须写在这里
* @param bool $IsGet 是否get请求
* @return mixed
     */
function curl_request($Url,$Data,$IsGet=false){
        if($IsGet===false){
            $curl=curl_init($Url);
            curl_setopt($curl,CURLOPT_POST,true);
            curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($Data));
            curl_setopt($curl,CURLOPT_HTTPHEADER,[‘Content-Type:application/x-www-form-urlencoded‘]);
        }
        else{
            $curl =curl_init($Url.‘?‘.http_build_query($Data));
        }
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
        $result=curl_exec($curl);
        curl_close($curl);
        return $result;
    }
}

 把config.php参数改改就能直接用

硬是要我换行的废话分割行,不然上行变拼音,禁止套娃末尾bao‘hu

来源:http://www.1994july.club/

以上是关于圆通电子面单demo的主要内容,如果未能解决你的问题,请参考以下文章

免费圆通电子面单接口对接

圆通电子面单接口对接及调试

关于菜鸟的圆通电子面单打印

免费韵达电子面单接口类型及对接demo

快递鸟电子面单接口对接demo-JD京东物流

电商平台适用基础快递查询api接口对接demo解决方案