Apache 无法使用 curl 发出传出 HTTP 请求

Apache cannot make outgoing HTTP Requests using curl

考虑以下系统配置:

下面的代码片段用于向 Sphere-Engine Compilers API 发出 http 请求:

$ch = curl_init('http://xxxxxxxx.compilers.sphere-engine.com/api/v4/test?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 3.0);
$x = curl_exec($ch);
print_r($x);

当我 运行 以下 CLI 时,上述脚本完全正常工作:

php script.php

我得到了预期的输出。

但是,当我尝试通过网络浏览器 运行 它时,它会生成:

CURLE_COULDNT_CONNECT (7) Failed to connect() to host or proxy.

我发现了很多建议,例如添加

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

这对我来说并不是真正的解决方案。关闭我 不会 的 SELinux。

注意:当我发出 URL 时,它本身工作正常,也给了我预期的输出。

有什么建议吗?提前致谢。

试试看 SELinux 是否会让 Web 服务器连接到网络:

getsebool httpd_can_network_connect

如果不行,允许

setsebool -P httpd_can_network_connect on

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-managing_confined_services-the_apache_http_server-booleans