Skip to content

Commit 3656fdf

Browse files
committed
Decode xml responses seamlessly
1 parent 24a93bd commit 3656fdf

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/Curl.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ protected function exec($_ch = null)
350350
if (!is_null($json_obj)) {
351351
$ch->response = $json_obj;
352352
}
353+
} elseif (preg_match('/^application\/xml/i', $ch->response_headers['Content-Type']) ||
354+
preg_match('/^text\/xml/i', $ch->response_headers['Content-Type'])) {
355+
$xml_obj = @simplexml_load_string($ch->response);
356+
if (!($xml_obj === false)) {
357+
$ch->response = $xml_obj;
358+
}
353359
}
354360
}
355361
}

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,31 @@ function assertion($key, $value) {
421421
assertion('CONTENT-TYPE', 'APPLICATION/JSON');
422422
}
423423

424+
public function testXMLResponse() {
425+
function xml_assertion($key, $value) {
426+
$test = new Test();
427+
$test->server('xml_response', 'POST', array(
428+
'key' => $key,
429+
'value' => $value,
430+
));
431+
432+
PHPUnit_Framework_Assert::assertInstanceOf('SimpleXMLElement', $test->curl->response);
433+
}
434+
435+
xml_assertion('Content-Type', 'application/xml; charset=utf-8');
436+
xml_assertion('content-type', 'application/xml; charset=utf-8');
437+
xml_assertion('Content-Type', 'application/xml');
438+
xml_assertion('content-type', 'application/xml');
439+
xml_assertion('CONTENT-TYPE', 'application/xml');
440+
xml_assertion('CONTENT-TYPE', 'application/xml');
441+
xml_assertion('Content-Type', 'text/xml; charset=utf-8');
442+
xml_assertion('content-type', 'text/xml; charset=utf-8');
443+
xml_assertion('Content-Type', 'text/xml');
444+
xml_assertion('content-type', 'text/xml');
445+
xml_assertion('CONTENT-TYPE', 'text/xml');
446+
xml_assertion('CONTENT-TYPE', 'text/xml');
447+
}
448+
424449
public function testArrayToStringConversion() {
425450
$test = new Test();
426451
$test->server('post', 'POST', array(

tests/PHPCurlClass/server.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@
105105
));
106106
exit;
107107
}
108+
else if ($test === 'xml_response') {
109+
$key = $_POST['key'];
110+
$value = $_POST['value'];
111+
header($key . ': ' . $value);
112+
$doc = new DOMDocument();
113+
$doc->formatOutput = true;
114+
$rss = $doc->appendChild($doc->createElement('rss'));
115+
$rss->setAttribute('version', '2.0');
116+
$channel = $doc->createElement('channel');
117+
$title = $doc->createElement('title');
118+
$title->appendChild($doc->createTextNode('Title'));
119+
$channel->appendChild($title);
120+
$link = $doc->createElement('link');
121+
$link->appendChild($doc->createTextNode('Link'));
122+
$channel->appendChild($link);
123+
$description = $doc->createElement('description');
124+
$description->appendChild($doc->createTextNode('Description'));
125+
$channel->appendChild($description);
126+
$rss->appendChild($channel);
127+
echo $doc->saveXML();
128+
exit;
129+
}
108130
else if ($test === 'error_message') {
109131
if (function_exists('http_response_code')) {
110132
http_response_code(401);

0 commit comments

Comments
 (0)