select.php 754 B

123456789101112131415161718192021222324
  1. <?php
  2. go(function () {
  3. Swoole\Runtime::enableCoroutine();
  4. $fp1 = stream_socket_client("tcp://www.baidu.com:80", $errno, $errstr, 30);
  5. $fp2 = stream_socket_client("tcp://www.qq.com:80", $errno, $errstr, 30);
  6. if (!$fp1) {
  7. echo "$errstr ($errno)<br />\n";
  8. } else {
  9. fwrite($fp1, "GET / HTTP/1.0\r\nHost: www.baidu.com\r\nUser-Agent: curl/7.58.0\r\nAccept: */*\r\n\r\n");
  10. $r_array = [$fp1, $fp2];
  11. $w_array = $e_array = null;
  12. $n = stream_select($r_array, $w_array, $e_array, 10);
  13. var_dump($r_array, $n);
  14. $html = '';
  15. while (!feof($fp1)) {
  16. $html .= fgets($fp1, 1024);
  17. }
  18. var_dump(strlen($html));
  19. fclose($fp1);
  20. }
  21. });
  22. swoole_event_wait();