What's the difference between fopen() and fsockopen()?

    They are completely different. fsockopen opens a socket (the type of socket depending on the protocol prefix). fopen opens a file or sometimes something else (like a file over HTTP, or a stream filter or something).

    But I believe that the set of things which can be opened with them has little (if any) overlap.

    However, once it's open, you can do most of the same things on the resulting handle (depending on how you opened it; if it's opened for reading only, you can't write, obviously). For some types of object, some operations don't make sense (such as using fseek() on a socket)

    Mark

      What are some examples of types of sockets? When would I want to use them?

        Sockets are a mechanism for programs to communicate with each other. Often over a network.

        Over a network, a socket's address is made up from the host name and the port number (as supplied to fsockopen, for example). So (using Example One from fsockopen's manual page) [url]www.example.com:80[/url] is a socket address (which in this case would appear to be an HTTP connection, in line with current conventions).

        In other words, your browser made a socket connection with phpbuilder's server and then the two computers started sending stuff to each other.

          Ah, so fopen() is probably used when accessing stuff locally, whereas fsockopen() would be used when you want to access something remotely. Does that sum it up?

            fopen() is for connecting to local files, or to remote systems in cases where the communication protocols have been embedded into the program (either built into the PHP environment itself, as in the case of HTTP, FTP and some other so-called URL [man]wrappers[/man], or written using [man]stream-wrapper-register[/man]).

            fsockopen() is when such a "URL wrapper" isn't provided, and you want or have to do the protocol yourself.

              Write a Reply...