342

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?

2
  • 2
    For all who do not need to download a file, try get_headers($url);
    – Avatar
    May 19, 2021 at 6: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. Jun 14, 2021 at 18:27

8 Answers 8

459

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.

6
  • 3
    This is correct, unless you need to use query string parameters.
    – Raptor
    Jun 10, 2011 at 8: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 7, 2011 at 1:59
  • 34
    @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.) Oct 7, 2011 at 3:14
  • 1
    Curl is way more fast than file_get_contents. Prefer Curl over file_get_contents in a high traffic environment Nov 25, 2013 at 20:33
  • 2
    @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/… Nov 27, 2013 at 0:19
148

For more advanced GET/POST requests, you can install the CURL library (https://www.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);
5
  • 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 6, 2009 at 5: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. Jun 25, 2015 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? Dec 4, 2015 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. Dec 4, 2015 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: stackoverflow.com/a/14064903/333296
    – Nux
    Sep 1, 2020 at 22:46
67

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");
3
  • 95
    http extension is not bundled with PHP and often not available in shared hosts.
    – Imran
    Jun 7, 2009 at 5:58
  • 27
    this requires pecl_http >= 0.1.0 , which is not installed by default.
    – Raptor
    Jun 10, 2011 at 8:06
  • 8
    "The manual page you are looking for (us2.php.net/manual/en/function.http-get.php) is not available on this server right now."
    – Pang
    Apr 24, 2018 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;
?>
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();
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

5

I like using fsockopen open for this.

1
  • 1
    I don't see how to do it. Can someone please add sample code? Jul 22, 2022 at 14:01
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();

Not the answer you're looking for? Browse other questions tagged or ask your own question.