function testRESTResponse() { parent::$logger->debug('testRESTResponse'); $curl_exec = TestData::getCurlExec(); $curl_getinfo = TestData::getCurlInfo(); $resp = new MLPHP\RESTResponse(); $resp->setBody($curl_exec); $resp->setInfo($curl_getinfo); $this->assertEquals($resp->getUrl(), $curl_getinfo['url']); $this->assertEquals($resp->getContentType(), $curl_getinfo['content_type']); $this->assertEquals($resp->getHttpCode(), $curl_getinfo['http_code']); $this->assertEquals($resp->getRedirectUrl(), $curl_getinfo['redirect_url']); }
/** * Execute a cURL request. * * @todo Handle more response codes * * @param resource ch The REST URL string (example: 'documents') * @return RESTResponse A RESTResponse object. */ protected function execute(&$ch) { $response = new RESTResponse(); $curl_exec = curl_exec($ch); // print('******* START $curl_exec (body) *******' . PHP_EOL); // print_r($curl_exec); // print('******* END $curl_exec (body) *******' . PHP_EOL); $response->setBody($curl_exec); $curl_getinfo = curl_getinfo($ch); // print('******* START $curl_getinfo *******' . PHP_EOL); // print_r($curl_getinfo); // print('******* END $curl_getinfo *******' . PHP_EOL); $response->setInfo($curl_getinfo); //print_r($response); $this->logger->debug("Response code: " . $response->getHttpCode()); if ($response->getHttpCode() === 0) { curl_close($ch); throw new \Exception('No connection: ' . $response->getUrl(), $response->getHttpCode()); } else { if ($response->getHttpCode() >= 400) { curl_close($ch); $this->logger->debug("HTTP Error " . $response->getHttpCode()); throw new \Exception($response->getErrorMessage(), $response->getHttpCode()); } else { curl_close($ch); // print('***** RESPONSE *****' . PHP_EOL); // print_r($response); return $response; } } }