How to send a GET request from PHP?

629,495

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

Share:
629,495
Veera
Author by

Veera

JavaScript developer. http://veerasundar.com/blog

Updated on July 08, 2022

Comments

  • Veera
    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
    xyz almost 15 years
    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.
  • Imran
    Imran almost 15 years
    http extension is not bundled with PHP and often not available in shared hosts.
  • Raptor
    Raptor almost 13 years
    This is correct, unless you need to use query string parameters.
  • Raptor
    Raptor almost 13 years
    this requires pecl_http >= 0.1.0 , which is not installed by default.
  • Raptor
    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
    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
    Deb over 10 years
    Curl is way more fast than file_get_contents. Prefer Curl over file_get_contents in a high traffic environment
  • Sasha Chedygov
    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
    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
    Peter Gordon almost 9 years
    As 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
    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
    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
    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
    Nux over 3 years
    Note! 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