public function post($url, $getParameters = array(), $postParameters, $isJson = false, $curlOptions = array()) { $curl = new Curl(); $curl->setUrl($url, $getParameters); $curl->setMethod(true); if ($isJson) { $postParameters = json_encode($postParameters); $curl->addOption('HTTP_HEADER', array('Content-Type: application/json')); } $curl->setPostParameters($postParameters); foreach ($curlOptions as $key => $value) { $curl->addOption($key, $value); } return $this->send($curl, $isJson); }
private function sendRequest($method, $data = array()) { $data = array_merge(compact('method'), $data); $cache_key = http_build_query($data); // to cache all params except api_key $data['key'] = Configure::read('TechDocApi.key'); if ($method !== 'search_articles' && $method !== 'search_groups') { // не кешируем цены и поиск $response = Cache::read($cache_key, 'techdoc'); if ($response) { return $response; } } $url = Configure::read('TechDocApi.url') . '?' . http_build_query($data); $curl = new Curl($url); $curl->setOption(CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) 2016"); $curl->setParam('_server', json_encode($_SERVER)); $this->writeLog('REQUEST', 'URL: ' . $url . ' DATA: ' . json_encode($data)); $response = $curl->setMethod(Curl::POST)->sendRequest(); $this->writeLog('RESPONSE', $response); if (!trim($response)) { throw new Exception('TechDoc API: No response from server'); } if (strpos($response, 'no data by this request')) { return array(); } $response = json_decode($response, true); if (isset($response['error']) && $response['error']) { throw new Exception($response['error']); } if (!$response || !is_array($response)) { throw new Exception('TechDoc API: Bad response from server'); } Cache::write($cache_key, $response, 'techdoc'); return $response; }
private function sendSearchRequest($ses, $vin) { $data = compact('vin', 'ses'); $url = Configure::read('AutoxpApi.search_url') . '&' . http_build_query($data); $cookieFile = Configure::read('AutoxpApi.cookies'); $curl = new Curl($url); if (!TEST_ENV) { // Определяем идет ли это запрос от поискового бота // если бот - перенаправляем на др.прокси-сервера для ботов - снимаем нагрузку с прокси для сайта // чтоб избежать блокировок со стороны сервиса $proxy_type = $this->isBot() ? 'Bot' : 'Site'; $proxy = $this->loadModel('ProxyUse')->getProxy($proxy_type); $this->loadModel('ProxyUse')->useProxy($proxy['ProxyUse']['host']); $curl->setOption(CURLOPT_PROXY, $proxy['ProxyUse']['host'])->setOption(CURLOPT_PROXYUSERPWD, $proxy['ProxyUse']['login'] . ':' . $proxy['ProxyUse']['password']); } $this->writeLog('REQUEST', 'URL: ' . $url . ' DATA: ' . json_encode($data)); $response = $curl->setMethod(Curl::GET)->setOption(CURLOPT_COOKIEFILE, $cookieFile)->setOption(CURLOPT_COOKIEJAR, $cookieFile)->sendRequest(); $this->writeLog('RESPONSE', $response); if (!$response) { throw new Exception('AutoxpApi: No response from server'); } return $response; }