php对接物流接口

注意点列举

  • 1.content-type是application/json。请求数据时,要将headers修改。接收数据时,php是无法直接收json格式的数据的。
  • 2.请求数据校验是将$identity和$token配合,作为身份校验。在url中将参数和密钥拼接在一起md5加密,生成token,在url终传参。

php post发送请求

这里对接的接口,类型是json格式。

    public function index()
    {
        ini_set("error_reporting","E_ALL & ~E_NOTICE");
        $identity = 'asdasd';
        $key = 'qweqweqw';
        $data = array(
                array(
                    "MerchantCode"=> "123",
                    "DistributionCode"=> "rfd",
                    "CurrentDistributioncode"=> "",
                    "SortingCenterId"=> "",
                    "SortingCenterName"=> "",
                    "WareHouseId"=> "",
                    "WarehouseName"=> null
                )
            );
        $dataString = json_encode($data, true);
        $str = $dataString.'|'.$key;
        $token = md5($str);
        $data =  json_encode($data, true);
        $url = "http://aaaaaaa?identity=". $identity."&token=".$token;
        $res = $this->request_post_push($url, $data);
        $resArr = json_decode($res, true);
        if ($resArr["IsSuccess"]) {
            $dataFetch = array(
                array(
                    "MerchantCode"=> "213",
                    "FormCode"=> "123123213",
                    "FromName"=> "qwe",
                    "FromAddress"=> "qwe",
                    "FromProvince"=> "qwe",
                    "FromCity"=> "qwe"
                )
            );
            $dataFc =  json_encode($dataFetch, true);
            $str2 = $dataFc.'|'.$key;
            $token2 = md5($str2);
            $urlFetch = "http://sssssssss?identity=". $identity."&token=".$token2;
            $resFetch = $this->request_post_push($urlFetch, $dataFc);
            var_dump($resFetch);exit;
        }
    }
    /**
     * 模拟post进行url请求
     * @param string $url
     * @param string $param
     */
    function request_post_push($url = "", $postData = "") {
// 初始化curl
        $ch = curl_init();
// 设置你需要抓取的URL
        curl_setopt($ch, CURLOPT_URL, $url);
// post提交方式
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 设置header
        curl_setopt($ch, CURLOPT_HEADER, false);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'X-AjaxPro-Method:ShowList',
            'Content-Type: application/json; charset=utf-8',
            'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
        ));
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
// 运行curl
        $data = curl_exec($ch);
// 关闭URL请求
        curl_close($ch);

        return $data ;
    }
  

php 接收请求

如果请求类型是json,php默认是解析不了的。这时候,只能用 file_get_contents('php://input', 'r');接收未被处理的数据。

       public function index()
    {
        $params = file_get_contents('php://input', 'r');
        $data=json_decode($params,true);
        $info_lists = $data;
        $returnData = array();
        $insertData = array();
        foreach($info_lists as $key => $value) {
            $rtItem = array(
                "WaybillNo"=> $value['WaybillNo'],
                "Message"=> null,
                "IsSuccess"=> true,
                "OperateId"=> $value['OperateId']
            );
            array_push($returnData, $rtItem);
            array_push($insertData, $value);
        }
        $logM = M('asd');
        $insertStr = json_encode($insertData);
        $dataStr = json_encode($data);
        $data['logtxt'] = $insertStr;
        $this->ajaxReturn(array('StatusInfo' => $returnData));
    }
  

随机浏览