解決在TP5中沒法使用快遞鳥的即時查詢API

    快遞鳥的接口對接其實很簡單,先去官網註冊帳號,登錄把基本信息填好,而後在產品管理中訂購一下「物流查詢」,免費,不過其餘產品是收費,免費的有對接口調用頻率限制,結合本身的應用流量夠用就能夠。php

    使用前複製一下帳號下的用戶ID和API key,而且快遞鳥對各個API提供了各類語言的demo,其實下載下來,找一下平時寄快遞的運單號,本地運行一下就能用了。(名稱: KdApiSearchDemo)html

    其實拿到demo代碼,能夠放到項目中,由於demo是以面向過程寫的,因此爲了方便天然就想封裝一下。express

<?php

namespace data\extend;

use data\service\Config;

/**

 * 快遞鳥即時查詢接口

 * @author Administrator

 *

 */

class Kdniao{

    private $ebusinessid;//商戶ID

    private $appkey;     //商戶祕鑰

    private $request_type;//請求類型

    private $request_url; //請求URL

    /**

     * 構造函數

    */

    public function __construct($shop_id){

        $config=new Config();

        $express_config=$config->getOrderExpressMessageConfig($shop_id);

        $is_use=$express_config['is_use'];

        if($is_use==0){

            $this->ebusinessid = 'niushop';

            $this->appkey = 'niushop';

        }else{

            $this->ebusinessid = $express_config["value"]["appid"];

            $this->appkey = $express_config["value"]["appkey"];

        }

        $this->request_type = 1002;

        $this->request_url = 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';

    }

    

    

    //---------------------------------------------

    /**

     * Json方式 查詢訂單物流軌跡

     */

    public function getOrderTracesByJson($requestData){

        //$requestData= "{'OrderCode':'','ShipperCode':'YTO','LogisticCode':'12345678'}";

        $datas = array(

            'EBusinessID' => $this->ebusinessid,

            'RequestType' => $this->request_type,

            'RequestData' => urlencode($requestData) ,

            'DataType' => '2',

        );

        $datas['DataSign'] = $this->encrypt($requestData, $this->appkey);

        $result=$this->sendPost($this->request_url, $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);

        $url_info = parse_url($url);

        if(empty($url_info['port']))

        {

            $url_info['port']=80;

        }

        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";

        $httpheader.= "Host:" . $url_info['host'] . "\r\n";

        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";

        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";

        $httpheader.= "Connection:close\r\n\r\n";

        $httpheader.= $post_data;

        $fd = fsockopen($url_info['host'], $url_info['port']);

        fwrite($fd, $httpheader);

        $gets = "";

        $headerFlag = true;

        while (!feof($fd)) {

            if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {

                break;

            }

        }

        while (!feof($fd)) {

            $gets.= fread($fd, 128);

        }

        fclose($fd);

    

        return $gets;

    }

    

    /**

     * 電商Sign簽名生成

     * @param data 內容

     * @param appkey Appkey

     * @return DataSign簽名

     */

    public function encrypt($data, $appkey) {

        return urlencode(base64_encode(md5($data.$appkey)));

    }

}

 

 可是運行起來就一直報錯api

 

demo中提供的請求快遞鳥API是使用的fsockopen函數,該函數是PHP較高版本(5.0以上)中功能比較強大的,經過一個URL和PORT請求返回一個文件指針,後面的就能夠經過文件操做函數獲取返回結果。app

    看到這個報錯,確認了我當前使用的PHP版本,而後去php.ini配置文件查看allow_url_fopen 是開的(ON),擴展包也是去掉註釋的,禁用函數中也沒有fsockopen。函數

    最好我選擇把接口請求寫成函數,居然就有效了,最後的總結出來的是,fscockopen函數放在php類裏面不起效果,要放在函數中使用,實例以下:post

 

 

相關文章
相關標籤/搜索