Home  >  Article  >  Backend Development  >  PHP dns polling function to obtain remote web page content

PHP dns polling function to obtain remote web page content

WBOY
WBOYOriginal
2016-07-25 08:56:48842browse
This article shares a function to obtain the content of remote web pages in a dns polling environment. Friends in need can refer to it.

If the same domain name corresponds to multiple IPs, the content of the remote web page can be obtained through PHP and HTTP protocols. For example, when accessing abc.php, add the bbs.it-home.org domain name to the header.

Example:

<?php
/*
* 函 数:同一域名对应多个IP时,获取指定服务器的远程网页内容
* 参数说明:
*    $ip   服务器的IP地址
*    $host   服务器的host名称
*    $url   服务器的URL地址(不含域名)
* 返回值:
*    获取到的远程网页内容
*    false   访问远程网页失败
*/
function HttpVisit($ip, $host, $url)
{
    $errstr = '';
    $errno = '';
    $fp = fsockopen ($ip, 80, $errno, $errstr, 90);
    if (!$fp)
    {
         return false;
    }
    else
    {
        $out = "GET {$url} 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;
    }
}

//调用方法:
$server_info1 = HttpVisit("59.112.33.213", "bbs.it-home.org", "/abc.php");
$server_info2 = HttpVisit("59.112.33.214", "bbs.it-home.org", "/abc.php");
$server_info3 = HttpVisit("59.112.33.215", "bbs.it-home.org", "/abc.php");
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn