homepage Welcome to WebmasterWorld Guest from 54.211.213.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Grabbing A Remote File
Which method is the most efficient?
Simsi




msg:3585917
 8:27 am on Feb 27, 2008 (gmt 0)

Hi

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.

Many thanks

Ian

[edited by: Simsi at 8:30 am (utc) on Feb. 27, 2008]

 

phparion




msg:3585946
 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.

Simsi




msg:3585949
 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 :-)

PHP_Chimp




msg:3586468
 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 ;)

Vis3R




msg:3586650
 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]

Simsi




msg:3586843
 8:30 am on Feb 28, 2008 (gmt 0)

Thanks Vis3r/PHP_Chimp.

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]

mipapage




msg:3586865
 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved