Using simplexml_load_file with cURL

The PHP function, simplexml_load_file, converts an XML document into an object. This function and file_get_contents uses a similar method, which makes it relatively slower than cURL. There are two different methods to use simplexml_load_file with cURL.

Method 1

As discussed before, you can easily implement cURL into PHP. Using that function along with the following code, you can use simplexml_load_file with cURL.

<?php
$url = 'http://feeds.feedburner.com/fusionswift';
$xml = simplexml_load_string(curl_get_contents($url));
?>

Method 2

It is also possible to use a function which replaces simplexml_load_file. You can use the following code as a full replacement for simplexml_load_file.

<?php
function simplexml_load_file_curl($url) {
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$xml = simplexml_load_string(curl_exec($ch));
	return $xml;
}

$url = 'http://feeds.feedburner.com/fusionswift';
$xml = simplexml_load_file_curl($url);
?>

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>