3

I have to post a xml to a url to be consumed by some web service. I am using Cent OS 5.6 Apache/2.2.3 (CentOS). When I post from command line using curl-d@"abc.xml" http://example.com It gives me result. But when I post from php using curl

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

It gives me curl error permission denied. Any idea why I am getting this from code while able to access the url from CLI using curl -d. Thanks in Advance

Athafoud
  • 2,898
  • 3
  • 40
  • 58
Puneet
  • 615
  • 3
  • 9
  • 23
  • Can you post the full error, For example is it `ERROR 7 Failed to connect to Permission denied` ?. Since you are in Centos maybe SELinux is the problem. In this case I an guessing that SELinux blocks all the requests from apache to the outer world. – Athafoud Aug 16 '14 at 09:13
  • I am not getting any code. just getting Failed to connect to 10.80.184.195: Permission denied – Puneet Aug 16 '14 at 10:19
  • Thats fine, try to issue the following command `setsebool -P httpd_can_network_connect 1 ` this will allow curl to connect through httpd. Also you can check the status of httpd SELinux policy with this `getsebool -a | grep httpd | grep off$` command – Athafoud Aug 16 '14 at 10:37
  • Sorry to ask again.I have not root access to run this command. I have raised a ticket to the concern person he will check and revert. But one thing is that i am able to curl another API's from same server and same code. All other are workign fine. – Puneet Aug 16 '14 at 10:49
  • You did not mentioned that everything else work fine, in that case maybe is not a SELinux misconfiguration problem. – Athafoud Aug 16 '14 at 11:06
  • setsebool -P httpd_can_network_connect 1 solved my problem. – Puneet Aug 22 '14 at 05:46

1 Answers1

7

This solve my problem

setsebool -P httpd_can_network_connect 1
Saad
  • 3,340
  • 2
  • 10
  • 32
Puneet
  • 615
  • 3
  • 9
  • 23
  • This helped me. https://stackoverflow.com/questions/46672070/php-curl-to-localhost-returns-permission-denied-on-an-open-port/46678301#46678301 – Altimus Prime Oct 11 '17 at 01:22