PHP cURL使用IP地址代替域名进行连接 [英] PHP cURL connect using IP address instead domain name

查看:360
本文介绍了PHP cURL使用IP地址代替域名进行连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一数据中心中运行了几台启用了内部网络的服务器。为了速度和成本(不对内部流量收费),我希望能够在其内部IP上的各个服务器之间进行cURL。

I run several servers in the same data center that have internal networking enabled. In the interests of speed and costs (internal traffic is not billed) I would like to be able to cURL between the various servers on their internal IPs.

我刚刚发现那如果我尝试类似的东西

I have just discovered that if I attempt something along the lines of

curl_init('10.x.x.x);

甚至

curl_init():
curl_setopt(CURLOPT_URL,'https://10.x.x.x/scripttorun.php');

不起作用。当我只有极少量的数据要发送时,我可以将其附加为查询字符串并执行file_get_contents。但是,当数据量太大而无法作为GET请求发送时,如果我不能使用cURL,事情就会变得更加混乱。

does not work. When I have only a very small quantity of data to send out I can append it as a query string and do a file_get_contents. However, when the amount of data is too big to send out as a GET request things get messier if I cannot use cURL.

一个搜索点表明,我可以通过编辑/ etc / hosts表来说服cURL使用IP。问题是内部网络上的各种服务器可能会以不可预见的间隔打开和关闭,因此我不得不定期轮询以查看哪些服务器处于联机状态,编写脚本来更新/ etc / hosts ...

A spot of searching suggested that I could persuade cURL to use IPs by editing the /etc/hosts table. The problem is that the various servers on the internal network may go on and off line at unpredicatable intervals so I would end up having to poll regularly to see which servers are online, write a script to update /etc/hosts... which gets pretty messy.

也许有一个简单的解决方案可以说服cURL使用IP。毕竟,file_get / put_contents不会退缩。如果可能必须解决cURL的人,我将非常愿意

Perhaps there is a simple solution that can persuade cURL to use IPs. After all, file_get/put_contents does so without flinching. I'd be most obliged to anyone who might be able workaround for cURL

推荐答案

如果您必须连接到服务器并询问特定的服务器名称,可以使用curl setopt参数来完成。如果要从特定IP从www.mytopsite.com获取文件/index.php(不添加任何/ etc / hosts映射),则可以这样操作:

If you ever have to connect to a server and ask for particular server name you can do it with curl setopt parameters. If you want to get file /index.php from www.mytopsite.com from particular IP (without adding any /etc/hosts mappings) you can do it like this:

$headers = array("Host: www.myfaketopsite.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://10.1.2.3/index.php");

这篇关于PHP cURL使用IP地址代替域名进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆