tags:

views:

7690

answers:

5

I'm planning to use PHP for a simple requirement. I need to download a XML content from a URL, for which I need to send HTTP GET request to that URL.

How do I do it in PHP?

+5  A: 

http_get should do the trick. The advantages of http_get over file_get_contents include the ability to view HTTP headers, access request details, and control the connection timeout.

$response = http_get("http://www.example.com/file.xml");
William Brendel
http extension is not bundled with PHP and often not available in shared hosts.
Imran
+6  A: 

Unless you need more than just the contents of the file, you could just use file_get_contents.

$xml = file_get_contents("http://www.example.com/file.xml");
musicfreak
it worked. thanks. :)
Veera
No problem. :)
musicfreak
+1  A: 

You must have the CURL library (http://us3.php.net/curl) installed:

$ch = curl_init("REMOTE XML FILE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
James Skidmore
Although there really isn't any need to use CURL for a "simple requirement", +1, because it's really the best solution for doing anything more advanced with HTTP requests in PHP.
xyz
+1  A: 

Depending on whether your php setup allows fopen on URLs, you could also simply fopen the url with the get arguments in the string (such as http://example.com?variable=value )

Edit: Re-reading the question I'm not certain whether you're looking to pass variables or not - if you're not you can simply send the fopen request containg http://example.com/filename.xml - feel free to ignore the variable=value part

Zxaos
A: 

I like using fsockopen open for this.

pbreitenbach