Logistics.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\api\model;
  3. use think\Model;
  4. /*
  5. * 查看物流
  6. */
  7. class Logistics extends Model
  8. {
  9. private $appkey = "3be319b58697756c60b5bb090ee9edf5"; //申请的快递查询APPKEY
  10. private $queryUrl = 'http://v.juhe.cn/exp/index';
  11. private $comUrl = 'http://v.juhe.cn/exp/com';
  12. public function logistics($order)
  13. {
  14. $result = $this->query($order['wuliu_com'],$order['wuliu_number']); //执行查询
  15. if($result['error_code'] == 0){//查询成功
  16. $list = $result['result']['list'];
  17. return json(['data' => $list,'order' => $order,'msg' => '', 'code' => 200]);
  18. }else{
  19. echo "获取失败,原因:".$result['reason'];
  20. return json(['msg' => $result['reason'], 'data' => '', 'code' => 100]);
  21. }
  22. }
  23. /**
  24. * 返回支持的快递公司公司列表
  25. * @return array
  26. */
  27. public function getComs(){
  28. $params = 'key='.$this->appkey;
  29. $content = $this->juhecurl($this->comUrl,$params);
  30. return $this->_returnArray($content);
  31. }
  32. public function query($com,$no){
  33. $params = array(
  34. 'key' => $this->appkey,
  35. 'com' => $com,
  36. 'no' => $no
  37. );
  38. $content = $this->juhecurl($this->queryUrl,$params,1);
  39. return $this->_returnArray($content);
  40. }
  41. /**
  42. * 将JSON内容转为数据,并返回
  43. * @param string $content [内容]
  44. * @return array
  45. */
  46. public function _returnArray($content){
  47. return json_decode($content,true);
  48. }
  49. /**
  50. * 请求接口返回内容
  51. * @param string $url [请求的URL地址]
  52. * @param string $params [请求的参数]
  53. * @param int $ipost [是否采用POST形式]
  54. * @return string
  55. */
  56. public function juhecurl($url,$params=false,$ispost=0){
  57. $httpInfo = array();
  58. $ch = curl_init();
  59. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  60. curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
  61. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
  62. curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
  63. curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  64. if( $ispost )
  65. {
  66. curl_setopt( $ch , CURLOPT_POST , true );
  67. curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  68. curl_setopt( $ch , CURLOPT_URL , $url );
  69. }
  70. else
  71. {
  72. if($params){
  73. curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  74. }else{
  75. curl_setopt( $ch , CURLOPT_URL , $url);
  76. }
  77. }
  78. $response = curl_exec( $ch );
  79. if ($response === FALSE) {
  80. //echo "cURL Error: " . curl_error($ch);
  81. return false;
  82. }
  83. $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  84. $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  85. curl_close( $ch );
  86. return $response;
  87. }
  88. }