Skip to content

Commit dce9acb

Browse files
committed
Merge pull request php-curl-class#296 from zachborboa/295
Allow setting a custom XML decoder
2 parents 266bc12 + 31c7644 commit dce9acb

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Curl::setCookieJar($cookie_jar)
202202
Curl::setDefaultJsonDecoder()
203203
Curl::setDefaultTimeout()
204204
Curl::setDefaultUserAgent()
205+
Curl::setDefaultXmlDecoder()
205206
Curl::setDigestAuthentication($username, $password = '')
206207
Curl::setHeader($key, $value)
207208
Curl::setJsonDecoder($function)
@@ -212,6 +213,7 @@ Curl::setReferrer($referrer)
212213
Curl::setTimeout($seconds)
213214
Curl::setURL($url, $data = array())
214215
Curl::setUserAgent($user_agent)
216+
Curl::setXmlDecoder($function)
215217
Curl::success($callback)
216218
Curl::unsetHeader($key)
217219
Curl::verbose($on = true, $output=STDERR)

src/Curl/Curl.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class Curl
77
{
8-
const VERSION = '4.9.0';
8+
const VERSION = '4.10.0';
99
const DEFAULT_TIMEOUT = 30;
1010

1111
public static $RFC2616 = array(
@@ -78,6 +78,7 @@ class Curl
7878

7979
private $jsonDecoder = null;
8080
private $jsonPattern = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i';
81+
private $xmlDecoder = null;
8182
private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
8283

8384
/**
@@ -97,6 +98,7 @@ public function __construct($base_url = null)
9798
$this->id = 1;
9899
$this->setDefaultUserAgent();
99100
$this->setDefaultJsonDecoder();
101+
$this->setDefaultXmlDecoder();
100102
$this->setDefaultTimeout();
101103
$this->setOpt(CURLINFO_HEADER_OUT, true);
102104
$this->setOpt(CURLOPT_HEADERFUNCTION, array($this, 'headerCallback'));
@@ -202,6 +204,7 @@ public function close()
202204
}
203205
$this->options = null;
204206
$this->jsonDecoder = null;
207+
$this->xmlDecoder = null;
205208
}
206209

207210
/**
@@ -689,6 +692,22 @@ public function setDefaultJsonDecoder()
689692
};
690693
}
691694

695+
/**
696+
* Set Default XML Decoder
697+
*
698+
* @access public
699+
*/
700+
public function setDefaultXmlDecoder()
701+
{
702+
$this->xmlDecoder = function($response) {
703+
$xml_obj = @simplexml_load_string($response);
704+
if (!($xml_obj === false)) {
705+
$response = $xml_obj;
706+
}
707+
return $response;
708+
};
709+
}
710+
692711
/**
693712
* Set Default Timeout
694713
*
@@ -745,6 +764,19 @@ public function setJsonDecoder($function)
745764
}
746765
}
747766

767+
/**
768+
* Set XML Decoder
769+
*
770+
* @access public
771+
* @param $function
772+
*/
773+
public function setXmlDecoder($function)
774+
{
775+
if (is_callable($function)) {
776+
$this->xmlDecoder = $function;
777+
}
778+
}
779+
748780
/**
749781
* Set Opt
750782
*
@@ -958,9 +990,9 @@ private function parseResponse($response_headers, $raw_response)
958990
$response = $json_decoder($response);
959991
}
960992
} elseif (preg_match($this->xmlPattern, $response_headers['Content-Type'])) {
961-
$xml_obj = @simplexml_load_string($response);
962-
if (!($xml_obj === false)) {
963-
$response = $xml_obj;
993+
$xml_decoder = $this->xmlDecoder;
994+
if (is_callable($xml_decoder)) {
995+
$response = $xml_decoder($response);
964996
}
965997
}
966998
}

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,4 +2585,23 @@ public function testAlternativeStandardErrorOutput()
25852585

25862586
$this->assertNotEmpty($stderr);
25872587
}
2588+
2589+
public function testXMLDecoder()
2590+
{
2591+
$data = array(
2592+
'key' => 'Content-Type',
2593+
'value' => 'text/xml',
2594+
);
2595+
2596+
$test = new Test();
2597+
$test->server('xml_with_cdata_response', 'POST', $data);
2598+
$this->assertFalse(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
2599+
2600+
$test = new Test();
2601+
$test->curl->setXmlDecoder(function($response) {
2602+
return simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
2603+
});
2604+
$test->server('xml_with_cdata_response', 'POST', $data);
2605+
$this->assertTrue(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
2606+
}
25882607
}

tests/PHPCurlClass/server.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@
182182
$rss->appendChild($channel);
183183
echo $doc->saveXML();
184184
exit;
185+
} elseif ($test === 'xml_with_cdata_response') {
186+
header('Content-Type: text/xml');
187+
echo '<?xml version="1.0" encoding="UTF-8"?>
188+
<rss>
189+
<items>
190+
<item>
191+
<id>1</id>
192+
<ref>33ee7e1eb504b6619c1b445ca1442c21</ref>
193+
<title><![CDATA[The Title]]></title>
194+
<description><![CDATA[The description.]]></description>
195+
<link><![CDATA[https://www.example.com/page.html?foo=bar&baz=wibble#hash]]></link>
196+
</item>
197+
<item>
198+
<id>2</id>
199+
<ref>b5c0b187fe309af0f4d35982fd961d7e</ref>
200+
<title><![CDATA[Another Title]]></title>
201+
<description><![CDATA[Some description.]]></description>
202+
<link><![CDATA[https://www.example.org/image.png?w=1265.73&h=782.26]]></link>
203+
</item>
204+
</items>
205+
</rss>';
206+
exit;
185207
} elseif ($test === 'upload_response') {
186208
$tmp_filename = tempnam('/tmp', 'php-curl-class.');
187209
move_uploaded_file($_FILES['image']['tmp_name'], $tmp_filename);

0 commit comments

Comments
 (0)