config = $config; } /** * @param $app * @return $this */ public function app($app) { if (isset($this->config[$app])) $this->currentApp = $app; return $this; } /** * 调用api,如果状态码不为200则抛出异常 * @param $uri * @param $params * @return mixed * @throws \Exception */ public function call($uri, $params) { $config = array_merge(['timeout' => 1], $this->config[$this->currentApp]); unset($config['secret']); $client = new \GuzzleHttp\Client($config); $params['appid'] = $this->currentApp; $params['timestamp'] = time(); $params['sign'] = $this->sign($params); $resp = $client->post($uri, ['form_params' => $params]); if ($resp->getStatusCode() == 200) { return \GuzzleHttp\json_decode($resp->getBody(), true); } else { throw new \Exception('request failed'); } } protected function sign($params) { $key = $this->config[$this->currentApp]['secret']; unset($params['sign']); ksort($params); $str = http_build_query($params, null, '&'); return md5($str . $key); } }