我需要取多个跨域的页面地址的http状态码,如有以下网址:
http://www.baidu.com
http://www.163.com
http://www.qq.com需要一个页面,实时查看这三个网站是否正常(http状态200),求代码!

解决方案 »

  1.   

    get_headers -- 取得服务器响应一个 HTTP 请求所发送的所有标头
      

  2.   

    看具体应用吧,一般的,如果get_header【php version>=5】 能用上,下面的代码就足够了$url = 'http://www.qq.com';
    print_r(get_headers($url, 1));
    如果失败,可以模拟成浏览器请求,去读目标网址的内容echo urlGet("http://www.baidu.com");function urlGet($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);//
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        $tmpInfo = curl_exec($curl);
        if (curl_errno($curl)) {
           echo 'Errno'.curl_error($curl);
        }
        curl_close($curl);
        return $tmpInfo;
    }
      

  3.   

    你之前的帖子不是问过了吗?用Ajax实现。
    你这次的问题是想用php实现?cURL结合无限循环实现:
    <?php
    ignore_user_abort(true); //忽略用户输入
    set_time_limit(0); //不限制脚本的执行时间
    function getState($url){ //获取远程网页状态
    // Create a curl handle
    //$ch = curl_init('http://www.yahoo.com/');
    $ch = curl_init($url);
    // Execute
    curl_exec($ch);
    // Check if any error occured
    if(!curl_errno($ch)){
    $info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    print_r($info);
    }
    // Close handle
    curl_close($ch);
    }
    function just_do_it(){
    getState('http://www.yahoo.com/');
    usleep(2000000); //延迟2秒
    }
    ob_end_clean();
    ob_start();//开始缓冲数据
    while(1){ // 无限循环
    echo str_repeat(" ",1024);//ie有默认的1k buffer
    ob_flush();
    flush();
    just_do_it();
    }
    ?>