12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\api\model;
- use think\Model;
- /*
- * 查看物流
- */
- class Logistics extends Model
- {
- private $appkey = "3be319b58697756c60b5bb090ee9edf5"; //申请的快递查询APPKEY
- private $queryUrl = 'http://v.juhe.cn/exp/index';
- private $comUrl = 'http://v.juhe.cn/exp/com';
- public function logistics($order)
- {
- $result = $this->query($order['wuliu_com'],$order['wuliu_number']); //执行查询
- if($result['error_code'] == 0){//查询成功
- $list = $result['result']['list'];
- return json(['data' => $list,'order' => $order,'msg' => '', 'code' => 200]);
- }else{
- echo "获取失败,原因:".$result['reason'];
- return json(['msg' => $result['reason'], 'data' => '', 'code' => 100]);
- }
- }
- /**
- * 返回支持的快递公司公司列表
- * @return array
- */
- public function getComs(){
- $params = 'key='.$this->appkey;
- $content = $this->juhecurl($this->comUrl,$params);
- return $this->_returnArray($content);
- }
- public function query($com,$no){
- $params = array(
- 'key' => $this->appkey,
- 'com' => $com,
- 'no' => $no
- );
- $content = $this->juhecurl($this->queryUrl,$params,1);
- return $this->_returnArray($content);
- }
- /**
- * 将JSON内容转为数据,并返回
- * @param string $content [内容]
- * @return array
- */
- public function _returnArray($content){
- return json_decode($content,true);
- }
- /**
- * 请求接口返回内容
- * @param string $url [请求的URL地址]
- * @param string $params [请求的参数]
- * @param int $ipost [是否采用POST形式]
- * @return string
- */
- public function juhecurl($url,$params=false,$ispost=0){
- $httpInfo = array();
- $ch = curl_init();
- curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
- curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
- curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
- curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
- if( $ispost )
- {
- curl_setopt( $ch , CURLOPT_POST , true );
- curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
- curl_setopt( $ch , CURLOPT_URL , $url );
- }
- else
- {
- if($params){
- curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
- }else{
- curl_setopt( $ch , CURLOPT_URL , $url);
- }
- }
- $response = curl_exec( $ch );
- if ($response === FALSE) {
- //echo "cURL Error: " . curl_error($ch);
- return false;
- }
- $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
- $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
- curl_close( $ch );
- return $response;
- }
- }
|