*/
$ Fp = fsockopen ("127.0.0.1", 80); // open the data stream
If (! $ Fp) // if an error occurs
{
Echo "unable to openn"; // output content
}
Else // if it is enabled successfully
{
Fwrite ($ fp, "get/http/1.0 rnrn"); // 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 read content
}
}
// Instance 2
// Create a server
$ Socket = stream_socket_server ("tcp: // 0.0.0.0.0: 8000", $ errno, $ errstr );
// If creation fails
If (! $ Socket)
{
Echo "$ errstr ($ errno) <br/> n ";
}
// If creation is successful
Else
{
// Accept the connection
While ($ conn = stream_socket_accept ($ socket ))
{
// Write data
Fwrite ($ conn, 'the local time is '. date ('n'/j/y g: I A'). "n ");
// Close the connection
Fclose ($ conn );
}
// Close the socket
Fclose ($ socket );
}
//
$ File = "test.txt"; // definition file
$ Fp = fopen ($ file, "w"); // open the data stream
If ($ fp) // if enabled successfully
{
Stream_set_write_buffer ($ fp, 0); // you can specify a buffer.
Fwrite ($ fp, $ output); // write content
Fclose ($ fp); // close the data stream
}