<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fullyreloaded Blog &#187; PHP</title>
	<atom:link href="http://blog.fullyreloaded.com/topics/programming/php/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.fullyreloaded.com</link>
	<description>reload your mind</description>
	<lastBuildDate>Fri, 23 Apr 2010 07:11:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create Socket Connection in PHP</title>
		<link>http://blog.fullyreloaded.com/create-socket-connection-in-php</link>
		<comments>http://blog.fullyreloaded.com/create-socket-connection-in-php#comments</comments>
		<pubDate>Fri, 05 Dec 2008 16:24:19 +0000</pubDate>
		<dc:creator>Lucky Adibrata</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[php fsockopen]]></category>
		<category><![CDATA[socket connection]]></category>

		<guid isPermaLink="false">http://blog.fullyreloaded.com/?p=120</guid>
		<description><![CDATA[PHP server provide a method in which it can establish socket connection to communicate with other server. Many software applications including web server, instant messaging, and peer to peer file sharing systems rely on sockets. In other words, you can possibly create any web based application, ie. instant messaging client purely in PHP.
Open Connection
The basic [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>PHP server provide a method in which it can establish socket connection to communicate with other server. Many software applications including web server, instant messaging, and peer to peer file sharing systems rely on sockets. In other words, you can possibly create any web based application, ie. instant messaging client purely in PHP.</p>
<p><strong>Open Connection</strong><br />
The basic function to open a socket connection is fsockopen(). You can establish a connection in just one line of PHP code, pretty simple uh?</p>
<pre lang="php" line="1">
//this method will open a connection and store a file handler to $fp variable,
//you can use fgets/fputs on it then later,
//if something goes wrong, you can check $errno and $errdesc variable
$fp=fsockopen("www.host.com",80,$errno,$errdesc);
</pre>
<p>After we established a connection, let&#8217;s close it by using fclose() method.</p>
<pre lang="php" line="1">
$fp=fsockopen("www.host.com",80,$errno,$errdesc);
fclose($fp);
</pre>
<p><strong>Sending a Request</strong><br />
Now I will show you how to send a data (request) to a server and then check the response returned by the server. Once we open a socket, you can send any data to it, and wait for the response.</p>
<p><span id="more-120"></span></p>
<pre lang="php" line="1">
$fp=fsockopen("www.host.com",80,$errno,$errdesc) or
die("Connection to www.host.com failed");

$request="GET / HTTP/1.1\r\n";
$request.="Host: www.host.com\r\n";
$request.="Referer: www.host.com\r\n";
$request.="Connection: close\r\n";
$request.="\r\n";

fputs($fp,$request); //send the request

while(!feof($fp)){
	$lines[] = fgets($fp, 1024);
}
foreach($lines as $line){ //display each lines
	$line=htmlentities($line);
	echo "LINE:".$line."";
}

fclose($socket); //don't forget to close connection
</pre>
<p><strong>Using Socket in PHP &#8211; POP3 Email Example</strong><br />
Here is an example of socket connection to check POP3 email.</p>
<pre lang="php" line="1">
function sendcommand($fp,$command){
	$response="";
	fputs($fp, $command);
	$response=fgets($fp,1024);
	return $response;
}

function checkmail(){
	$host="pop.host.com"; //PHP also support secure connection
			      //you can add "tls://" or "ssl://"
			      //to create secure connection
	$port=110;
	$username="username";
	$password="password";

	$fp=fsockopen($host,$port,$errno,$errstr,10);

	$command="USER ".$username."\r\n"; //send username
	$response=sendcommand($fp,$command);
	echo "LINE:".$response."";
	$command="PASS ".$password."\r\n"; //send password
	$response=sendcommand($fp,$command);
	echo "LINE:".$response."";
	if(strstr($response,"+OK")!=""){ //accepted
		$command="LIST"."\r\n";
		$response=sendcommand($fp,$command);
		echo "LINE:".$response."";
	}
	$command="QUIT"."\r\n";
	$response=sendcommand($fp,$command);
	echo "LINE:".$response."";

	fclose($fp);
}

checkmail();
</pre>
<p>Now you have learned how to use socket in PHP, you can extend the usage for many purpose, like getting information from another web, query to whois server, check pop3/imap mail, instant messaging, online ftp client, and many more. And the most important thing, it is easy to implement.</p>
<p>Good luck on experimenting! <img src='http://blog.fullyreloaded.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.fullyreloaded.com/create-socket-connection-in-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
