338

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?

Veera
  • 32,532
  • 36
  • 98
  • 137
  • 2
    For all who do not need to download a file, try `get_headers($url);` – Avatar May 19 '21 at 06:15
  • Is there a way to call an HTML file synchronously from PHP? The HTML file does nothing but link to a JavaScript file that returns a value using document.write(). In other words, I want to run JavaScript code on the same server from PHP. I've tried both file_get_contents and cURL, and both return the JavaScript file instead of its output. – David Spector Jun 14 '21 at 18:27

8 Answers8

454

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.

Sasha Chedygov
  • 127,549
  • 26
  • 102
  • 115
  • 3
    This is correct, unless you need to use query string parameters. – Raptor Jun 10 '11 at 08:04
  • 2
    @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 07 '11 at 01:59
  • 33
    @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 07 '11 at 03:14
  • 1
    Curl is way more fast than file_get_contents. Prefer Curl over file_get_contents in a high traffic environment – Deb Nov 25 '13 at 20:33
  • 1
    @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. – Sasha Chedygov Nov 26 '13 at 01:42
  • 2
    @Sasha - I have faced this on many production environments but you could see these links for now: http://haltiko.blogspot.com/2013/02/curl-or-filegetcontents-what-is-more.html or http://mdb9.wordpress.com/2011/03/06/file_get_contents-vs-curl-what-has-better-performance/ – Deb Nov 27 '13 at 00:19
148

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);
Volomike
  • 23,743
  • 21
  • 113
  • 209
James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • 10
    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 06 '09 at 05:42
  • 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. – Peter Gordon Jun 25 '15 at 18:59
  • @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? – Rauni Lillemets Dec 04 '15 at 12:17
  • 1
    @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. – Peter Gordon Dec 04 '15 at 15:52
  • 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: https://stackoverflow.com/a/14064903/333296 – Nux Sep 01 '20 at 22:46
66

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
  • 31,712
  • 14
  • 72
  • 77
  • 95
    http extension is not bundled with PHP and often not available in shared hosts. – Imran Jun 07 '09 at 05:58
  • 27
    this requires pecl_http >= 0.1.0 , which is not installed by default. – Raptor Jun 10 '11 at 08:06
  • 8
    *"The manual page you are looking for (http://us2.php.net/manual/en/function.http-get.php) is not available on this server right now."* – Pang Apr 24 '18 at 10:01
20

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;
?>
Taryn
  • 242,637
  • 56
  • 362
  • 405
pepe
  • 201
  • 2
  • 3
10

Guzzle is a very well known library which makes it extremely easy to do all sorts of HTTP calls. See https://github.com/guzzle/guzzle. Install with composer require guzzlehttp/guzzle and run composer install. Now code below is enough for a http get call.

$client = new \GuzzleHttp\Client();
$response = $client->get('https://example.com/path/to/resource');

echo $response->getStatusCode();
echo $response->getBody();
Mark Baaijens
  • 496
  • 5
  • 6
9

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
  • 7,791
  • 12
  • 47
  • 61
5

I like using fsockopen open for this.

pbreitenbach
  • 11,261
  • 3
  • 33
  • 24
3

On the other hand, using the REST API of servers is very popular in PHP. You can suppose all URLs are parts of a REST API and use many well-designed PHP packages.

Actually, REST API is a way to use services from a site.

So, there are many PHP packages developed to simplify REST API call. For example here is a very nice one:

https://github.com/romanpitak/PHP-REST-Client

Using such packages helps you to fetch resources easily.

So, getting the xml file (that you mentioned about) is as easy as:

$client = new Client('http://example.com');
$request = $client->newRequest('/filename.xml');
$response = $request->getResponse();
echo $response->getParsedResponse();
Mostafa Barmshory
  • 1,849
  • 24
  • 39