|
| 1 | +<?php |
| 2 | +class JdClient |
| 3 | +{ |
| 4 | + public $serverUrl = "https://api.jd.com/routerjson"; |
| 5 | + public $accessToken; |
| 6 | + public $connectTimeout = 0; |
| 7 | + public $readTimeout = 0; |
| 8 | + public $appKey; |
| 9 | + public $appSecret; |
| 10 | + public $version = "2.0"; |
| 11 | + public $format = "json"; |
| 12 | + private $charset_utf8 = "UTF-8"; |
| 13 | + private $json_param_key = "360buy_param_json"; |
| 14 | + |
| 15 | + protected function generateSign($params) |
| 16 | + { |
| 17 | + ksort($params); |
| 18 | + $stringToBeSigned = $this->appSecret; |
| 19 | + foreach ($params as $k => $v) { |
| 20 | + if("@" != substr($v, 0, 1)) { |
| 21 | + $stringToBeSigned .= "$k$v"; |
| 22 | + } |
| 23 | + } |
| 24 | + unset($k, $v); |
| 25 | + $stringToBeSigned .= $this->appSecret; |
| 26 | + return strtoupper(md5($stringToBeSigned)); |
| 27 | + } |
| 28 | + |
| 29 | + public function curl($url, $postFields = null) |
| 30 | + { |
| 31 | + $ch = curl_init(); |
| 32 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 33 | + curl_setopt($ch, CURLOPT_FAILONERROR, false); |
| 34 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 35 | + if ($this->readTimeout) { |
| 36 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
| 37 | + } |
| 38 | + if ($this->connectTimeout) { |
| 39 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
| 40 | + } |
| 41 | + //https 请求 |
| 42 | + if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
| 43 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 44 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 45 | + } |
| 46 | + |
| 47 | + if (is_array($postFields) && 0 < count($postFields)) { |
| 48 | + $postBodyString = ""; |
| 49 | + $postMultipart = false; |
| 50 | + foreach ($postFields as $k => $v) { |
| 51 | + if ("@" != substr($v, 0, 1)) { // 判断是不是文件上传 |
| 52 | + $postBodyString .= "$k=" . urlencode($v) . "&"; |
| 53 | + } else { //文件上传用multipart/form-data,否则用www-form-urlencoded |
| 54 | + $postMultipart = true; |
| 55 | + } |
| 56 | + } |
| 57 | + unset($k, $v); |
| 58 | + curl_setopt($ch, CURLOPT_POST, true); |
| 59 | + if ($postMultipart) { |
| 60 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
| 61 | + } else { |
| 62 | + curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
| 63 | + } |
| 64 | + } |
| 65 | + $reponse = curl_exec($ch); |
| 66 | + |
| 67 | + if (curl_errno($ch)) { |
| 68 | + throw new \Exception(curl_error($ch),0); |
| 69 | + } else { |
| 70 | + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 71 | + if (200 !== $httpStatusCode) { |
| 72 | + throw new Exception($reponse,$httpStatusCode); |
| 73 | + } |
| 74 | + } |
| 75 | + curl_close($ch); |
| 76 | + return $reponse; |
| 77 | + } |
| 78 | + |
| 79 | + public function execute($request, $access_token = null) |
| 80 | + { |
| 81 | + //组装系统参数 |
| 82 | + $sysParams["app_key"] = $this->appKey; |
| 83 | + $sysParams["v"] = $this->version; |
| 84 | + $sysParams["method"] = $request->getApiMethodName(); |
| 85 | + $sysParams["timestamp"] = date("Y-m-d H:i:s"); |
| 86 | + if (null != $access_token) { |
| 87 | + $sysParams["access_token"] = $access_token; |
| 88 | + } |
| 89 | + |
| 90 | + //获取业务参数 |
| 91 | + $apiParams = $request->getApiParas(); |
| 92 | + $sysParams[$this->json_param_key] = $apiParams; |
| 93 | + |
| 94 | + //签名 |
| 95 | + $sysParams["sign"] = $this->generateSign($sysParams); |
| 96 | + //系统参数放入GET请求串 |
| 97 | + $requestUrl = $this->serverUrl . "?"; |
| 98 | + foreach ($sysParams as $sysParamKey => $sysParamValue) { |
| 99 | + $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; |
| 100 | + } |
| 101 | + //发起HTTP请求 |
| 102 | + try { |
| 103 | + $resp = $this->curl($requestUrl, $apiParams); |
| 104 | + } catch (Exception $e) { |
| 105 | + $result->code = $e->getCode(); |
| 106 | + $result->msg = $e->getMessage(); |
| 107 | + return $result; |
| 108 | + } |
| 109 | + |
| 110 | + //解析JD返回结果 |
| 111 | + $respWellFormed = false; |
| 112 | + if ("json" == $this->format) { |
| 113 | + $respObject = json_decode($resp, true); |
| 114 | + if (null !== $respObject) { |
| 115 | + $respWellFormed = true; |
| 116 | + foreach ($respObject as $propKey => $propValue) { |
| 117 | + $respObject = $propValue; |
| 118 | + } |
| 119 | + } |
| 120 | + } else if("xml" == $this->format) { |
| 121 | + $respObject = @simplexml_load_string($resp); |
| 122 | + if (false !== $respObject) { |
| 123 | + $respWellFormed = true; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
| 128 | + if (false === $respWellFormed) { |
| 129 | + $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
| 130 | + $result->code = 0; |
| 131 | + $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
| 132 | + return $result; |
| 133 | + } |
| 134 | + |
| 135 | + return $respObject; |
| 136 | + } |
| 137 | + |
| 138 | + public function exec($paramsArray) |
| 139 | + { |
| 140 | + if (!isset($paramsArray["method"])) { |
| 141 | + trigger_error("No api name passed"); |
| 142 | + } |
| 143 | + $inflector = new LtInflector; |
| 144 | + $inflector->conf["separator"] = "."; |
| 145 | + $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request"; |
| 146 | + if (!class_exists($requestClassName)) { |
| 147 | + trigger_error("No such api: " . $paramsArray["method"]); |
| 148 | + } |
| 149 | + |
| 150 | + $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null; |
| 151 | + |
| 152 | + $req = new $requestClassName; |
| 153 | + foreach($paramsArray as $paraKey => $paraValue) |
| 154 | + { |
| 155 | + $inflector->conf["separator"] = "_"; |
| 156 | + $setterMethodName = $inflector->camelize($paraKey); |
| 157 | + $inflector->conf["separator"] = "."; |
| 158 | + $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
| 159 | + if (method_exists($req, $setterMethodName)) { |
| 160 | + $req->$setterMethodName($paraValue); |
| 161 | + } |
| 162 | + } |
| 163 | + return $this->execute($req, $session); |
| 164 | + } |
| 165 | +} |
0 commit comments