secretKey = $secretKey; $this->type = $type; $this->format = $format; } public function getHttpClient() { return new Client($this->guzzleOptions); } public function setGuzzleOptions(array $options) { $this->guzzleOptions = $options; } /** * @param $params * @param $api * @return mixed|string * @throws GuzzleException * @throws HttpException * @throws InvalidArgumentException */ public function baseFun($params, $api) { $url = $this->apiUrl . $api; if (!in_array(strtolower($this->format), ['xml', 'json'])) { throw new InvalidArgumentException('Invalid response format: ' . $this->format); } if (!in_array(strtolower($this->type), ['base', 'all'])) { throw new InvalidArgumentException('Invalid type value(base/all): ' . $this->type); } // 附加参数 $params['times'] = time(); $params['output'] = $this->format; $params['extensions'] = $this->type; $signature = (new Sign())->generateHmacSignature($this->secretKey, $params); $params = [ 'headers' => ['content-type' => 'application/json', 'accept' => 'application/json'], 'body' => json_encode([ 'signature' => $signature, 'data' => $params ]), 'http_errors' => false ]; try { $response = $this->getHttpClient() ->request('POST', $url, $params) ->getBody() ->getContents(); return 'json' === $this->format ? json_decode($response, true) : $response; } catch (\Exception $e) { throw new HttpException($e->getMessage(), $e->getCode(), $e); } } /** * 获取工单信息 * @param $order_sn * @return mixed|string * @throws GuzzleException * @throws HttpException * @throws InvalidArgumentException */ public function getProductOrder($order_sn) { $data = [ 'order_sn' => $order_sn ]; return $this->baseFun($data, 'production.status'); } }