|Grabbing A Remote File|
Which method is the most efficient?
| 8:27 am on Feb 27, 2008 (gmt 0)|
I am writing a PHP script that grabs a set of relatively small (max 500 lines of text) files from a remote URL (referenced via [www...] as opposed to an IP address). I have identified 4 potential methods for grabbing the file and saving it to a local file: fopen(), curl, fsockopen() and file_get_contents().
Because the script will be run by multiple users, I won't know which method(s) their server supports, so I do a test to determine which is viable and store the result in a config file. Which of these methods is the most efficient, or in what order would you test for compatibility?
A the moment I have it set to test fopen(), file_get_contents(), fsockopen() and curl in that order - but I'd rather have the most efficient one first obviously so if it's available, it will become the default method on that server.
[edited by: Simsi at 8:30 am (utc) on Feb. 27, 2008]
| 9:30 am on Feb 27, 2008 (gmt 0)|
if you have a direct file link on the server then fopen() will be my choice. But i have seen on some hosts fopen is not allowed..
my personal choice is cURL. I love it due to its availability on a wide range of hosts nowadays and its control options which make your job very easy.
| 9:38 am on Feb 27, 2008 (gmt 0)|
|But i have seen on some hosts fopen is not allowed.. |
Thanks for the feedback. This is why I am trying 4 methods...to find one that does work.
Anyone else care to give views? The more opinion the better :-)
| 9:20 pm on Feb 27, 2008 (gmt 0)|
cURL for the same reasons. None of the hosts that I work with have problems with cURL, however some of the file functions are not allowed on some.
So its just more portable. Dont know about how efficient it is from a CPU point of view...but its a lot more efficient from my point of view not having to rewrite the code every time I move scripts from one host to another ;)
| 12:47 am on Feb 28, 2008 (gmt 0)|
Some time ago i did a research, and found out file_get_contents is the fastest way, fastest code. If you need some more advanced options than just requesting a file, like adding cookies and other headers, you can use function's context variable. So file_get_contents is what i'd suggest.
[edited by: Vis3R at 12:49 am (utc) on Feb. 28, 2008]
| 8:30 am on Feb 28, 2008 (gmt 0)|
Funnily enough, file_get_contents() was the one I seemed to get on best with in testing too, but my instinct told me CURL would be more efficient, where present.
[edited by: Simsi at 8:30 am (utc) on Feb. 28, 2008]
| 8:46 am on Feb 28, 2008 (gmt 0)|
|Funnily enough, file_get_contents() was the one I seemed to get on best with in testing too, but my instinct told me CURL would be more efficient, where present. |
My experience has been the opposite, in the context of presence:
file_get_contents() is the most simple and easy to use, when present (or better said, "when allowed"), however cURL is more reliable across hosts.