[文章作者:张宴 本文版本:v1.0 最后修改:2008.12.15 转载请注明原文链接:http://blog.zyan.cc/post/389/]

  PHP获取远程网页内容有多种方式,例如用自带的file_get_contents、fopen等函数。
引用
<?php  
echo file_get_contents("http://blog.zyan.cc/abc.php");  
?>

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

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

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

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

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

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

Tags:



技术大类 » PHP/JS/Shell | 评论(59) | 引用(1) | 阅读(75912)
xk
2008-12-15 08:43
沙发.
Bendy Homepage
2008-12-15 17:33
呵..这个比较简单.但可能会很实用...
FinalBSD Homepage
2008-12-15 17:57
干脆再封装一下,使用gethostbynamel获得这个域名对应的所有IP地址,然后直接用域名调用如何?省的一个个指定IP地址。
itloveboy Email
2008-12-15 18:49
张宴您好!
看了你得很多的文章,学习了很多的知识,首先我向你表示感谢,:)
我看过你的一篇关于 中南民族大学 PHPWIND论坛设计的文章,我在去年也是跟你做得实名认证差不多,但是我的程序还有很多的不足,最重要的也是我想向您请教的,就是关于数据和程序加密的,我看了你的展示里面把学生的和老师的个人数据都加密了,而且把程序也加密了,我想知道这是怎么做到的,如果方便的话,就告诉我一下吧,期待您的回答!
peter Email Homepage
2008-12-19 04:29
感谢博主~
jk
2008-12-20 16:18
这个请求的时候要用HTTP/1.0吧~
玉面修罗 Homepage
2008-12-24 11:14
可以封装一下..
PHP能并发执行这类任务..
特别是当主机数量庞大的时候,并发执行的效率高很多.
不羁的风
2008-12-27 12:51
很享受
稳压器 Homepage
2009-1-19 15:00
不错 支持








稳压器
域名 Homepage
2009-4-10 14:24
这个满实用的,我收藏了··
crystal Homepage
2009-4-10 14:25
这应该是楼主的经验总结的··很不错噢··
鱼儿 Homepage
2009-4-10 14:27
原来楼主叫张宴···哈哈 好新颖的名字·
cykzl
2009-4-23 11:22
博主才毕业两年,有这样的成就佩服、、、、、
图书批发
2009-9-16 09:56
学习一下,研究研究。--图书批发
sinopower
2010-2-5 16:38
dig +short 解析出来,cname不行
暴力王
2010-5-3 17:38
为什么不直接使用curl呢? curl也能实现这样的效果的.
hfcorriez Email
2010-9-20 21:57
看了张晏的博客,受益匪浅。感谢。。。学习
dresses Email Homepage
2011-7-26 10:20
Here are a variety of the empire <a href="http://www.romancearound.com/">dresses</a>, I want to introduce yourself. The first shoulder chiffon <a href="http://www.romancearound.com/bridesmaid-dresses-c-197.html">bridesmaid dresses</a> empire style wedding will <a href="http://www.romancearound.com/wedding-dresses-trumpet-wedding-dresses-c-180_253.html">trumpet wedding dresses</a>.This Tia cowries, dressed in her wedding day.Chiffon of the empire the best clothes, because they are the size of the drum as complete motion from the wind the site makes it easy to create a plan impression.One romantic flowers floating above shoulder function and help catch the eyes of the cut of the chest and neck problems, so that more comprehensive coverage of physical illusion bust.Clothing do not like big long torso, short legs, or pear-shaped<a href="http://www.romancearound.com/bridesmaid-dresses-ankle-length-bridesmaid-dresses-c-197_198.html">ankle length bridesmaid dresses</a> figure.special is also suitable for pregnant brides since the mobility as a dress can accommodate more Many of the abdomen.
KingGoo.Com Email Homepage
2011-9-1 17:27
之前弄这个东西,忘记把这个文件删除了。今天测试我新域名,突然发现nginx无法屏蔽版本号~直接curl -I url  不限制,但直接访问域名就显示版本好!我汗~~~后来才知道,默认页是你上面那个东东~~~原来你的是1.10   1.1内存消耗好小
Louis Vuitton Neverfull Email Homepage
2012-4-21 17:36
www.lvbagsclassic.com are authorized authentic Louis Vuitton handbags outlet store. All the items at our site are 100% authentic. All our Louis Vuitton handbags will come with the authenticity card, serial Number, dust bag and care booklet. We promise you will be 100% satisfied when you get such cheap authentic Louis Vuitton handbags from us!
Louis Vuitton Neverfull
分页: 1/3 第一页 1 2 3 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]