Merge pull request #296 from zachborboa/295

Allow setting a custom XML decoder
This commit is contained in:
Zach Borboa
2016-01-31 04:32:31 -08:00
4 changed files with 79 additions and 4 deletions
+2
View File
@@ -202,6 +202,7 @@ Curl::setCookieJar($cookie_jar)
Curl::setDefaultJsonDecoder()
Curl::setDefaultTimeout()
Curl::setDefaultUserAgent()
Curl::setDefaultXmlDecoder()
Curl::setDigestAuthentication($username, $password = '')
Curl::setHeader($key, $value)
Curl::setJsonDecoder($function)
@@ -212,6 +213,7 @@ Curl::setReferrer($referrer)
Curl::setTimeout($seconds)
Curl::setURL($url, $data = array())
Curl::setUserAgent($user_agent)
Curl::setXmlDecoder($function)
Curl::success($callback)
Curl::unsetHeader($key)
Curl::verbose($on = true, $output=STDERR)
+36 -4
View File
@@ -5,7 +5,7 @@ namespace Curl;
class Curl
{
const VERSION = '4.9.0';
const VERSION = '4.10.0';
const DEFAULT_TIMEOUT = 30;
public static $RFC2616 = array(
@@ -78,6 +78,7 @@ class Curl
private $jsonDecoder = null;
private $jsonPattern = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i';
private $xmlDecoder = null;
private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
/**
@@ -97,6 +98,7 @@ class Curl
$this->id = 1;
$this->setDefaultUserAgent();
$this->setDefaultJsonDecoder();
$this->setDefaultXmlDecoder();
$this->setDefaultTimeout();
$this->setOpt(CURLINFO_HEADER_OUT, true);
$this->setOpt(CURLOPT_HEADERFUNCTION, array($this, 'headerCallback'));
@@ -202,6 +204,7 @@ class Curl
}
$this->options = null;
$this->jsonDecoder = null;
$this->xmlDecoder = null;
}
/**
@@ -689,6 +692,22 @@ class Curl
};
}
/**
* Set Default XML Decoder
*
* @access public
*/
public function setDefaultXmlDecoder()
{
$this->xmlDecoder = function($response) {
$xml_obj = @simplexml_load_string($response);
if (!($xml_obj === false)) {
$response = $xml_obj;
}
return $response;
};
}
/**
* Set Default Timeout
*
@@ -745,6 +764,19 @@ class Curl
}
}
/**
* Set XML Decoder
*
* @access public
* @param $function
*/
public function setXmlDecoder($function)
{
if (is_callable($function)) {
$this->xmlDecoder = $function;
}
}
/**
* Set Opt
*
@@ -958,9 +990,9 @@ class Curl
$response = $json_decoder($response);
}
} elseif (preg_match($this->xmlPattern, $response_headers['Content-Type'])) {
$xml_obj = @simplexml_load_string($response);
if (!($xml_obj === false)) {
$response = $xml_obj;
$xml_decoder = $this->xmlDecoder;
if (is_callable($xml_decoder)) {
$response = $xml_decoder($response);
}
}
}
+19
View File
@@ -2585,4 +2585,23 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertNotEmpty($stderr);
}
public function testXMLDecoder()
{
$data = array(
'key' => 'Content-Type',
'value' => 'text/xml',
);
$test = new Test();
$test->server('xml_with_cdata_response', 'POST', $data);
$this->assertFalse(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
$test = new Test();
$test->curl->setXmlDecoder(function($response) {
return simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
});
$test->server('xml_with_cdata_response', 'POST', $data);
$this->assertTrue(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
}
}
+22
View File
@@ -182,6 +182,28 @@ if ($test == 'http_basic_auth') {
$rss->appendChild($channel);
echo $doc->saveXML();
exit;
} elseif ($test === 'xml_with_cdata_response') {
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>
<rss>
<items>
<item>
<id>1</id>
<ref>33ee7e1eb504b6619c1b445ca1442c21</ref>
<title><![CDATA[The Title]]></title>
<description><![CDATA[The description.]]></description>
<link><![CDATA[https://www.example.com/page.html?foo=bar&baz=wibble#hash]]></link>
</item>
<item>
<id>2</id>
<ref>b5c0b187fe309af0f4d35982fd961d7e</ref>
<title><![CDATA[Another Title]]></title>
<description><![CDATA[Some description.]]></description>
<link><![CDATA[https://www.example.org/image.png?w=1265.73&h=782.26]]></link>
</item>
</items>
</rss>';
exit;
} elseif ($test === 'upload_response') {
$tmp_filename = tempnam('/tmp', 'php-curl-class.');
move_uploaded_file($_FILES['image']['tmp_name'], $tmp_filename);