3

So here's the scenario.

1) On my local machine that is running MAMP, I'm not facing any issue and I'm able to successfully get the response along with CURLINFO_HTTP_CODE=200

2) However, there is a remote server on which my PHP code is deployed. If I try to run the same thing (as I've mentioned in #1) I get an error that says

curl_error = Failed to connect to my_ip port my_port: Connection refused with CURLINFO_HTTP_CODE = 7

$url = 'myURL';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,30); //timeout in seconds
curl_setopt($ch,CURLOPT_TIMEOUT, 20); // same for here. Timeout in seconds.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
);

$data_string = {"name":"mark"} `

LEE
  • 3,335
  • 8
  • 40
  • 70
  • Well, the error message is pretty clear, isn't it? That target server refuses a connection. Most likely there is a firewall in between. Or that server does not server all requests / interfaces. If in doubt you should simply check that servers request log file. – arkascha Jan 07 '18 at 18:58
  • @arkascha Yes, what you said is right. But then why doesn't the same issue occur when I'm on my localhost? – LEE Jan 07 '18 at 19:03
  • Well, obviously the view on the network topology is different from that development system. I cannot tell you why, only you know that network. – arkascha Jan 07 '18 at 19:29

1 Answers1

-1

In my case, call API from localhost was successful, but when i call API from online website i get error(connection refused). that's because the IP of API was available on our internal network(company network), not on the internet. i know it's silly mistake.

reza jafari
  • 1,228
  • 13
  • 14