3 ms·
Why not? 1) The name of the function is file_get_contents, however you're not getting a file. It just annoys me, it reads wrong. stream_get_contents is better
by johnnyfaehell 12y ago
Why not?
1) The name of the function is file_get_contents, however you're not getting a file. It just annoys me, it reads wrong. stream_get_contents is better but I still wouldn't go reaching for streams for HTTP requests.
2) It provides you with a lack of control.
3) It's pretty sloppy, to do anything like POST requests with bodies you have to do hacky stuff.
I wouldn't be writing my own curl call request either I would use a library such as Guzzle.
- TazeTSchnitzel 12y ago>1) The name of the function is file_get_contents, however you're not getting a file. No, you are getting a file. It's from a remote location, but it's a file nonetheless. Are ftp:// resources not files? Are ssh:// resources not files? >2) It provides you with a lack of control. Does it? Stream context options give you a degree of control. If file_get_contents isn't quite right for you, there's also fopen. >3) It's pretty sloppy, to do anything like POST requests with bodies you have to do hacky stuff. $ctx = stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $some_post_data_here ] ]); $result = file_get_contents('http://example.com/post_endpoint', false, $ctx); Where's the hacky stuff? It's also shorter and more readable than the equivalent in cURL (the PHP cURL extension, not the command-line, obviously) and you don't even need to install an extension!
- johnnyfaehell 12y ago> No, you are getting a file. It's from a remote location, but it's a file nonetheless. Are ftp:// resources not files? Are ssh:// resources not files? FTP of course it's a file, it's a FILE TRANSFER PROTOCOL. HTTP is HYPERTEXT TRANSFER PROTOCOL. You're getting Hypertext. SSH:// are not files. You are getting the contents of a stream. So you're getting the stream content. > Does it? Stream context options give you a degree of control. If file_get_contents isn't quite right for you, there's also fopen. To be fair I've not looked into all the options for the streams in http. But does it give you the option to use global dns cache? How about save the cookies and reuse them for later requests? Will it deal with gzip for me? If you can do all the stuff curl can do I'll be all fair enough you've got a point. > Where's the hacky stuff? For me the fact I had to define the header. > It's also shorter and more readable than the equivalent in cURL It's not more readable. It says it's getting a file, you lied to me and got stream contents instead. > and you don't even need to install an extension! If you really don't want to install extensions and just use the core language fair enough, I would rather use the best tools available.
- TazeTSchnitzel 12y ago>HTTP is HYPERTEXT TRANSFER PROTOCOL. You're getting Hypertext. The responses HTTP gives are very file-like. So much so HTTP is often used for distributing files. Indeed, most pages reference several files. > But does it give you the option to use global dns cache? I don't believe so. I don't think most users need to, however. > How about save the cookies and reuse them for later requests? I can't actually remember. Possibly. > Will it deal with gzip for me? Yes. > If you can do all the stuff curl can do I'll be all fair enough you've got a point. It can do most of the stuff cURL does. It doesn't do everything, but my point is most of the time you don't necessarily need cURL, and IMO this API is nicer. > For me the fact I had to define the header. That's not hacky at all; sending POST requests usually requires specifying a Content-Type. cURL just automatically does that for you in some cases. > It's not more readable. It says it's getting a file, you lied to me and got stream contents instead. You did get a file. The file the request returned. > If you really don't want to install extensions and just use the core language fair enough, I would rather use the best tools available. You might be missing my point.