I have an XML file I can get via a URL. I know I can get the file using fopen
, but sometimes I've seen scripts use curl
. Is there an advantage to using curl
over fopen
to get XML files?
views:
275answers:
3allow_url_fopen
, which is required if you want to open a remote file with fopen
, can be disabled ; so, there are situations in which fopen('http://...')
is not possible.
Note : in this answer, I say "fopen
", but it's exactly the same with all PHP functions that can access remote files : fopen
, file_get_contents
, simplexml_load_file
, ...
On the other hand, curl is an extension, and is not always enabled either.
One nice with curl is that it's pretty easy to configure, and there are a lot of existing options (see curl_setopt
)
To configure the way fopen
accesses remote files, it's a bit trickier -- you'll generally have to work with streams (see here, for example) ; and, generally speaking, there are more people knowing curl than there are developpers mastering streams.
Safest way -- especially if your application will be deployed to servers on which you are not administrator, and cannot re-configure :
- Try one solution
- And, if it doesn't work, try the other one
fopen is simpler to use, and I think not all server setups support curl out of the box. If fopen works fine for you it's probably your best choice.
Well, if you are going to use SimpleXML to load the file you can use
simplexml_load_file($filename);
However, some servers will restrict loading urls from this function. In this case you would be restricted to cURL.