PHP Curl 无法连接到自己服务器上的主机

发布于 2024-11-06 07:45:00 字数 817 浏览 1 评论 0原文

我有一个 PHP 脚本,它使用 cURL 来访问也位于我的服务器(安装的电子邮件营销程序)上的文件。我编写了一个快速脚本来测试我的 cURL 安装,但当指向我自己的服务器上的文件时,它失败了。脚本是:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mysite.com/test.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec($ch);

if($contents) {
    echo $contents;
} else {
    $err = curl_error($ch);
    echo $err;
}

curl_close($ch);

当我运行此代码时,它返回错误:无法连接到主机

如果我将 URL 更改为http://www.google.com或任何其他网站,它工作得很好。另外,当我尝试通过另一台服务器上的 cURL 打开特定页面时,它的工作原理如下。我从这些测试中做出的假设是 PHP cURL 已安装并且(有点)工作,并且问题不在尝试打开的页面中。

我认为这可能是端口问题,所以我尝试指定具有相同结果的端口(未连接到主机)。我尝试了 curl_setopthttp://www.mysite.com:80/ 来指定我知道要打开的端口。

这让我相信问题出在我的 apache 安装中,但我不是 apache 专家,而且我一整天都在用头撞谷歌,但无济于事。有什么想法可能导致这种特定的 cURL 失败吗?

I have a PHP script that uses cURL to access a file also located on my server (installed email marketing program). I wrote a quick script to test my cURL installation and it fails when pointed at a file on my own server. The script is:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mysite.com/test.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec($ch);

if($contents) {
    echo $contents;
} else {
    $err = curl_error($ch);
    echo $err;
}

curl_close($ch);

When I run this code, it returns an error: couldn't connect to host

If I change the URL to http://www.google.com or any other site, it works just fine. Also, when I try to open the specific page via cURL on another server, it works like it should. The assumptions I'm making from these tests is that the PHP cURL is installed and (kinda) working and that the problem is not in the page that is trying to be opened.

I thought it might be a port issue, so I tried specifying the port with the same result (not connecting to host). I tried both curl_setopt and http://www.mysite.com:80/ to specify a port I know to be open.

This leads me to believe that the issue is something in my apache installation, but I'm not an apache expert and I've been banging my head against Google all day to no avail. Any ideas what could cause such a specific cURL failure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羁绊已千年 2024-11-13 07:45:00

遇到了同样的问题,我的域名无法在服务器上解析,导致对自身的 CURL 请求失败。

我的问题是,由于某些防火墙规则,我的 Web 服务器无法自行解析(Web 服务器前面有一个负载均衡器阻止来自服务器的流量)。

换句话说,在我的服务器上运行以下命令

curl -I http://www.mywebserver.com 结果是:

$ curl -I http://www.mywebserver.com
curl: (7) 无法连接到主机

更新服务器上的 /etc/hosts 文件以将域名映射到本地 IP 地址解决了该问题,并且 fopencurl 再次发挥作用。

Ran into the same issue where my domain name would not resolve on the server causing CURL requests to itself to fail.

My problem was that my web server could not resolve to itself due to some firewall rules (there is a load balancer in front of the web server that was blocking traffic from the server).

In other words, running the following command on my server

curl -I http://www.mywebserver.com resulted in:

$ curl -I http://www.mywebserver.com
curl: (7) couldn't connect to host

Updating the /etc/hosts file on the server to map the domain name to the local ip address resolved the issue and fopen and curl from within my scripts were functional again.

薄情伤 2024-11-13 07:45:00

您可以 ping www.mysite.com 吗?

检查您的 /etc/hosts ...也许为 www.mysite.com 指定了错误的 IP。

Can you ping www.mysite.com ?

Check your /etc/hosts ... maybe there is a wrong IP specified for www.mysite.com.

╭⌒浅淡时光〆 2024-11-13 07:45:00

尝试在网络浏览器中打开http://www.mysite.com/test.php。如果有效,则问题出在 cURL 上。如果没有,则问题出在您的网络或 apache 配置上。

您可以通过确保 apache 正在您的系统上运行来检查您的 apache 配置。然后尝试 telnet80 从命令行。连接后,尝试执行以下操作:

GET /test.php HTTP/1.1
Host: www.mysite.com
<2 line breaks>

如果您收到 apache 的响应,则听起来像是网络问题。确保 mysite.com 解析为您的 apache 服务器的 IP 地址,并且端口 80 流量可以到达您的服务器。这也可能是您的路由器尝试向其自己的 IP 地址发出请求的环回路由问题(假设您的 apache 机器位于路由网络连接后面,与您所在的连接相同)。

Try to open http://www.mysite.com/test.php in a web browser. If it works, then the problem is with cURL. If it doesn't, then the problem is either with your network, or apache configuration.

You can check your apache configuration by making sure that it is running on your system. Then try to telnet <apache server ip> 80 from the command line. Once connected try to do the following:

GET /test.php HTTP/1.1
Host: www.mysite.com
<2 line breaks>

If you get a response from apache, then it sounds like a network issue. Make sure mysite.com resolves to the IP address of your apache server, and port 80 traffic can get to your server. It could also be a loopback routing issue of your router trying to make a request to it's own ip address (assuming your apache box is behind a routed network connection, the same connection that you are on).

若言繁花未落 2024-11-13 07:45:00

我在远程网络服务器上也遇到了这个问题。在这台机器上运行curl,我无法访问同一台机器上的apache 服务器托管的文件。奇怪的是我可以访问文件

curl -O http://example.com/file1.txt

curl -0 http://sub.example.com/file1.txt

不起作用。

我通过将缺少的 DNS 记录添加到计算机上运行的 DNS 服务器来解决这个问题。似乎 nslookup 等在询问其他人之前首先查看本地 DNS 服务器。当本地 DNS 服务器回复“找不到”时,curl 放弃了。

I ran into this as well on a remote webserver. Running curl on this machine I wasn't able to access files hosted by an apache server on the same machine. Strange this was I could access file by

curl -O http://example.com/file1.txt

but

curl -0 http://sub.example.com/file1.txt

did not work.

I solved it by adding the missing DNS record to the DNS server running on the machine. Seems like nslookup etc first have a look at the local DNS server before asking somebody else. And as the local DNS server replied with "can't find" curl gave up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文