Decode xml responses seamlessly

This commit is contained in:
Zach Borboa
2014-04-13 15:20:21 -07:00
parent 24a93bdc51
commit 3656fdfa62
3 changed files with 53 additions and 0 deletions
+6
View File
@@ -350,6 +350,12 @@ class Curl
if (!is_null($json_obj)) {
$ch->response = $json_obj;
}
} elseif (preg_match('/^application\/xml/i', $ch->response_headers['Content-Type']) ||
preg_match('/^text\/xml/i', $ch->response_headers['Content-Type'])) {
$xml_obj = @simplexml_load_string($ch->response);
if (!($xml_obj === false)) {
$ch->response = $xml_obj;
}
}
}
}
+25
View File
@@ -421,6 +421,31 @@ class CurlTest extends PHPUnit_Framework_TestCase {
assertion('CONTENT-TYPE', 'APPLICATION/JSON');
}
public function testXMLResponse() {
function xml_assertion($key, $value) {
$test = new Test();
$test->server('xml_response', 'POST', array(
'key' => $key,
'value' => $value,
));
PHPUnit_Framework_Assert::assertInstanceOf('SimpleXMLElement', $test->curl->response);
}
xml_assertion('Content-Type', 'application/xml; charset=utf-8');
xml_assertion('content-type', 'application/xml; charset=utf-8');
xml_assertion('Content-Type', 'application/xml');
xml_assertion('content-type', 'application/xml');
xml_assertion('CONTENT-TYPE', 'application/xml');
xml_assertion('CONTENT-TYPE', 'application/xml');
xml_assertion('Content-Type', 'text/xml; charset=utf-8');
xml_assertion('content-type', 'text/xml; charset=utf-8');
xml_assertion('Content-Type', 'text/xml');
xml_assertion('content-type', 'text/xml');
xml_assertion('CONTENT-TYPE', 'text/xml');
xml_assertion('CONTENT-TYPE', 'text/xml');
}
public function testArrayToStringConversion() {
$test = new Test();
$test->server('post', 'POST', array(
+22
View File
@@ -105,6 +105,28 @@ else if ($test === 'json_response') {
));
exit;
}
else if ($test === 'xml_response') {
$key = $_POST['key'];
$value = $_POST['value'];
header($key . ': ' . $value);
$doc = new DOMDocument();
$doc->formatOutput = true;
$rss = $doc->appendChild($doc->createElement('rss'));
$rss->setAttribute('version', '2.0');
$channel = $doc->createElement('channel');
$title = $doc->createElement('title');
$title->appendChild($doc->createTextNode('Title'));
$channel->appendChild($title);
$link = $doc->createElement('link');
$link->appendChild($doc->createTextNode('Link'));
$channel->appendChild($link);
$description = $doc->createElement('description');
$description->appendChild($doc->createTextNode('Description'));
$channel->appendChild($description);
$rss->appendChild($channel);
echo $doc->saveXML();
exit;
}
else if ($test === 'error_message') {
if (function_exists('http_response_code')) {
http_response_code(401);