PHP program code for concurrently obtaining (stream_select) information

Source: Internet
Author: User
& Lt ;? Php $ events; if (array_key_exists (url, $ _ GET) {$ url $ _ GET [url] ;}$ hostsarray (www.xunlei.com, www.qq.com, www.163.com, www.baidu.com, www.kaixin.com, vip. xun
  1. $ Url = "www.ite5e.com ";
  2. If (array_key_exists (url, $ _ GET )){
  3. $ Url = $ _ GET [url];
  4. }
  5. $ Hosts = array ("www.xunlei.com", "www.qq.com", "www.163.com", "www.baidu.com", "www.kaixin.com", "vip.xunlei.com ");
  6. # $ Hosts = array ($ url );
  7. $ Timeout = 5;
  8. $ Status = array ();
  9. $ Retdata = array ();
  10. $ Sockets = array ();
  11. $ E = array ();
  12. /* Initiate connections to all the hosts simultaneously */
  13. Foreach ($ hosts as $ id => $ host ){
  14. $ Errno = 0;
  15. $ Errstr = "";
  16. $ S = stream_socket_client ("$ host: 80", $ errno, $ errstr, $ timeout, STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT );
  17. If ($ s ){
  18. $ Sockets [$ id] = $ s;
  19. $ Status [$ id] = "in progress ";
  20. } Else {
  21. $ Status [$ id] = "failed, $ errno $ errstr ";
  22. }
  23. $ Retdata [$ id] =;
  24. }
  25. /* Now, wait for the results to come back in */
  26. While (count ($ sockets )){
  27. $ Read = $ write = $ sockets;
  28. /* This is the magic function-explained below */
  29. $ N = stream_select ($ read, $ write, $ e, $ timeout );
  30. If ($ n> 0 ){
  31. /* Readable sockets either have data for us, or are failed connection attempts */
  32. Foreach ($ read as $ r ){
  33. $ Id = array_search ($ r, $ sockets );
  34. $ Data = fread ($ r, 8192 );
  35. If (strlen ($ data) = 0 ){
  36. If ($ status [$ id] = "in progress "){
  37. $ Status [$ id] = "failed to connect ";
  38. }
  39. Fclose ($ r );
  40. Unset ($ sockets [$ id]);
  41. } Else {
  42. $ Retdata [$ id]. = $ data;
  43. }
  44. }
  45. /* Writeable sockets can accept an HTTP request */
  46. Foreach ($ write as $ w ){
  47. If (! Is_resource ($ w) continue;
  48. $ Id = array_search ($ w, $ sockets );
  49. Fwrite ($ w, "GET/HTTP/1.0 Host:". $ hosts [$ id]. "");
  50. $ Status [$ id] = "waiting for response ";
  51. }
  52. } Else {
  53. /* Timed out waiting; assume that all hosts associated
  54. * With $ sockets are faulty */
  55. Foreach ($ sockets as $ id => $ s ){
  56. $ Status [$ id] = "timed out". $ status [$ id];
  57. }
  58. Break;
  59. }
  60. }
  61. Foreach ($ hosts as $ id => $ host ){
  62. # Echo "Host: $ host ";
  63. # Echo "Status:". $ status [$ id]. "";
  64. # Echo "Retdata:". $ retdata [$ id]. "";
  65. $ Strs = explode ("", $ retdata [$ id], 2 );
  66. Echo isset ($ strs [1])? $ Strs [1]: $ retdata [$ id];
  67. }
  68. Function debug ($ I ){
  69. Var_dump ($ I );
  70. Var_dump (gettype ($ I ));
  71. Var_dump (is_resource ($ I ));
  72. }
  73. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.