项目经理需求,需求是公司穷,没钱,就要免费的,所以有了以下代码
/**
* 获取物流信息
* @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);
}
2 条评论
44行应该改为$retjson
感谢提醒,已修正!