views:

29

answers:

1

In PHP, what are the biggest considerations when choosing between using http_get("https://...") and a sockets loop with fsockopen("ssl://..."), fputs() and fread()?

I’ve seen a couple of implementations lately that use the latter. Is that just old legacy code or is there some good reason for it?

Thanks.

A: 

http_get requires a PECL extension, which is not bundled with PHP.

fsockopen is more complicated to use (requires looping, sending the headers manually, reading the headers manually, and, in general, more code), but is part of the PHP (it's always present).

In my opinion, the best fail-safe option is to use the http wrapper, as in:

file_get_contents('https://...')

The http wrapper, however, has its own set of limitations – no digest authentication, no automatic handling of encoded content, etc. So if either the PECL http extension or the curl extension are available, those would probably be a better option.

Artefacto
Thanks. For some odd reason, the host I'm accessing has problems with the fsockopen() approach and file_get_contents() will work fine for my current application. (Actually, I'll probably use simplexml_load_file().)
danorton