From 3656fdfa625bc75e50f73cb72737f5dc1806e5fa Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 13 Apr 2014 15:20:21 -0700 Subject: [PATCH] Decode xml responses seamlessly --- src/Curl.class.php | 6 ++++++ tests/PHPCurlClass/PHPCurlClassTest.php | 25 +++++++++++++++++++++++++ tests/PHPCurlClass/server.php | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/Curl.class.php b/src/Curl.class.php index 26209ba..b0346ba 100644 --- a/src/Curl.class.php +++ b/src/Curl.class.php @@ -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; + } } } } diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index c42bea0..76317f7 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -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( diff --git a/tests/PHPCurlClass/server.php b/tests/PHPCurlClass/server.php index 96e835b..cb1e51d 100644 --- a/tests/PHPCurlClass/server.php +++ b/tests/PHPCurlClass/server.php @@ -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);