PHP调用快递100电子面单接口API接口

PHP调用快递100电子面单接口API接口

1.注册申请快递100接口

https://poll.kuaidi100.com/pollquery/pollStep.do?method=myApiKey.
申请快递100企业版-快递查询接口(API) 截图如下
在这里插入图片描述
申请通过快递100接口会已邮件的形式发送给你对应的配置参数 截图如下
在这里插入图片描述

2.接口调用

// 快递100  电子面单key
    protected $free_sheetkey = '********';//申请通过快递100提供
    protected $free_secret = '******'; //申请通过快递100提供
 /*
     *电子面单接口API
     * @order  array() 订单信息 ['out_trade_no'->内部订单号,'num'->数量,'weight'->重量]
     * @address  array()  收货人信息
     * @returnsetting  array() 发货人信息
     */
    public function getElecOrder($order=array(),$address=array(),$returnsetting=array()){
        // $getlists = 'http://api.kuaidi100.com/eorderapi.do?method=getElecOrder';
        $param = $this->get_recMan($order,$address,$returnsetting);
        $str = $this->setParameter($param);
        $http = 'http://api.kuaidi100.com/eorderapi.do?method=getElecOrder&'.$str;
        $return_data = http($http);
        return $return_data;
    }
   
   // 快递100 面单数据实体
    /**
    *  @orderid  订单号
    *  @$address  收件人地址
    *  @ returnsetting  寄件地址
    **/
    public function get_recMan($order=array(),$address=array(),$returnsetting=array()){
        $paramlists = array(
            'partnerId' => '****',  //中通快递电子面单账号  需要跟对应快递合作,顺丰,宅急送可免
            'partnerKey' => '****',//中通快递电子面单密码
            'kuaidicom' => 'zhongtong', //中通快递
            'orderId' => $order['out_trade_no'], //内部订单编号
            'recMan'  => array(   //收件人信息
                    'name' => $address['consigner'], 
                    'mobile'=> $address['mobile'],
                    'zipCode'=> $address['zip_code'],
                    'printAddr'=> $address['address_info'].$address['address'],
                ),
            'sendMan' => array(   // 寄件人信息
                    'name' => $returnsetting['seller_name'],
                    'mobile' => $returnsetting['seller_mobile'],
                    'zipCode' => $returnsetting['seller_zipcode'],
                    'printAddr' => $returnsetting['shop_address'],
            ),
            'count' =>$order['num'],
            'weight' => $order['weight'],
        );
        return json_encode($paramlists);
    }
 /**
     * 生成签名
     * @param $parameter
     * @return string
     */
    protected function getStringToSign($parameter)
    {
        // var_dump($parameter);die();
        $str = '';
        foreach ($parameter as $key => $value) {
            if (!empty($value)) {
                $str .=  ($value);
            }
        }
        $signature = strtoupper(md5($str));
        return $signature;
    }

    protected function setParameter($method)
    {
        $time = $_SERVER['REQUEST_TIME'];
        $publicParameter = array(
            'param' => $method,
            't' => $time,
            'key' => $this->free_sheetkey,
            'secret' => $this->free_secret,
        );
        // var_dump($publicParameter);die();
        $sign = $this->getStringToSign($publicParameter);
        $parameter = array_merge($publicParameter, ['sign' => $sign]);
        $str = '';
        foreach ($parameter as $key => $value) {
            $str .= $key . '=' . urlencode($value) . '&';
        }
        // var_dump($str);die();
        return rtrim($str, '&');
    }

	/**
	 * 发送HTTP请求方法,目前只支持CURL发送请求
	 *
	 * @param string $url
	 *            请求URL
	 * @param array $params
	 *            请求参数
	 * @param string $method
	 *            请求方法GET/POST
	 * @return array $data 响应数据
	 */
	function http($url, $timeout = 30, $header = array())
	{
	    if (! function_exists('curl_init')) {
	        throw new Exception('server not install curl');
	    }
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($ch, CURLOPT_HEADER, true);
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
	    if (! empty($header)) {
	        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	    }
	    $data = curl_exec($ch);
	    list ($header, $data) = explode("\r\n\r\n", $data);
	    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	    if ($http_code == 301 || $http_code == 302) {
	        $matches = array();
	        preg_match('/Location:(.*?)\n/', $header, $matches);
	        $url = trim(array_pop($matches));
	        curl_setopt($ch, CURLOPT_URL, $url);
	        curl_setopt($ch, CURLOPT_HEADER, false);
	        $data = curl_exec($ch);
	    }
	    
	    if ($data == false) {
	        curl_close($ch);
	    }
	    @curl_close($ch);
	    return $data;
	}

快递100技术文档:
https://www.kuaidi100.com/openapi/ele_api.shtml

电子面单我是对接的中通快递,联系了一个中通快递网点。网点通过电子面单下添加业务员会相应给一个 账号和商家密码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯鑫BOSS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值