search
Homephp教程php手册同域名多IP下PHP获取远程网页内容的函数

同域名多IP下PHP获取远程网页内容的函数

Jun 13, 2016 am 11:38 AM
phpDowncontentfunctiondomain namecollegeBuild a websiteTutorialofWeb pageObtainRemotely

  烈火建站学院(Bkjia.Com)PHP教程 PHP获取远程网页内容有多种方式,例如用自带的file_get_contents、fopen等函数。

echo file_get_contents("http://www.bkjia.com/abc.php");  
?>

  但是,在DNS轮询等负载均衡中,同一域名,可能对应多台服务器,多个IP。假设blog.kcoffee.net被DNS解析到72.249.146.213、72.249.146.214、72.249.146.215三个IP,用户每次访问blog.kcoffee.net,系统会根据负载均衡的相应算法访问其中的一台服务器。

  上周做一个视频项目时,就碰到这样一类需求:需要依次访问每台服务器上的一个PHP接口程序(假设为abc.php),查询这台服务器的传输状态。

  这时就不能直接用file_get_contents访问blog.kcoffee.net/abc.php了,因为它可能一直重复访问某一台服务器。

  而采用依次访问http://72.249.146.213/abc.php、http://72.249.146.214/abc.php、http://72.249.146.215/abc.php的方法,在这三台服务器上的Web Server配有多个虚拟主机时,也是不行的。

  通过设置本地hosts也不行,因为hosts不能设置多个IP对应同一个域名。

  那就只有通过PHP和HTTP协议来实现:访问abc.php时,在header头中加上blog.kcoffee.net域名。于是,我写了下面这个PHP函数。

以下为引用的内容:
/************************ 
* 函数用途:同一域名对应多个IP时,获取指定服务器的远程网页内容 
* 创建时间:2008-12-09 
* 创建人:张宴(blog.s135.com) 
* 参数说明: 
*    $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("72.249.146.213", "blog.s135.com", "/abc.php");  
$server_info2 = HttpVisit("72.249.146.214", "blog.s135.com", "/abc.php");  
$server_info3 = HttpVisit("72.249.146.215", "blog.s135.com", "/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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)