As a non-low-level programmer, it is very difficult to deeply understand the internal implementation mechanism of socket. We only need to understand that socket is a set of functions encapsulated by the operating system to implement process communication. It is enough to create and call it. .
The language characteristics and positioning of PHP determine that it is only suitable for socket clients, not for socket servers.
Because socket is mainly oriented to the bottom layer and network service development, the server side is generally implemented in languages such as C or Java. This can better operate the bottom layer and solve problems encountered in network service development. There are also mature and complete solutions to problems (such as concurrency, blocking, etc.), but PHP is obviously not suitable for this application scenario. (Recommended study: PHP video tutorial)
In fact, PHP operates the MySQL database through socket. This is precisely because the socket shields the underlying protocol, making network services Interconnection between them becomes simple.
In addition to sockets implemented in traditional server-side languages, with the popularity of HTML5, WebSockets implemented in browser clients are also gradually emerging. This is worthy of attention. FlashSocket is also a good solution. .
To operate the socket on the client, you can use functions such as fsockopen, socket_create or stream_socket_client. If it is PHP5, it is recommended to use stream_socket_client.
Socket interactive application example: using socket to submit a form
Create a new test.php file and send it to http://demo.com/index .php?id=1 Submit form data, the code is as follows:
<?php $data = array('comment'=>'this is a robot comment'); $data = http_build_query($data); $out = "POST http://demo.com/index.php?id=1 HTTP/1.1\r\n"; // 通过POST方式发送数据 $out .= "Host: demo.com\r\n"; $out .= "Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n"; $out .= "Content-length: ".strlen($data)."\r\n"; $out .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:48.0) Gecko/20100101 Firefox/48.0"."\r\n"; $out .= "Connection: close"."\r\n"."\r\n"; // 注意:此处有两个 \r\n $out .= $data."\r\n"; // 正文数据 $fp = fsockopen("demo.com", 80, $errno, $errstr, 30); // 创建socket客户端连接 // $fp = stream_socket_client("tcp://demo.com:80", $errno, $errstr, 30); 推荐这种写法 fwrite($fp, $out); // 向服务器发送数据 while (!feof($fp)) { echo fgets($fp, 1280); // 读取服务器响应的数据 } fclose($fp); // 关闭socket连接 ?>
You need to pay attention to the following points:
The first parameter of fsockopen can also use the IP address , do not bring http:// string, unless using SSL, etc.
Request headers do not necessarily need to bring all header fields, generally only need to bring a few core headers
At the last header, there are two line breaks after Connection
Pay attention to encoding issues
The above is the detailed content of Why is php not suitable for sockets?. For more information, please follow other related articles on the PHP Chinese website!