KingPin's Forum
 
K.P.s.N. Register vbBux / KPs Mall Bugs Blogs FAQ Search Today's Posts Mark Forums Read Donate
Go Back   KingPin's Forum > KP's Network Forum > Tutorials & Stuff!
Reload this Page PHP Tutorial #3 : Using Sockets in PHP
 


Tutorials & Stuff! This is a discussion on PHP Tutorial #3 : Using Sockets in PHP in the Tutorials & Stuff!;
Description: Using Sockets in PHP Introduction Sockets are a method in which PHP can connect to another server over the Internet ...

Reply
 
LinkBack Thread Tools
PHP Tutorial #3 : Using Sockets in PHP
(#1 (permalink))
Old
KingPin's Avatar
KingPin is Offline
Da Boss!
KingPin has disabled reputation
 

My Mood:
 
Posts: 7,034
Thanks: 20
Thanked 17 Times in 15 Posts
Blog Entries: 22
Join Date: Apr 2004
Location: Brooklyn, NY
PHP Tutorial #3 : Using Sockets in PHP - July 10th, 2004

Using Sockets in PHP

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.

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.
Code:
<?php

/*

Arguments that fsockopen takes:

fsockopen(Hostname/IP, Port Number, Error Number

Variable, Error Description Variable)

The error number variable and error description

variable are populated only on failure of

fsockopen. $errno will contain the error number

and $errdesc will contain the error description

e.g. Server cannot be found.

*/

$fp = fsockopen( "www.example.com", 80, $errno,

$errdesc);

?>
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.
Code:
<?php

$fp = fsockopen( "www.example.com", 80, $errno,

$errdesc); //establish connection

fclose($fp); //close connection

?>
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.
Code:
<?php

/*

Once again we connect to the server at example.com

*/

$host = "www.example.com";

$page = "/index.html";

$fp = fsockopen($host, 80, $errno, $errdesc) or

die("Connection to $host failed");

/*

Now we define the headers to be sent to the server

GET means we want the page or we want the contents

of it. We can use POST to send variables to that

page and return the results as i will show you

later in this tutorial.

*/

$request = "GET $page HTTP/1.0rn";

$request .= "Host: $hostrn";

$request .= "Referer: $hostrn";

/*

Using fputs() we send the request to the server

and then loop through the results and form an

array called $page

*/

fputs($fp, $request);

while(!feof($fp)){

$page[] = fgets($fp, 1024);

}

/*

Close the connection and count how many lines the

server returned for a certain page

*/

fclose($fp);

echo "The server returned ".(count($page)).

" Lines";

/*

Loop through the page array and print each line to

the browser. Here we use the for() statement.

*/

for($i=0; $i<count($page); $i++){

echo $page[$i];

}

?>
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.
Code:
<?php

$servers = array(

        "www.example.com" => "/index.html",

        "www.example2.com" => "/index.php"

);

/*

loop through the servers array and then connect to

the host and return an error if fsockopen couldn't

connect to the server.

*/

foreach($servers as $host=> $page){

$fp = fsockopen($host,80,$errno,$errdesc,10);

echo "Trying $host<br>n";

if(!$fp){

echo("couldnt connect to $host");

echo "<br><hr><br>n";

continue;

}

/*

Print trying to get the page then define the

request and send it to the server

*/

echo "trying to get $page<br>n";

$request = "HEAD $page HTTP/1.0rnrn";

fputs($fp, $request);

/*

print the results to the browser

*/

echo fgets($fp, 1024);

echo "<br><br><br>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.com
Trying to get: /index.html
HTTP/1.1 200 OK

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.

 
Reply With Quote
Revenue Shared Ads
PHP Tutorial #3 : Using Sockets in PHP
Revenue Shared Ads
Reply

Bookmarks

Tags
php, tutorial, using, sockets

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Rupees Per Thread View: 1.00
Rupees Per Thread: 15.00
Rupees Per Post: 5.00
Forum Jump



These are the 100 most searched terms
Search Cloud
_backup.rc antrix wow antrix wow server autocad 2007 keygen best google gadgets best wow character bt map devil may cry 4 crack enchanting leveling erika bella fuckteam fuckteam 5 georgina lempin gigistar gigistar topless kpsden kpsforum leatherworking guide leveling enchanting mangos custom vendors mangos item id mangos linux mangos server mangos spell id mangos wotlk mature creampie milena velba no rest for the ass object id wow phoenix marie this is sparta vipersdenforums voyage century bot windows activation workaround windows xp lite wotlk alpha private server wotlk private server wotlk server wotlk wiki wow leatherworking guide wow mount locations wow object id wow object id list wow object id's wow object ids wow objects id www.adobeflashplayer www.adobeflashplayer.com www.kpsforum.com xbox 360 mmorpg ... powered by Simple Search Cloud
Powered by vBulletin® Version 3.8.0 Beta 3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright 2004-2009 KPsN


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82