Examples of fsockopen usage in php, phpfsockopen examples
The example in this article describes the usage of fsockopen in php. Share it with everyone for your reference.
The specific implementation method is as follows:
Copy code The code is as follows:
$fp=fsockopen("127.0.0.1",80); //Open the data stream
if(!$fp) //If there is an error in opening
{
echo "unable to openn"; //Output content
}
else //If successfully opened
{
fwrite($fp,"get / http/1.0rnrn"); //Write content to the data stream
stream_set_timeout($fp,2); //Set the timeout
$res=fread($fp,2000); //Read content
$info=stream_get_meta_data($fp); //Get the data stream header
fclose($fp); //Close the data stream
if($info['timed_out']) //If timeout
{
echo 'connection timed out!'; //Output content
}
else
{
echo $res; //Output the read content
}
}
//Example 2
//Create server
$socket=stream_socket_server("tcp://0.0.0.0:8000",$errno,$errstr);
//If creation fails
if(!$socket)
{
echo "$errstr ($errno)
n";
}
//If created successfully
else
{
//Accept connection
while($conn=stream_socket_accept($socket))
{
//Write data
fwrite($conn,'the local time is '.date('n/j/y g:i a')."n");
//Close connection
fclose($conn);
}
//Close socket
fclose($socket);
}
//
$file="test.txt"; //Definition file
$fp=fopen($file,"w"); //Open the data stream
if($fp) //If successfully opened
{
stream_set_write_buffer($fp,0); //Set buffer
fwrite($fp,$output); //Write content
fclose($fp); //Close the data stream
}
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/937725.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/937725.htmlTechArticleExamples of fsockopen usage in php, phpfsockopen examples This article describes the usage of fsockopen in php. Share it with everyone for your reference. The specific implementation method is as follows: Copy the code The code is as follows...