您的位置:首页 > 产品设计 > UI/UE

如果一个域名对应多个IP,取得指定IP下的网页内容

2009-05-08 01:56 627 查看
<?php
class Files
{
    public static function getUrlContentList($url, $ip = '')
    {
        if (empty ( $url )) {
            return false;
        }
        $parsed = parse_url ( $url );
        $host = $parsed ['host'];
        $uri = $parsed ['path'] ? $parsed ['path'] : '';
        $uri .= $parsed ['query'] ? '?' . $parsed ['query'] : '';
        $uri .= $parsed ['fragment'] ? '#' . $parsed ['fragment'] : '';
        unset ( $parsed );
        $ips = gethostbynamel ( $host );
        $rets = array ();
        if (! empty ( $ip )) {
            if (! in_array ( $ip, $ips )) {
                return false;
            } else {
                $rets [] = self::HttpVisit ( $ip, $host, $uri );
                return $rets;
            }
        }
        foreach ( $ips as $ip ) {
            $rets [] = self::HttpVisit ( $ip, $host, $uri );
        }
        return $rets;
    }
    
    public static function HttpVisit($ip, $host, $uri)
    {
        $errstr = '';
        $errno = '';
        $fp = fsockopen ( $ip, 80, $errno, $errstr, 90 );
        if (! $fp) {
            return false;
        } else {
            $out = "GET {$uri} HTTP/1.1/r/n";
            $out .= "Host:{$host}/r/n";
            $out .= "Connection: close/r/n/r/n";
            fputs ( $fp, $out );
            while ( $line = fread ( $fp, 4096 ) ) {
                $response .= $line;
            }
            fclose ( $fp );
            //去掉Header头信息
            $pos = strpos ( $response, "/r/n/r/n" );
            $response = substr ( $response, $pos + 4 );
            return $response;
        }
    }
}

/*
调用方法:
$data = Files::getUrlContentList('http://xxx/xxx.htm', '192.168.xxx.xxx');
print_r($data);
 */
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  fp function url query path header