PHP http_get vs fsockopen to HTTPS server?
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开发者_如何学C old legacy code or is there some good reason for it?
Thanks.
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.
精彩评论