views:

294

answers:

2

I am making a web application in PHP and want to read content from a other domain. For that i have to options fopen and curl but what are the differences like security / options etc.?

and what is the best way to use and why ?

does it matter if the url from the other domain is a http or https site ?

+6  A: 

Curl uses external library and it has a lot more power to customizing the request - custom headers, generating POST request, uploading files. Everything you need I must say.

Fopen is limited to only just make a GET request of the url without any further customization.

As for security CURL is not influenced by security configuration in PHP (like forbidden fopen of remote URLS and such).

The both possibilities return you data which you can use in every possible way you want. If you make a security hole in your project, after getting the data is your fault after all.

Also I am not quite sure but I think that fopen cannot handle SSL (https) connections. Both fopen and CURL support SSL (as noted by Andy Shellam in a comment below).

bisko
fopen does support HTTPS providing you've compiled PHP against OpenSSL.
Andy Shellam
Thank you for the note, Andy! Now I know one more thing about PHP :)
bisko
+1  A: 

See http://stackoverflow.com/questions/636678/what-are-the-important-differences-between-using-fopenurl-and-curl-in-php/637693 for some security settings that affect fopen namely allow_url_include.

Also, note that with curl if you setopt CURLOPT_FOLLOWLOCATION then curl follows redirects to file:// to fetch data (still subject to open_basedir). Redirects to other schemes such as ftp:// could be worse (have not tested ftp://). Without that setopt curl does not follow redirects at all. fopen seems to work with 302 by default but only http:// -> http:// and not http:// -> file://.

mar