/** * Store an access token for a amount of time * * @param string $access_token * @param int $seconds * @param array $param * * @return bool Successfully store or not */ public function storeAccessTokenForSeconds($access_token, $seconds, array $param = array()) { $time = time() + (int) $seconds - 1; if ($this->save($access_token, $time) === true) { $this->logger->info(__CLASS__, 'store', sprintf('Access token stored for %ss', $seconds)); } // if cannot save, an exception will be raised return true; }
/** * @param string $url * @param string $method * @param string|null $access_token * @param string|array $parameters * @param string $contentType * * @return array */ private function doApiCall($url, $method, $access_token = null, $parameters = array(), $contentType = 'text/xml') { $request = array(); $headers = array(); $request[CURLOPT_TIMEOUT] = (int) $this->http_timeout; $request[CURLOPT_USERAGENT] = str_replace('%VERSION%', Client::VERSION, $this->http_user_agent); $request[CURLOPT_CUSTOMREQUEST] = $method; if (!empty($parameters)) { if ($method === 'GET') { $url = Tools::httpBuildUrl($url, array("query" => http_build_query($parameters)), Tools::HTTP_URL_JOIN_QUERY); } else { if (is_array($parameters)) { $request[CURLOPT_POSTFIELDS] = http_build_query($parameters); } else { if (is_string($parameters)) { $request[CURLOPT_POSTFIELDS] = $parameters; } } } } $request[CURLOPT_URL] = $url; if (!is_null($contentType)) { $headers[] = "Content-Type: {$contentType}"; } if (!is_null($access_token)) { $headers[] = 'Authorization: Bearer ' . $access_token; } if (!empty($this->http_proxy_host)) { $request[CURLOPT_PROXY] = $this->http_proxy_host; if (!empty($this->http_proxy_port)) { $request[CURLOPT_PROXYPORT] = $this->http_proxy_port; } if (!empty($this->http_proxy_type)) { $request[CURLOPT_PROXYTYPE] = $this->http_proxy_type; } if (!empty($this->http_proxy_auth)) { $request[CURLOPT_PROXYAUTH] = $this->http_proxy_auth; } if (!empty($this->http_proxy_user)) { $request[CURLOPT_PROXYUSERPWD] = $this->http_proxy_user . ':' . $this->http_proxy_pass; } } $request[CURLOPT_HTTPHEADER] = $headers; $this->logger->info(__CLASS__, 'api', sprintf('%s %s', $method, $url)); $start = microtime(true); @(list($result, $status_code, $error, $errno) = $this->execCurl($request)); $end = microtime(true); $duration = (int) round(($end - $start) * 1000); if ($errno === 0) { $return = array('http_code' => $status_code, 'http_body' => $result, 'duration' => $duration); if ($status_code >= 400) { $this->logger->error(__CLASS__, 'api', sprintf('Response HTTP code %s, body length %s bytes, duration %sms on endpoint %s %s', $status_code, strlen($result), $duration, $method, $url)); } else { if ($status_code >= 300) { $this->logger->warning(__CLASS__, 'api', sprintf('Response HTTP code %s, body length %s bytes, duration %sms on endpoint %s %s', $status_code, strlen($result), $duration, $method, $url)); } else { $this->logger->info(__CLASS__, 'api', sprintf('Response HTTP code %s, body length %s bytes, duration %sms', $status_code, strlen($result), $duration)); } } } else { $return = array('error_msg' => $error, 'error_num' => $errno, 'duration' => $duration); $this->logger->error(__CLASS__, 'api', sprintf('cURL error #%s : %s', $errno, $error)); } return $return; }