Take the 2-minute tour ×

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?

share|improve this question
add comment

6 Answers

up vote 118 down vote accepted

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

$xml = file_get_contents("http://www.example.com/file.xml");
share|improve this answer
it worked. thanks. :) –  Veera Jun 6 '09 at 5:43
This is correct, unless you need to use query string parameters. –  Raptor Jun 10 '11 at 8:04
@ShivanRaptor: What would that change? –  Sasha Chedygov Oct 6 '11 at 21:13
@musicfreak: query string is sometimes ignored by some servers for security settings. also, cross-server query may result in this error: failed to open stream: HTTP request failed! –  Raptor Oct 7 '11 at 1:59
@ShivanRaptor: I'm not sure what you're talking about... The query string is just part of the string. There's no reason a server would ignore it. Now, obviously, file_get_contents doesn't let you make the request with custom cookies, headers, and other things that a browser would typically send, so you might not get the response you were looking for. In that case, you'd be better off using CURL anyway. (But that isn't the OP's case.) –  Sasha Chedygov Oct 7 '11 at 3:14
show 3 more comments

For more advanced GET/POST requests, you can install the CURL library (http://us3.php.net/curl):

$ch = curl_init("REMOTE XML FILE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
share|improve this answer
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 Jun 6 '09 at 5:42
add comment

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");
share|improve this answer
http extension is not bundled with PHP and often not available in shared hosts. –  Imran Jun 7 '09 at 5:58
this requires pecl_http >= 0.1.0 , which is not installed by default. –  Raptor Jun 10 '11 at 8:06
add comment

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

share|improve this answer
add comment

Remember that if you are using a proxy you need to do a little trick in your php code:


$aContext = array(
    'http' => array(
        'proxy' => 'proxy:8080',
        'request_fulluri' => true,
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("http://www.google.com", False, $cxContext);

echo $sFile;
share|improve this answer
add comment

I like using fsockopen open for this.

share|improve this answer
add comment

Your Answer


