Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fopen or fsockopen? 1

Status
Not open for further replies.

piti

Technical User
Apr 12, 2001
627
SK
hi
i include into my pages part of my partners' page
i use fopen but now i learned the fsockopen way
which one is the best?
 
I'm not sure either is best. The two functions do different things.

fopen() is designed to open a file handle. It can talk to HTTP, FTP, or your filesystem and return a file handle to read or write (except for HTTP) to the file.

fsockopen() just opens a TCP/IP socket connection from your machine to another one (or in the case of UDP, just sends a packet to the other one). What you do with it from there is your business. fsockopen() gives you very fine control over the socket, especially if you're running PHP on a Unix-like OS.

If you know all the commands which go back and forth from your various client apps and their respective servers (HTTP, FTP, Gopher, SMTP, POP3, etc), the sky is the limit in what fsockopen() can do for you. If you just need to suck down a web-page, fopen() will probably do what you need. There are also other specialized PHP families of functions for other high-level protocols -- FTP for example.

Something that comes to mind as a first use a lot of people will have for fsockopen() is to be able to send POST-method form data to a web script. This requires you to write to the handle the HTTP headers, then the form data. After that you can retrieve the page as generated by the web server by reading from the handle. You can't do what with fopen() because a handle it opens to an HTTP resource is read-only. ______________________________________________________________________
TANSTAAFL!
 
thanx, what i do is just reading a daily changed info from the other server and including that into my html code - no ftp, submiting form info or other stuff you mentioned

sometimes the other server responses that it's busy (as i remember it's one of the 5xx http errors) - isn't the error code returned quicker while using the sockets? when using fopen() the page pauses loading while waiting for the other server's response
 
Since you have more granular control over operations with fsockopen(), you can do things like set the timeout on the socket (socket_set_timeout(): or set whether the socket is blocking or non-blocking (socket_set_blocking(): which may or may not work correctly on Win32). ______________________________________________________________________
TANSTAAFL!
 
thanx, i'll give it a try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top