php cURL 到 localhost 返回在开放端口上被拒绝的权限

2024-04-13php开发问题
73

本文介绍了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 返回在开放端口上被拒绝的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17