Skip to content

Commit 85e4109

Browse files
committed
Fix php-curl-class#64: Allow access to raw response content
1 parent eefd659 commit 85e4109

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/Curl/Curl.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Curl
3535
public $request_headers = null;
3636
public $response_headers = null;
3737
public $response = null;
38+
public $raw_response = null;
3839

3940
public function __construct()
4041
{
@@ -311,6 +312,7 @@ private function parseRequestHeaders($raw_headers)
311312
private function parseResponse($response)
312313
{
313314
$response_headers = '';
315+
$raw_response = $response;
314316
if (!(strpos($response, "\r\n\r\n") === false)) {
315317
$response_array = explode("\r\n\r\n", $response);
316318
for ($i = count($response_array) - 1; $i >= 0; $i--) {
@@ -325,6 +327,7 @@ private function parseResponse($response)
325327
list($response_header, $response) = explode("\r\n\r\n", $response, 2);
326328
}
327329
$response_headers = $this->parseResponseHeaders($response_header);
330+
$raw_response = $response;
328331

329332
if (isset($response_headers['Content-Type'])) {
330333
if (preg_match('/^application\/json/i', $response_headers['Content-Type'])) {
@@ -344,7 +347,7 @@ private function parseResponse($response)
344347
}
345348
}
346349

347-
return array($response_headers, $response);
350+
return array($response_headers, $response, $raw_response);
348351
}
349352

350353
private function parseResponseHeaders($raw_headers)
@@ -398,9 +401,9 @@ protected function exec($_ch = null)
398401
$ch = is_null($_ch) ? $this : $_ch;
399402

400403
if ($ch->multi_child) {
401-
$ch->response = curl_multi_getcontent($ch->curl);
404+
$ch->raw_response = curl_multi_getcontent($ch->curl);
402405
} else {
403-
$ch->response = curl_exec($ch->curl);
406+
$ch->raw_response = curl_exec($ch->curl);
404407
$ch->curl_error_code = curl_errno($ch->curl);
405408
}
406409

@@ -412,7 +415,7 @@ protected function exec($_ch = null)
412415
$ch->error_code = $ch->error ? ($ch->curl_error ? $ch->curl_error_code : $ch->http_status_code) : 0;
413416

414417
$ch->request_headers = $this->parseRequestHeaders(curl_getinfo($ch->curl, CURLINFO_HEADER_OUT));
415-
list($ch->response_headers, $ch->response) = $this->parseResponse($ch->response);
418+
list($ch->response_headers, $ch->response, $ch->raw_response) = $this->parseResponse($ch->raw_response);
416419

417420
$ch->http_error_message = '';
418421
if ($ch->error) {

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,15 @@ function assertion($key, $value)
541541
PHPUnit_Framework_Assert::assertTrue(is_float($response->float));
542542
PHPUnit_Framework_Assert::assertEmpty($response->empty);
543543
PHPUnit_Framework_Assert::assertTrue(is_string($response->string));
544+
PHPUnit_Framework_Assert::assertEquals($test->curl->raw_response, json_encode(array(
545+
'null' => null,
546+
'true' => true,
547+
'false' => false,
548+
'integer' => 1,
549+
'float' => 3.14,
550+
'empty' => '',
551+
'string' => 'string',
552+
)));
544553
}
545554

546555
assertion('Content-Type', 'APPLICATION/JSON');
@@ -576,6 +585,24 @@ function xmlAssertion($key, $value)
576585
));
577586

578587
PHPUnit_Framework_Assert::assertInstanceOf('SimpleXMLElement', $test->curl->response);
588+
589+
$doc = new DOMDocument();
590+
$doc->formatOutput = true;
591+
$rss = $doc->appendChild($doc->createElement('rss'));
592+
$rss->setAttribute('version', '2.0');
593+
$channel = $doc->createElement('channel');
594+
$title = $doc->createElement('title');
595+
$title->appendChild($doc->createTextNode('Title'));
596+
$channel->appendChild($title);
597+
$link = $doc->createElement('link');
598+
$link->appendChild($doc->createTextNode('Link'));
599+
$channel->appendChild($link);
600+
$description = $doc->createElement('description');
601+
$description->appendChild($doc->createTextNode('Description'));
602+
$channel->appendChild($description);
603+
$rss->appendChild($channel);
604+
$xml = $doc->saveXML();
605+
PHPUnit_Framework_Assert::assertEquals($test->curl->raw_response, $xml);
579606
}
580607

581608
xmlAssertion('Content-Type', 'application/atom+xml; charset=UTF-8');

0 commit comments

Comments
 (0)