I use fopen or curl to load an XML file given a URL in PHP

I have an XML file that I can get through the url. I know I can get the file using fopen , but sometimes I saw scripts use curl . Is there an advantage to using curl over fopen to get XML files?

+4
source share
3 answers

Well, if you are going to use SimpleXML to upload a file, you can use

 simplexml_load_file($filename); 

However, some servers will restrict the loading of URLs from this function. In this case, you will be limited to cURL.

+1
source

allow_url_fopen , which is required if you want to open a remote file with fopen , can be disabled; therefore, situations arise where fopen('http://...') not possible. Note: in this answer I say " fopen ", but in the same way with all the PHP functions that can access remote files: fopen , file_get_contents , simplexml_load_file , ...

Curl , on the other hand, is an extension and is not always included.


One nice thing with curl is that it is fairly easy to configure, and there are many existing options (see curl_setopt )

To set up the fopen method of accessing remote files is a bit more complicated - you usually have to work with streams (see here , for example); and, generally speaking, there are more people who know curl than they develop the development of flows.


The safest way - especially if your application is deployed to servers on which you are not an administrator, and cannot reconfigure:

  • Try one solution.
  • And if it doesn’t work, try another
+3
source

fopen is easier to use, and I think that not all server settings support curl out of the box. If fopen is great for you, this is probably your best bet.

+1
source

Source: https://habr.com/ru/post/1308393/


All Articles