3

I am trying to execute curl command from php using exec() function but it gives me that error

curl: (7) couldn't connect to host

This is my code:

$command = "curl http://localhost:6821/schedule.json 
           -d project=default -d search=".$_POST["search_type"]." 
           -d url='".$url_info["url"]."' -d domain='".$domain."' -d               
           city='".$url_info["city"]."' -d state='".$url_info["state"]."' 2>&1";

$return = exec($command);
echo $return;

I am beginner with curl. I search and found there is a php library could perform curl command functionality but I don't know how to transfer this command line to php code using the curl library.

Mohamed Hana
  • 63
  • 1
  • 2
  • 11
  • probably isn't the current problem, but it will probably be your next problem: you're not escaping arguments for curl correctly here. search=".escapeshellarg($_POST["search_type"])."~~ – hanshenrik Feb 28 '16 at 01:52
  • where does it fail in this code? – hanshenrik Feb 28 '16 at 01:53
  • I tried it @hanshenrik and it gives me this warning connecting to server.. Warning: socket_connect() [function.socket-connect]: unable to connect [111]: Connection refused in /var/www/rssfeed/test_sac.php on line 16 socket_connect failed! – Mohamed Hana Feb 28 '16 at 03:51

1 Answers1

1

This may be your issue. Because curl is set to port 6821, it may be blocked. Does the same request to port 80 work? Check out this other thread.

How to resolve cURL Error (7): couldn't connect to host?

Community
  • 1
  • 1
Brian Mc
  • 141
  • 8