PHP: fsockopen()

PHP has built in functions allowing your scripts to communicate with various other applications and services via a socket connection, this opens new and exciting possibilities for us PHP programmers. You might be asking yourself why? With this ability we can query mail servers, get website content whilst also providing POST and GET data, query a DNS server these socket connections are almost unlimited you can connect to virtually any host or port number and use various transport technologies such as TCP, UDP, HTTP, HTTPS, SSL, TLS this allows for a wide range or usages, it wouldn’t surprise me if you could connect to a FTP or SSH server but I have never tried this.

The main uses and real live implementations I have used with this technology were to query a web server whilst providing some POST data and retrieving the response for processing. I have also managed to get a script working which allows you to run multiple occurrences of the same script using your web servers built in multithreading technology (but each thread/occurrence of your script will process a different section of data speeding up long processes) but I will go into this in another post shortly.

PHP Manual: fsockopen

Here is an example section of code which will connect to 127.0.0.1 using port 13, the communication will be performed using UDP packets (we know this due to the udp:// at the beginning of the host address, if this is not set it defaults to HTTP). Any error numbers or messages are stored into the 2 optional variables $errno and $errstr which can be used later in are scripts to determine what to do.

Basically the following script queries the date/time service on your own computer or server for the time then displays the response. The following is ok if you know you will get an instant response, but if we are communicating with other services such as a website where multiple lines of data are returned we need to use the following code to get the full output.

What this does is loops through the complete response and each time displays the next line, until the response has been looped through completely. If you don’t need to wait for the response and only need to initiate the communication then you can remove the while loops and fread commands. This will cause your script to start the communication and then it will just continue with the rest of the script, for example I want to load a website page 20 times which takes 10 seconds each to complete. That would give my script a running time of more than 200 seconds if it was to wait for each response. Now I remove the while and fread commands and now my script only takes 2 seconds to run but still initiates the other website pages (it completes faster due to not waiting for the response from each page, but the web server which handles these websites still finishes processing the request which my script made even through nothing will ever process the response).

Leave a Reply

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Next ArticlePHP: fsockopen() - Sending POST data with HTTP request