I am getting this error in my PHP Curl
* Hostname in DNS cache was stale, zapped
* Trying xxx.xx.x.xx...
* connect to xxx.xx.x.xx port 4005 failed: Connection refused
* Failed to connect to sample.website.net.au port 4005: Connection refused
* Closing connection 0
I have this code:
$ch = curl_init();
$headers = array(
'Content-type: text/xml',
'charset: utf-8',
'SOAPAction: urn:ServiceQualification#AddresSsearch',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); // This should always be set to 'TRUE' when in production to ensure the SSL is enabled.
$response = curl_exec ($ch);
if ($result === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
htmlspecialchars(curl_error($ch)));
}
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
curl_close ($ch);
return $response;
I don't know why I am having this error. I tried this one in my localhost, and it works perfectly, but when I uploaded it in my server, I am getting the said php curl error.
What seems to be the problem here? Do I need to contact the url that I am trying to connect?
I am banging my head now, your help will be greatly appreciated.
Thanks!
Connection refused:
Blocked by local
Check if you may connect outgoing to port 4005. Possibly it's your firewall that blocks you.
Blocked by remote
Check if you may connect outgoing to port 4005. Possibly it's the remote firewall that blocks you.
Help
Check if you can ping the remote host via your terminal:
ping 1.2.3.4 // this is the ip, you can also use the hostname.
Try connecting the remote host with specific port via your terminal:
telnet 1.2.3.4 4005
This is more easy than debugging via php code.
Related
I would like to develop a script which, at the submit of a form, send a request to an url using ip adress and port in order to send an sms via the gateway configured on my phone.
The url I have
$url = "192.168.1.31:43382/send.html?smsto=XXXXXXXXXX&smsbody=test&smstype=sms";
And my cURL code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
$result = curl_exec($ch);
curl_close($ch);
It works fine localy until I put it on my server.
When I log verbose's value, I get this :
Hostname was NOT found in DNS cache
Trying 192.168.1.31...
connect to 192.168.1.31 port 43382 failed: Connection timed out
Failed to connect to 192.168.1.31 port 43382: Connection timed out
Closing connection 0
I've seen that it could be a firewall issue, so I disabled it but nothing new.
I'm new to cURL, if someone could guide me...
Thanks
I seem to be having an issue with a single url endpoint while cUrl from local xampp.
I get an error message of Failed to connect to localhost port 1080: Connection refused even when I set curl_setopt($ch, CURLOPT_PORT, 80);.
But this only happens from a particular url (without ssl http://), other urls with https:// is fine. And all other cUrl to different endpoints are fine.
On a live server, the problematic endpoint, same code everything works fine.
Can anyone help me understand why this might be happening?
My http.conf: ServerName localhost:80
complete code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PORT, $_SERVER['SERVER_PORT']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// "Cache-Control: max-age=0",
// "Connection: keep-alive",
// "Keep-Alive: 300",
"Content-Type: application/json",
'Api-Key: ' . $apiKey,
));
$output = curl_exec($ch);
//curl_easy_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$errmsg = curl_error ( $ch );
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$httpHeaders = curl_getinfo($ch);
curl_close($ch);
print_r($ch);
echo $httpcode;
print_r($httpHeaders);
print_r($errmsg); // Failed to connect to localhost port 1080: Connection refused
after spending the entire day, the fix was to set an empty value for proxy and use port 80
curl_setopt($ch, CURLOPT_PROXY, ''); //!! FIX
curl_setopt($ch, CURLOPT_PORT, $_SERVER['SERVER_PORT']);
I had the same issue , i solved it by running apache on other port, you should to edit your httpd.conf
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"} `
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.
I am using below code and getting "Failed to connect to api.textlocal.in port 80: Connection timed out" error. I was able to use the same y'day as well but now suddenly facing the issue. I am using Godaddy's shared hosting. I have checked with them they are saying so such firewall blockage is there also checked from API vendor Textlocal they also told the same(No IP restriction or firewall blockage). Could some one help me out from here.
I can get response from google.com as in commented block.
$ch = curl_init('http://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
var_dump("Curl Error No-".curl_errno($ch));
var_dump("<br/>Curl Error Description-".curl_error($ch));
curl_close($ch);
/*
$ch = curl_init("http://google.com"); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
*/
Here I have tried to get curl request and it works on some servers and did not work in some servers, but it works in local host.
when I print the error msg it says
"Failed to connect to api.lankagate.gov.lk port 8280: Connection refused".
Here is the my function:
function get_on_going_vehicle_number() {
$vehicle_category = 1;
$xml_data = '<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://schemas.conversesolutions.com/xsd/dmticta/v1">
<soapenv:Header/>
<soapenv:Body>
<v1:GetOnGoingVehicleNo>
<v1:vehicleCategory>' . $vehicle_category . '</v1:vehicleCategory>
</v1:GetOnGoingVehicleNo>
</soapenv:Body>
</soapenv:Envelope>';
$URL = "http://api.lankagate.gov.lk:8280/GetOnGoingVehicleNoDMT/1.0";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml',
'Authorization: Bearer 7655a410-d019-3f09-b214-08c40e737af0'
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT,8280);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
echo $error_msg;
}
return $output;
curl_close($ch);
}
we try to telnet:
working server: > telnet api.lankagate.gov.lk 8280 Trying 43.224.126.66...
Connected to api.lankagate.gov.lk.
Escape character is '^]'.
Connection closed by foreign host. Non working server:
user#dwp-s01:~$ telnet api.lankagate.gov.lk 8280
Not working server:
Trying 192.168.12.117...
telnet: Unable to connect to remote host: Connection refused
user#dwp-s01:~$
It'tries to different Ip : 192.168.12.117
Please give me a solution
There could be multiple issues here.
api.lankagate.gov.lk has firewall enabled.
Port 8280 is not free.
I guess you can't disable the firewall on api.lankagate.gov.lk. So, please try some other port.
192.168.12.117 seems local network.
So, you might have pointed this ip in your host file and have to remove that
Example:
c:\Windows\System32\Drivers\etc\hosts or /etc/hosts
192.168.12.117 api.lankagate.gov.lk
Or your network router or proxy (192.168.12.117) blocking you to connect that.