Apache cannot make outgoing HTTP Requests using curl

随声附和 提交于 2019-12-22 00:03:25

问题


Consider the following system configurations:

  • Fedora 27
  • SELinux enabled
  • Apache/2.4.33
  • PHP 7.1.17

And the below code snippet that is used to issue an http request to Sphere-Engine Compilers API:

$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);

The above script works completely fine when I run the following CLI:

php script.php

And I get the intended output.

However, when I try to run it through the web browser it produces:

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

I have found many suggestions such as adding

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

Which wasn't really a solution for me. Turning off SELinux which I will not do.

Note: The URL itself is working fine when I issue it and gives me the intended output too.

Any suggestions? Thanks in advance.


回答1:


Try this to see if SELinux will let the web server connect to the network:

getsebool httpd_can_network_connect

If not, allow it with

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



来源:https://stackoverflow.com/questions/50807700/apache-cannot-make-outgoing-http-requests-using-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!