/** * Send request * * @param RequestMessage $requestMessage * * @return array Response data * @throws ApiException * @throws ConnectionException */ public function sendRequest(RequestMessage $requestMessage) { // Initialize curl and set options $curl = curl_init(); $headers = []; curl_setopt($curl, CURLOPT_URL, $requestMessage->getFullUrl()); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $requestMessage->getMethod()); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifyPeer); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADERFUNCTION, function ($curl, $header) use(&$headers) { $parts = explode(': ', $header); $headers[$parts[0]] = isset($parts[1]) ? trim($parts[1]) : null; return strlen($header); }); // Set JSON body if available if ($body = $requestMessage->getPostBody()) { curl_setopt($curl, CURLOPT_POSTFIELDS, $body); } // Get response body and status code $jsonResponse = json_decode(curl_exec($curl)); $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); // Parse headers for call stats $this->processHeaders($headers); // Throw connection exception if no status code or json response if ($jsonResponse === null || $statusCode == 0) { throw new ConnectionException(); } // Throw API exception if unexpected status code if ($statusCode !== 200) { throw new ApiException($jsonResponse->errors[0]); } return $jsonResponse; }
/** * Test: Get full Url * * @covers \Pushy\Transport\RequestMessage::getFullUrl */ public function testGetFullUrl() { $this->requestMessage->setQueryParam('test', 'value'); $this->assertEquals(RequestMessage::API_DOMAIN . '?test=value', $this->requestMessage->getFullUrl()); }