Home > Backend Development > PHP Tutorial > Examples of fsockopen usage in php, phpfsockopen examples_PHP tutorial

Examples of fsockopen usage in php, phpfsockopen examples_PHP tutorial

WBOY
Release: 2016-07-13 10:10:16
Original
1190 people have browsed it

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.

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template