Sometimes our PHP program needs to communicate with other systems. For example, a company's official website provides product traceability information inquiry. The back-end website needs to communicate with the company's traceability system or ERP system. At this time, PHP network is required. For programming, PHP provides a sockets extension. The official website address is:
http://nl3.php.net/manual/zh/intro.sockets.php
This extension gives us the ability to directly manipulate sockets through php, so that we can communicate with other systems. We use sockets to work above the transport layer of the OSI network model, directly using TCP, UDP provides services, so you can use it as a client for other application layer protocols, such as simulating HTTP clients (browsers). Common smtp, pop, and ftp can be simulated with it. What is more interesting is that you can use it and allow TELNET's server interaction, these protocols are all application layer protocols, so they can all interact. Customized communication between systems requires custom protocols.
Here is an example to develop a simple file receiving server using PHP. Another PHP client program shows how to communicate with this server program. The code is as follows.
Server-side program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | <?php
$address = '127.0.0.1';
$port = 81;
error_reporting (E_ALL);
set_time_limit(0);
ob_implicit_flush();
if (( $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n" ;
}
if (socket_bind( $sock , $address , $port ) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
}
if (socket_listen( $sock , 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
}
do {
if (( $client_sock = socket_accept( $sock )) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
break ;
}
echo "[client connect start]\n" ;
$data = "" ;
$data_size = -1;
$received_size = 0;
$ext_name = "txt" ;
do {
if (false === ( $buf = socket_read( $client_sock , 2048))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error( $client_sock )) . "\n" ;
break 2;
}
if ( $data_size === -1) {
$arr = explode ( "::" , $buf , 3);
$data_size = (int) $arr [0];
$ext_name = $arr [1];
$data .= $arr [2];
$received_size += strlen ( $data );
continue ;
}
$data .= $buf ;
$received_size += strlen ( $buf );
if ( $data_size <= $received_size ) {
if ( $data_size < $received_size ) {
$data = substr ( $data , 0, $data_size );
}
echo "received:" . $received_size . "/" . "total:" . $data_size ;
$talkback = "-end-" ;
socket_write( $client_sock , $talkback , strlen ( $talkback ));
file_put_contents ( "servertest." . $ext_name , $data );
break 2;
}
echo $talkback = "received:" . $received_size . "\n" ;
socket_write( $client_sock , $talkback , strlen ( $talkback ));
} while (true);
socket_close( $client_sock );
} while (true);
socket_close( $sock );
?>
|
Copy after login
Client-side program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | <?php
$file = "yunke.jpg" ;
$address = '127.0.0.1';
$port = 81;
error_reporting (E_ALL);
set_time_limit(0);
ob_implicit_flush(true);
if (( $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n" ;
exit ();
}
if (socket_connect( $sock , $address , $port ) === false) {
echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
exit ();
}
$data = file_get_contents ( $file );
$data_size = strlen ( $data );
$arr_temp = explode ('.', $file );
$ext_name = end ( $arr_temp );
$sock_data = $data_size . "::" . $ext_name . "::" . $data ;
while (true) {
$sock_data_size = strlen ( $sock_data );
$send_size = socket_write( $sock , $sock_data , $sock_data_size );
if ( $send_size === false) {
echo "send false:" . socket_strerror(socket_last_error( $sock )) . "\n" ;
socket_close( $sock );
echo "[client shutdown]\n" ;
exit ();
}
if ( $send_size == $sock_data_size ) {
break ;
}
$sock_data = substr ( $sock_data , $send_size );
}
while (true) {
if (false === ( $out = socket_read( $sock , 2048))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
break 1;
}
echo "server: " . $out . "\n" ;
if ( substr ( $out , -5) == "-end-" ) {
break ;
}
}
socket_close( $sock );
echo "[client shutdown]\nsend data:" . format( $data_size );
function format( $byte = 0)
{
if ( $byte > 1024 * 1024) {
return ceil ( $byte / (1024 * 1024)) . "MB" ;
} elseif ( $byte > 1024) {
return ceil ( $byte / 1024) . "KB" ;
} else {
return $byte . " Byte" ;
}
}
|
Copy after login
This example shows transmitting a file to the server through a TCP short link. In an actual project, the communication module will not be so simple, and more issues need to be considered
For example:
Whether a long connection method is required (data is transmitted back and forth multiple times in a tcp link), data verification to prevent damage, and the size between different systems Endianness issues, custom communication protocols, packet sticking issues, timeout processing, concurrent access, flow control, TCP packet unpacking, etc.
If you want to learn more about PHP network programming and above All the above mentioned require systematic learning. It is recommended to take a look at the implementation of Workerman
It is a socket server framework written in PHP to help solve socket communication problems. You can use it to build an automatic Define the server, etc.
workermanThe official website address is: http://www.workerman.net/
The following two more viewing servers are provided Sample program for HTTP headers and browser headers:
The following example views the header information returned by the server, modifies the server address that needs to be viewed, and then accesses the script in the browser. Can:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php
error_reporting (E_ALL);
set_time_limit(0);
ob_implicit_flush(true);
$address = 'www.qq.com';
$port =80;
if (( $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n" ;
exit ();
}
if (socket_connect( $sock , gethostbyname ( $address ), $port ) === false) {
echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
exit ();
}
$msg = "GET / HTTP/1.1 \r\n" ;
$msg .= "Host: {$address}\r\n" ;
$msg .= "Connection: Close\r\n\r\n" ;
socket_write( $sock , $msg , strlen ( $msg ));
$str = "" ;
while ( $out = @socket_read( $sock , 2048*4)) {
$str .= $out ;
}
$str = explode ( "\r\n\r\n" , $str );
$str = $str [0];
if ( $str )
{
echo "<pre class=" brush:php;toolbar:false ">\r\n" . $str ."\r\n
|
";
}else{
echo "nothing";
}
//关闭套接字资源
socket_close($sock);
Copy after login
The following is an example of viewing the browser header, you can easily view the session information sent by the browser
First Make sure port 80 is closed, then modify the host file of the machine, direct the URL you want to access to the 127.0.0.1 address of the machine, use the php command line mode to start this script, and then use the browser to access the URL that has been directed. The program will always be open. If you need to close it, please use the ctrl+c key combination in the console. The program is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?php
error_reporting (E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '127.0.0.1';
$port = 80;
if (( $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n" ;
}
if (socket_bind( $sock , $address , $port ) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
}
if (socket_listen( $sock , 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
}
do {
if (( $msgsock = socket_accept( $sock )) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error( $sock )) . "\n" ;
break ;
}
echo "[client start " . date ( "Y-m-d H:i:s" ). "]\n\n" ;
do {
if (false === ( $buf = socket_read( $msgsock , 2048*6))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error( $msgsock )) . "\n" ;
break 2;
}
$talkback = "HTTP/1.1 200 OK\r\n\r\n" ;
$talkback .= date ( "Y-m-d H:i:s" ). "\nBrowser HTTP Headers:\n\n" . $buf . "\n" ;
socket_write( $msgsock , $talkback , strlen ( $talkback ));
echo "$buf\n" ;
break ;
} while (true);
socket_close( $msgsock );
} while (true);
socket_close( $sock );
?>
|
Copy after login
Related recommendations:
How to implement socket communication to obtain the local source port number in Linux
PHP Socket server construction and testing example sharing
Simple method to use Socket in PHP
The above is the detailed content of Detailed explanation of socket communication in php. For more information, please follow other related articles on the PHP Chinese website!