Using Sockets in PHP by Andrew Walsh |
|
Introduction
Sockets are a method in which PHP can connect to another server over the Internet and networks. The basic function to open a socket to a server is fsockopen(). You may be wondering why you would want to connect to another server? If you need to obtain information from a 3rd party server then sockets are for you.
Making the Connection
You may think connecting to another Internet server is difficult but you would be wrong. You can establish a connection in just one line of PHP code. In this section I will show you how to simply connect and disconnect using sockets.
<?php |
Now that we have established a connection to the server at example.com let's close the connection. You might be familiar with the fclose() function, we use this to close the connection.
<?php |
Now lets move on to more useful things related to sockets on the next page.
Sending The Request
In the following section you will learn how to send a request to a server and then list how many lines the server returned for a particular page and then how to loop through the returned array to display the page. Once you have a socket open to a server the variable $fp or whatever you have called it acts like a file in many ways, meaning you can send variables to $fp and return results.
<?php |
That shouldn't of been too hard because after the initial connection and when we returned the array with the request we only looped through the array and printed its contents to the browser. In the next section I will show you a example of connecting to multiple servers.
Searching for a Page
Right in this section I will show you an example that uses fsockopen() to connect to a server. The example I will show you is how to connect to multiple webservers and check if a certain page is on that server.
<?php |
That piece of code will simply display something like this:
Trying: www.example.com |
That will be displayed only if the page was found. 404 Not found will replace 200 OK if the page wasn't found. You may recognize the 404 error as the exact same one as seen in your browser if you go to a page that doesn't exist.
In the next section I will show you how to get whois results for a domain name using fsockopen().
Whois Example
So you have learned how to connect to servers, fetch results, loop through results and finally close connections. So I thought I would include a more practical example in this section and I chose to do a whois example which connects to certain whois servers and checks for a record for a certain domain.
<?php |
Notice I have written this code in a function so it is more practical and reusable than the other examples in this tutorial. They can be coded up into a function with relative ease.
Conclusion
By now if you have followed this tutorial stage by stage you should know how to:
- Connect To A Server Using Sockets
- Disconnect From a Server
- Send a request to a server
- List the number of lines returned by the server on a request
- Use sockets in a practical project like a whois look-up script
And you should now realise that sockets and connecting to servers is both useful to know and easy to use.
About the Author
Andrew Walsh lives in the UK and is engaged in his secondary education. He wishes to persue a higher education in computer studies. He started programming at the age of 13 years, currently he has knowledge of PHP/MySQL, HTML, CSS and JavaScript.