& 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
-
- $ Url = "www.ite5e.com ";
- If (array_key_exists (url, $ _ GET )){
- $ Url = $ _ GET [url];
- }
- $ Hosts = array ("www.xunlei.com", "www.qq.com", "www.163.com", "www.baidu.com", "www.kaixin.com", "vip.xunlei.com ");
- # $ Hosts = array ($ url );
- $ Timeout = 5;
- $ Status = array ();
- $ Retdata = array ();
- $ Sockets = array ();
- $ E = array ();
- /* Initiate connections to all the hosts simultaneously */
- Foreach ($ hosts as $ id => $ host ){
- $ Errno = 0;
- $ Errstr = "";
- $ S = stream_socket_client ("$ host: 80", $ errno, $ errstr, $ timeout, STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT );
- If ($ s ){
- $ Sockets [$ id] = $ s;
- $ Status [$ id] = "in progress ";
- } Else {
- $ Status [$ id] = "failed, $ errno $ errstr ";
- }
- $ Retdata [$ id] =;
- }
- /* Now, wait for the results to come back in */
- While (count ($ sockets )){
- $ Read = $ write = $ sockets;
- /* This is the magic function-explained below */
- $ N = stream_select ($ read, $ write, $ e, $ timeout );
- If ($ n> 0 ){
- /* Readable sockets either have data for us, or are failed connection attempts */
- Foreach ($ read as $ r ){
- $ Id = array_search ($ r, $ sockets );
- $ Data = fread ($ r, 8192 );
- If (strlen ($ data) = 0 ){
- If ($ status [$ id] = "in progress "){
- $ Status [$ id] = "failed to connect ";
- }
- Fclose ($ r );
- Unset ($ sockets [$ id]);
- } Else {
- $ Retdata [$ id]. = $ data;
- }
- }
- /* Writeable sockets can accept an HTTP request */
- Foreach ($ write as $ w ){
- If (! Is_resource ($ w) continue;
- $ Id = array_search ($ w, $ sockets );
- Fwrite ($ w, "GET/HTTP/1.0 Host:". $ hosts [$ id]. "");
- $ Status [$ id] = "waiting for response ";
- }
- } Else {
- /* Timed out waiting; assume that all hosts associated
- * With $ sockets are faulty */
- Foreach ($ sockets as $ id => $ s ){
- $ Status [$ id] = "timed out". $ status [$ id];
- }
- Break;
- }
- }
- Foreach ($ hosts as $ id => $ host ){
- # Echo "Host: $ host ";
- # Echo "Status:". $ status [$ id]. "";
- # Echo "Retdata:". $ retdata [$ id]. "";
- $ Strs = explode ("", $ retdata [$ id], 2 );
- Echo isset ($ strs [1])? $ Strs [1]: $ retdata [$ id];
- }
- Function debug ($ I ){
- Var_dump ($ I );
- Var_dump (gettype ($ I ));
- Var_dump (is_resource ($ I ));
- }
- ?>