How to send a GET request from PHP?
Solution 1
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");
For anything more complex, I'd use cURL.
Solution 2
For more advanced GET/POST requests, you can install the CURL library (http://us3.php.net/curl):
$ch = curl_init("REMOTE XML FILE URL GOES HERE"); // such as http://example.com/example.xml
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
Solution 3
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");
Solution 4
Remember that if you are using a proxy you need to do a little trick in your php code:
(PROXY WITHOUT AUTENTICATION EXAMPLE)
<?php
$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;
?>
Solution 5
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
Comments
-
Veera almost 2 years
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?
-
xyz almost 15 yearsAlthough 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.
-
Imran almost 15 yearshttp extension is not bundled with PHP and often not available in shared hosts.
-
Raptor almost 13 yearsThis is correct, unless you need to use query string parameters.
-
Raptor almost 13 yearsthis requires pecl_http >= 0.1.0 , which is not installed by default.
-
Raptor over 12 years@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!
-
Sasha Chedygov over 12 years@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.) -
Deb over 10 yearsCurl is way more fast than file_get_contents. Prefer Curl over file_get_contents in a high traffic environment
-
Sasha Chedygov over 10 years@Deb: Really? Do you have a benchmark you could link to? I won't believe that until I see some numbers, because I don't see how
file_get_contents
could be significantly slower, especially since most of the time would be spent waiting for the response. -
Deb over 10 years@Sasha - I have faced this on many production environments but you could see these links for now: haltiko.blogspot.com/2013/02/… or mdb9.wordpress.com/2011/03/06/…
-
Peter Gordon almost 9 yearsAs already mentioned, curl is much faster than
file_get_contents()
. A simple 4 letter string took about 10 seconds with it, while curl took a more reasonable ~1 second max. -
Rauni Lillemets over 8 years@pgmann are You sure in that result? this is not in line with other statistics with what have been quoted. Indeed, CURL is faster, but it shouldn't be that much faster. Maybe this drop in response time came from server-side caching of some resource, etc? Maybe You should repeat the test?
-
Peter Gordon over 8 years@Rauni I'm afraid I can't retest but I always go with cURL for the speed increase, etc. The request times are approximate, what it felt like when loading the page. I did try multiple times.
-
Pang about 6 years"The manual page you are looking for (us2.php.net/manual/en/function.http-get.php) is not available on this server right now."
-
Nux over 3 yearsNote! For debugging use
curl_setopt($ch, CURLOPT_VERBOSE, true);
. And note that HTTPS will probably not work out of the box. You need to download and setup CA file path. See: stackoverflow.com/a/14064903/333296