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.