mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-24 13:59:15 +00:00
Decode xml responses seamlessly
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user