/** * @param $query_method string Request method name (GET, POST, PUT, DELETE, etc...) * @param $method_name string Api method name to be called * @param $params array Additional args which will be passed as request body * @return mixed * @throws \Exception */ public function call($query_method, $method_name, $body = [], $params = []) { $secure = $this->getAttr($params, 'secure', true); $retry_times = $this->getAttr($params, 'retry_times', 3); $query_params = array_merge($this->request_args, $body); if ($secure) { $query_params['app_id'] = $this->app_id; $query_params['hash'] = HashBuilder::build($query_params, $this->secret); } $json_str = json_encode($query_params); if ($this->debug) { var_dump($json_str); ob_flush(); } for ($i = 0; $i < $retry_times; $i++) { $curl = curl_init(implode('/', [$this->host, $this->version, $method_name])); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($query_method)); curl_setopt($curl, CURLOPT_POSTFIELDS, $json_str); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT_MS, 5000); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: ' . strlen($json_str)]); foreach ($this->curlOpts as $p => $v) { curl_setopt($curl, $p, $v); } $response = curl_exec($curl); $response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); switch ($response_code) { case 401: try { $deserialized = Json::decode($response); if (array_key_exists('status', $deserialized)) { throw ApiCallBaseException::create($deserialized); } } catch (InvalidParamException $e) { $this->curlOpts[CURLOPT_USERPWD] = implode(':', $this->httpAuth); } break; case 200: return $response; default: throw new \Exception('Request ended with HTTP code: ' . $response_code . "; Response: " . $response, $response_code); } } throw new \Exception('Request error!', 500); }
public function testCorrectHash() { $expected_hash = '181de8b0f26ee8866c0f8660c7427c74042abcc928e31f60ec7ead3f07c88db9'; $this->assertEquals($expected_hash, HashBuilder::build(['app_id' => $this->appId], $this->secret)); }