<?php
//请求url
defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx');
defined('Eorder') or define('Eorder', 'http://api.kdniao.com/api/Eorderservice');

//调用查询物流轨迹
//---------------------------------------------

// $logisticResult=getOrderTracesByJson();
// echo logisticResult;
class Kd{
    private $uid;//快递鸟用户id
    private $apiley;//快递鸟用户id
        /**
         * Json方式 查询订单物流轨迹
         */
     public function __construct($uid,$apiley){
         $this->uid = $uid;
         $this->apiley = $apiley;
     }
     /**
      * 单号识别
      * 返回
      *  "ShipperCode": "YD",
          "ShipperName": "韵达快递"
      * */
    public function getOrderTracesByJson($LogisticCode){
        $requestData= "{'LogisticCode':'{$LogisticCode}'}";
        $datas = array(
            'EBusinessID' => $this->uid,
            'RequestType' => '2002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData, $this->apiley);
        $result=$this->sendPost(ReqURL, $datas);

        //根据公司业务处理返回的信息......

        return $result;
    }
     public function getTracesByJson($ShipperCode,$LogisticCode){
            $requestData= "{'OrderCode':'','ShipperCode':'{$ShipperCode}','LogisticCode':'{$LogisticCode}'}";

            $datas = array(
                'EBusinessID' => $this->uid,
                'RequestType' => '1002',
                'RequestData' => urlencode($requestData) ,
                'DataType' => '2',
            );
            $datas['DataSign'] = $this->encrypt($requestData, $this->apiley);
            $result=$this->sendPost(ReqURL, $datas);

            //根据公司业务处理返回的信息......

            return $result;
        }
      public function submitEOrder($requestData){
            $datas = array(
                'EBusinessID' => $this->uid,
                'RequestType' => '1007',
                'RequestData' => urlencode($requestData) ,
                'DataType' => '2',
            );
            $datas['DataSign'] = $this->encrypt($requestData, $this->apiley);
            $result=$this->sendPost(Eorder, $datas);

            //根据公司业务处理返回的信息......

            return $result;
        }
        /**
         *  post提交数据
         * @param  string $url 请求Url
         * @param  array $datas 提交的数据
         * @return url响应返回的html
         */
      public  function sendPost($url, $datas) {
            $temps = array();
            foreach ($datas as $key => $value) {
                $temps[] = sprintf('%s=%s', $key, $value);
            }
            $post_data = implode('&', $temps);
            $gets = $this->request_post($url,$post_data);
            return $gets;
        }
        /**
         * 模拟post进行url请求
         * @param string $url
         * @param string $param
         */
      public function request_post($url = '', $param = '') {
            if (empty($url) || empty($param)) {
                return false;
            }

            $postUrl = $url;
            $curlPost = $param;
            $ch = curl_init();//初始化curl
            curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
            curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
            $data = curl_exec($ch);//运行curl
            curl_close($ch);

            return $data;
        }

        /**
         * 电商Sign签名生成
         * @param data 内容
         * @param appkey Appkey
         * @return DataSign签名
         */
      public function encrypt($data, $appkey) {
            return urlencode(base64_encode(md5($data.$appkey)));
        }
}
?>