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 the Sockets is 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.
Now That We Have Established A Connection To The Server At Example.com Let's Close The Connection. You might be fasiliar with the fclose () Function, WE Use To Close The Connection.
PHP $ fp = fsockopen ("www.example.com", 80, $ errno, $ errdesc); // Establish ConnectionFclose ($ fp); // Close Connection?>
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 / * Once again we connect to the server at example.com * / $ Host = "www.example.com"; $ page = "/index.html"; $ fp = fsockopen ($ Host, 80, $ Errno, $ Errdesc) ORDIE (" Connection to $ Host Failed "; / * Now we define the headers to be sent to the serverGET means we want the page or we want the contentsof it. We can use POST to send variables to thatpage and return the results as i will show youlater in this tutorial. * / $ request = " GET $ PAGE HTTP / 1.0 / r / n "; $ Request. =" Host: $ HOST / R / N "; $ request. =" Referer: $ Host / R / N "; / * Using fputs () WE Send The Request to The Serverand The Request to the Serverand The loop THROUGH The Results and Form Anaarray CA LLED $ page * / fputs ($ fp, $ request); while (! Feof "{$ page [] = fgets ($ fp, 1024);} / * Close The Connection and Count How Many Lines Thaserver Returned For a certrain page * / fclose ($ fp); echo "the server returned". ($ page). "lines"; / * loop through the page array and print each line Tothe Browser. Here We Use the for () Statement. * / for ($ I = 0; $ i
That should not 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 $ servers = array ("www.example.com" => "/index.html", "www.example2.com" => "/index.php");/*loop through the servers array and then Connect Tothe Host And Return An Error IF Fisockopen Couldn'tconnect To The Server. * / Foreach ($ Servers AS $ Host => $ page) {$ fp = fsockopen ($ Host, 80, $ Errno, $ Errdesc, 10); Echo "Trying $ Host
/ N"; if (! $ fp) {echo ("Couldnt Connect To $ Host"); ECHO "
That Piece Of Code Will Simply Display Something Like this:
Trying: www.example.comTrying to get: /index.htmlhttp/1.1 200 ok
That will be displayed only if the page was found. 404 Not found will replace 200 OK if the page was not 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 qi1 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 replaord for a ceertain domain.
phpextract ($ _ pos); Function Whois ($ domain, $ ext) {$ URL = $ domain. $ ext; / * use a switch () Statement to Determine Which Whois Server Is The Best To Use for the Entered Domain. * / switch ($ ext) {case ".co.uk": $ WHOIS = "Whois.nic.uk"; Break; Case ".com": $ Where = "Whois.Networksolutions.com"; Break; Case .fr ": $ WHOIS =" Whois.nic.fr "; Break; Case" .biz ": $ WHOIS =" Whois.biz "; Break; default: $ Whereis =" whois.networksolutions.com ";} TRIM ($ URL) <> "") {$ URL = Trim ($ URL); / * Open the connection to the Above Whois Server * / $ f = fsockopen ($ WHOIS, 43, $ Errno, $ Errstr, 30) ; If (! $ F) {echo "connection to server failed ($ errno)";} else {/ * send the domain to the server and return thereultsults * / fputs ($ F, "$ URL / R / N") PRINT "
/ r / n"; while (! Feof) {echo fread ($ f, 128);} print " pre>"; / * use fclose to close the connection to the connection Whois server * / fclose ($ f);}} else {echo "invalid domain entered";}}?> NOTERED I have written this code in a function SOIT ITHER Examples in this Tutorial. The Other. The can be coded Up INTO A FUNCTION for Relative Ease.Conclusion
By Now if you have followed this tutorial stage by stage you shouth knowledge
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