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

查看:342
本文介绍了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',然后出现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.

编辑
主机文件看起来像

EDIT The hosts file looks like

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

编辑2

当我在同一端口上使用命令行卷曲时,一切都会消失。

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....


推荐答案

我发现了问题的答案在:
发布时被拒绝的权限

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天全站免登陆