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

时间:2018-06-12 00:20:48

标签: php apache curl selinux php-curl

请考虑以下系统配置:

  • Fedora 27
  • 启用SELinux
  • 的Apache / 2.4.33
  • PHP 7.1.17

以下用于向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

我得到了预期的输出。

但是,当我尝试通过Web浏览器运行它时会产生:

  

CURLE_COULDNT_CONNECT(7)无法连接()到主机或代理。

我找到了许多建议,例如添加

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

这对我来说不是一个真正的解决方案。关闭我将的SELinux。

注意:发布时,网址本身也正常工作,并且也为我提供了预期的输出。

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

1 个答案:

答案 0 :(得分:2)

尝试此操作以查看SELinux是否允许Web服务器连接到网络:

  

getsebool httpd_can_network_connect

如果没有,请使用

  

setsebool -P httpd_can_network_connect

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