码迷,mamicode.com
首页 > 其他好文 > 详细

圆通电子面单demo

时间:2020-04-27 22:59:27      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:and   form   erp   line   urlencode   nsf   parameter   post   imp   

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

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 \YT\YT($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

标签:and   form   erp   line   urlencode   nsf   parameter   post   imp   

原文地址:https://www.cnblogs.com/1156184981651a/p/12790331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!