同域名多IP下PHP获取远程网页内容的函数
烈火建站学院(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函数。
以下为引用的内容: |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)