public function testLogErrorRequest() { $response = new ConsoleResponse('http://url.com/', 'POST', ['mykey1' => 'asdsd'], ['mykey2' => 'wegewg'], ['Content-Type' => 'text']); $response->logError(503, 'err message', 21); $this->assertEquals(503, $response->getErrorNumber()); $this->assertEquals('err message', $response->getErrorMessage()); $this->assertEquals(21, $response->getResponseTime()); $this->assertTrue($response->isError()); }
/** * Make request to API url * * @param string $url * @param string $method * @param array $values * @param string|null $token * * @return ConsoleResponse */ public function makeRequest($url, $method, array $values, $token = null) { list($postFields, $getFields, $cookieFields, $rawPost, $putFields) = $this->processValues($values); $postFields = $this->normalizeValues($postFields); $getFields = $this->normalizeValues($getFields); $putFields = $this->normalizeValues($putFields); if (count($getFields)) { $parts = []; foreach ($getFields as $key => $value) { $parts[] = "{$key}={$value}"; } $url = $url . '?' . implode('&', $parts); } $putRawPost = null; if (count($putFields)) { $parts = []; foreach ($putFields as $key => $value) { $parts[] = "{$key}={$value}"; } $putRawPost = implode('&', $parts); } $startTime = microtime(true); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_NOBODY, false); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_VERBOSE, false); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_HEADER, true); if (count($postFields)) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields); } if ($rawPost) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $rawPost); } if ($putRawPost) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $putRawPost); } if (count($cookieFields)) { $parts = []; foreach ($cookieFields as $key => $value) { $parts[] = "{$key}={$value}"; } curl_setopt($curl, CURLOPT_HTTPHEADER, ["Cookie: " . implode('&', $parts)]); } curl_setopt($curl, CURLOPT_TIMEOUT, 30); $headers = []; if ($token !== null && $token !== false) { $headers = ['Authorization: Bearer ' . $token]; curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } $consoleResponse = new ConsoleResponse($url, $method, $postFields, $getFields, $cookieFields, $headers, $rawPost); $response = curl_exec($curl); $elapsed = intval((microtime(true) - $startTime) * 1000); $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $responseHeaders = substr($response, 0, $headerSize); $responseBody = substr($response, $headerSize); $curlErrorNumber = curl_errno($curl); $curlError = curl_error($curl); if ($curlErrorNumber > 0) { $consoleResponse->logError($curlErrorNumber, $curlError, $elapsed); } else { $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); $consoleResponse->logRequest($httpCode, $responseBody, $responseHeaders, $elapsed); } return $consoleResponse; }