php cURL 到 localhost 返回在开放端口上被拒绝的权限 [英] php cURL to localhost returns permission denied on an open port

查看:70
本文介绍了php cURL 到 localhost 返回在开放端口上被拒绝的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用 php cURL 库向端口 4321 上的 localhost 发出 cURL 请求时遇到权限被拒绝错误.希望这对于以前遇到此问题的人来说非常容易或明显.

I'm getting a permission denied error when trying to make a cURL request with the php cURL library to localhost on port 4321. This will hopefully be really easy or obvious for someone who's run into this before.

我能够从局域网上的另一个系统向生产服务器发出相同的 cURL 请求.例如,如果在局域网上的另一个系统上,我使用下面的函数发出请求,其中 $host='http://192.168.1.100:4321' 则一切正常.如果我在 $host='http://localhost:4321'$host='http://127.0.0.1:4321'$host='::1:4321' 然后我收到Permission Denied"的 cURL 错误

I'm able to make the identical cURL request from another system on the local area network to the production server. For example, if on another system on the local area network I make a request using the function below where $host='http://192.168.1.100:4321' then everything works exactly like it should. If I run on the system itself where $host='http://localhost:4321' or $host='http://127.0.0.1:4321' or $host='::1:4321' then I get a cURL error of "Permission Denied"

我为我非常简单的请求编写的函数是:

The function I wrote for my very simple request is:

function makeRequest($host,$data){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $host);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = json_decode(curl_exec($ch),true);
    if(!empty(curl_error($ch))){
        $result = print_r(curl_error($ch).' - '.$host);
    }
    curl_close($ch);
    return $result;
}

系统是一个centos 7服务器.运行 firewall-cmd --list-all 显示我的开放端口

The system is a centos 7 server. Running firewall-cmd --list-all shows my open ports

端口:443/tcp 80/tcp 4321/tcp

如果您有任何想法,或者需要我检查设置,请随时询问.

If you have some idea, or need me to check a setting don't hesitate to ask.

编辑主机文件看起来像

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

EDIT2

当我对同一个端口使用命令行 curl 时,一切都会恢复正常.

When I use commandline curl against the same port everything comes back alight.

 /]$ curl -v localhost:4321
* About to connect() to localhost port 4321 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 4321 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:4321
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: no-cache, no-store, must-revalidate
< Content-Length: 774....

推荐答案

我在以下位置找到了问题的答案:使用 Curl 发布 xml 时获取权限被拒绝?

I found the answer to the problem at: Getting permission denied while Posting xml using Curl?

问题是SELinux,解决方法是运行:

The problem is SELinux and the solution is to run:

sudo setsebool httpd_can_network_connect 1

我可以使用 php cURL 库访问世界上所有其他网站,但不能使用不同端口上的 localhost,而我能够从命令行 cURL 访问 localhost,这对我来说没有任何意义.

It doesn't make sense to me that I could use the php cURL library to access every other website in the world, but not localhost on a different port, while I was able to access the localhost from command line cURL.

这篇关于php cURL 到 localhost 返回在开放端口上被拒绝的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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