baseUrl = $baseUrl; $this->headers = $headers; } /** * @param $type * @param $url * @param null $parameter * @param array $headers * @param bool $json * @return null|string */ private function restCall($type, $url, $parameter = null, $headers = [], $json = false) { if (!$url) { Yii::error("Parameter $url cannot be empty"); return null; } $client = new Client(['base_url' => $this->baseUrl]); unset($parameter['files']); try { $requestTimeout = Yii::$app->params['timeout']['request']; if (isset($parameter['request_timeout'])) { $requestTimeout = $parameter['request_timeout']; unset($parameter['request_timeout']); } $key = $type == 'get' ? 'query' : 'body'; $parameter = is_array($parameter) ? ["$key" => $parameter] : []; $headers = (empty($headers)) ? $this->headers : array_merge($this->headers, $headers); $headers['client_user_agent'] = (Yii::$app->request->getIsConsoleRequest()) ? 'console-request' : Yii::$app->request->getUserAgent(); $parameter['headers'] = $headers; $parameter['timeout'] = $requestTimeout; $parameter['connect_timeout'] = Yii::$app->params['timeout']['connection']; if ($json) { $parameter["$key"] = json_encode($parameter["$key"]); $parameter['headers']['content-type'] = 'application/json'; } // This is for Debugging errors $GLOBALS['_API'] = ['url' => $url, 'parameter' => $parameter]; $this->__request = $client->$type($url, $parameter); // $this->__request->addHeader('Accept-Encoding', 'compress, gzip'); return (string)$this->__request->getBody(); } catch (ServerException $e) { Yii::error('Server error on API:::' . print_r($e->getMessage(), true)); $this->responseCode = $e->getResponse()->getStatusCode(); $this->response = $e->getResponse(); return null; } catch (ClientException $e) { if ($e->getResponse()->getStatusCode() != '404') { Yii::error('Client error on API:::' . print_r($e->getMessage(), true)); } $this->responseCode = $e->getResponse()->getStatusCode(); $this->response = $e->getResponse(); return null; } catch (RequestException $e) { Yii::error('Request error on API:::' . print_r($e->getMessage(), true)); return null; } catch (\Exception $e) { Yii::error('Error on API:::' . print_r($e->getMessage(), true)); return null; } } /** * Make a Http GET call to babase on url + uri * * @param string $uri Uri to call base on base url * @param array $parameter Model's attribute * @param int $cacheDuration * @param bool $json * @param array $headers * * @return array array('error' => null, 'result' => null) array Error or result */ private function get($uri, $parameter = null, $cacheDuration = null, $json = false, $headers = []) { $this->cacheDuration = $cacheDuration; return $this->restCall('get', $uri, $parameter, $headers, $json); } /** * Make a Http POST call to babase on url + uri * * @param string $uri Uri to call base on base url * @param array $parameter Model's attribute * @param bool $json * @param array $headers * @internal param array $header * * @return array array('error' => null, 'result' => null) array Error or result */ private function post($uri, $parameter = null, $json = false, $headers = []) { return $this->restCall('post', $uri, $parameter, $headers, $json); } /** * Make a Http PUT call to babase on url + uri * * @param string $uri Uri to call base on base url * @param array $parameter Model's attribute * * @return array array('error' => null, 'result' => null) array Error or result */ private function put($uri, $parameter = null) { return $this->restCall('put', $uri, $parameter); } /** * Make a Http DELETE call base on base url + uri * * @param string $uri Uri to call base on base url * @param array $parameter Model's attribute * * @return array array('error' => null, 'result' => null) array Error or result */ private function delete($uri, $parameter = null) { return $this->restCall('delete', $uri, $parameter); } /** * @param $value */ public function setBaseUrl($value) { $this->baseUrl = $value; } /** * @return string */ public function getBaseUrl() { return $this->baseUrl; } // this exists for technical reasons. only allow get,put,post,delete functions through // these functions are not public so that any inherited class can implement its own __call // without having to override each and every function /** * @param string $name * @param array $args * @return mixed|null */ public function __call($name, $args) { switch ($name) { case 'get': case 'put': case 'post': case 'delete': return call_user_func_array([$this, $name], $args); break; // will never get here :) default: trigger_error('Method does not exists', E_ERROR); } return null; } /** * @param $headers */ public function setDefaultHeaders($headers) { $this->headers = $headers; } }