Aliyun.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\library\delivery\aliyun;
  10. class Aliyun {
  11. private static $host = "https://wuliu.market.alicloudapi.com"; //api访问链接
  12. public static $errorInfo = [
  13. 201=>"快递单号错误",
  14. 203=>"快递公司不存在",
  15. 204=>"快递公司识别失败",
  16. 205=>"没有信息",
  17. 207=>"IP限制",
  18. 0=>"正常"
  19. ];
  20. public static function query($no="",$type=""){
  21. $config = self::getConfig();
  22. if(empty($config["AppKey"])){
  23. throw new \Exception("参数AppKey不能为空",0);
  24. }else if(empty($config["AppSecret"])){
  25. throw new \Exception("参数AppSecret不能为空",0);
  26. }else if(empty($config["AppCode"])){
  27. throw new \Exception("参数AppCode不能为空",0);
  28. }else if(empty($no)){
  29. throw new \Exception("物流单号不能为空",0);
  30. }
  31. $headers = [];
  32. array_push($headers, "Authorization:APPCODE " . $config["AppCode"]);
  33. $querys = "no=".$no;
  34. if(!empty($type)){
  35. $querys .= "&type=" . $type;
  36. }
  37. $url = self::$host . "/kdi" . "?" . $querys;
  38. $result = self::get($url,$headers);
  39. list($header, $body) = explode("\r\n\r\n", $result["data"], 2);
  40. if ($result["code"] == 200) {
  41. $array = json_decode($body,true);
  42. return [
  43. "expName"=>$array["result"]["expName"],
  44. "number"=>$array["result"]["number"],
  45. "takeTime"=>$array["result"]["takeTime"],
  46. "updateTime"=>$array["result"]["updateTime"],
  47. "list"=>$array["result"]["list"],
  48. ];
  49. } else {
  50. if ($result["code"] == 400 && strpos($header, "Invalid Param Location") !== false) {
  51. throw new \Exception("参数错误",$result["code"]);
  52. } elseif ($result["code"] == 400 && strpos($header, "Invalid AppCode") !== false) {
  53. throw new \Exception("AppCode错误",$result["code"]);
  54. } elseif ($result["code"] == 400 && strpos($header, "Invalid Url") !== false) {
  55. throw new \Exception("请求的 Method、Path 或者环境错误",$result["code"]);
  56. } elseif ($result["code"] == 403 && strpos($header, "Unauthorized") !== false) {
  57. throw new \Exception("服务未被授权(或URL和Path不正确)",$result["code"]);
  58. } elseif ($result["code"] == 403 && strpos($header, "Quota Exhausted") !== false) {
  59. throw new \Exception("套餐包次数用完",$result["code"]);
  60. } elseif ($result["code"] == 500) {
  61. throw new \Exception("API网关错误",$result["code"]);
  62. } elseif ($result["code"] == 0) {
  63. throw new \Exception("URL错误",$result["code"]);
  64. } else {
  65. throw new \Exception("参数名错误 或 其他错误",$result["code"]);
  66. }
  67. }
  68. }
  69. public static function get($url,$headers){
  70. $curl = curl_init();
  71. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
  72. curl_setopt($curl, CURLOPT_URL, $url);
  73. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  74. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  75. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  76. curl_setopt($curl, CURLOPT_HEADER, true);
  77. if (1 == strpos("$" . self::$host, "https://")) {
  78. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  79. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  80. }
  81. $data = curl_exec($curl);
  82. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  83. return [
  84. "data"=>$data,
  85. "code"=>$httpCode
  86. ];
  87. }
  88. public static function getConfig(){
  89. $path = dirname(__FILE__) . '/config.php';
  90. if(file_exists($path)){
  91. $config = include $path;
  92. }else{
  93. $config = [
  94. "AppKey"=>"",
  95. "AppSecret"=>"",
  96. "AppCode"=>""
  97. ];
  98. }
  99. return $config;
  100. }
  101. public static function setConfig($data=[]){
  102. if(empty($data)){
  103. return false;
  104. }
  105. $path = dirname(__FILE__) . '/config.php';
  106. return file_put_contents($path,"<?php " . PHP_EOL . "return " . var_export($data,true) . ';' . PHP_EOL . '?>');
  107. }
  108. }