app_id = $options['app_id']; $this->app_key = $options['app_key']; $this->api = $options['api_url'] ?? $this->api; return $this; } /** * 快递查询. * * @param string $tracking_code 快递单号 * @param string $shipping_code 物流公司编号 * @return string * * @throws InvalidArgumentException * @throws HttpException */ public function track($tracking_code = '', $shipping_code = '', $additional = []) { if (empty($tracking_code)) { throw new InvalidArgumentException('TrackingCode is required'); } if (empty($shipping_code)) { throw new InvalidArgumentException('ShippingCode is required'); } $requestData = [ 'com' => $tracking_code, 'nu' => $shipping_code ]; if (!empty($additional)) { $requestData = array_merge($requestData, $additional); } $requestData = \json_encode($requestData); $post_data = [ 'com' => $shipping_code, 'nu' => $tracking_code, 'phone' => '', // 手机号 ]; if (!empty($additional)) { $data = array_merge($data, $additional); } $post_data['showapi_appid'] = $this->app_id; $post_data['showapi_sign'] = $this->app_key; try { $response = $this->getHttpClient()->request('POST', $this->api.'-3', [ 'form_params' => $post_data, ])->getBody()->getContents(); } catch (GuzzleException $e) { throw new HttpException($e->getMessage(), $e->getCode(), $e); } return (new ResultBackVo())->ShowApi(\json_decode($response)); } /** * 数据签名. * * @param $data * @param $apikey * * @return string */ private function encrypt($data, $apikey) { return urlencode(base64_encode(md5($data.$apikey))); } /** * @return Client */ public function getHttpClient() { return new Client($this->guzzleOptions); } /** * @return Client */ public function getGuzzleOptions() { return new Client($this->guzzleOptions); } /** * @param $options */ public function setGuzzleOptions($options) { $this->guzzleOptions = $options; return $this; } }