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
Related
Unable to connect with the tally server using the following script.
Please help.
Tally is in different system i'm using another laptop.
$server = 'http://192.168.1.20:9000';
$headers = array( "Content-type: text/xml" ,"Content-length: ".strlen($requestXML) ,"Connection: close" );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
if(curl_errno($ch)){
print curl_error($ch);
echo " something went wrong..... try later";
}else{
echo " request accepted";
print $data;
curl_close($ch);
}
}
Try Opening the same URL in browser
If you cant see below response Your IP is wrong or you are not in local network
<RESPONSE>TallyPrime Server is Running</RESPONSE>
If you want to connect outside local network you should use public IP and configure firewall to accept requests on port 9000
Sample Response
I use PHP CURL for making changes to the Firebase database. After many curl requests, suddenly, Failed to connect to something.firebaseio.com port 443: Connection refused error occours. After few hours, it starts working again.
Following is the curl script in php
$url = 'https://something.firebaseio.com/iochatting/roomchats/data.json?auth=private_key';
$json = json_encode($msg);
$headers = array();
$headers[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20500);
$response = curl_exec($ch);
if ($response === FALSE) {
}
curl_close($ch);
This code works fine most of the time but suddenly all connection requests are rejected.
Also, i have tried https://github.com/kreait/firebase-php , this too stops suddenly.
I want the curl to work not the php-sdk. Is there anyway ? Is google refusing server request ?
When the connections are rejected when sending request normally, if i use proxy in of port 443. It works again for few times.
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 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.
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.