Using sockets in PHP [reproduced]

xiaoxiao2021-03-06  41

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.

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.

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.

"/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 "



/ n"; Continue;} / * Print trying to get the page to get the server * / echo "Trying to get $ PAGE
/ N"; $ request = "Head $ Page HTTP / 1.0 / r / n / r / n" ; fputs ($ fp, $ request); / * print the results to the browser * / echo fgets ($ fp, 1024); echo "


/ n"; / * Once again close the Connection with fclose () * / fclose ($ fp); / * Close the foreach loop * /}?>

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.

"") {$ 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 ""; / * 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

转载请注明原文地址:https://www.9cbs.com/read-72474.html

New Post(0)