【原创】PHP通过快递100接口 百度接口 查询物流信息快递信息

项目经理需求,需求是公司穷,没钱,就要免费的,所以有了以下代码

/** 
  * 获取物流信息
  * @author Reaper <a@0oo.ren> 
  * @request https://www.52bz.la
  * @param string $postid 快递单号 $code 快递识别码,没有默认自动识别
  * @return url
  */ 
public function express($postid,$code)
{
    //接口
    $url = 'https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?appid=4001&nu=' . $postid . '&com=' . $code;

    //创建cookie目录文件
    $cookie = tempnam(sys_get_temp_dir(), 'cookie');;
    $t = parse_url($url);

    //获取cookie并写入
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "$t[scheme]://$t[host]/");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
    curl_exec($curl);

    //使用接口
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
    $data = curl_exec($curl);
    curl_close($curl);
    
    //转换给前端需要的格式并输出
    $odata = json_decode($data, 1);
    $newdata = $odata["data"]["info"]["context"];
    if (empty($odata["data"]["info"]["context"])) {
        return json(['statusCode' => 404, 'msg' => '出现未知错误!']);
    }
    foreach ($newdata as $k => $v) {
        $newdata[$k]['time'] = date("Y-m-d H:i:s", $v['time']);
    }
    $retjson = ['statusCode' => 200, 'msg' => '获取成功', 'data' =>  $newdata];
    return json_encode($retjson,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
Last modification:August 21st, 2019 at 01:16 am
如果觉得我的文章对你有用,请随意赞赏

2 comments

  1. 诗无尽头i

    44行应该改为$retjson

    1. Reaper
      @诗无尽头i

      感谢提醒,已修正!

Leave a Comment