Failed to connect to : Connection refused - cURL PHP - php

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.

Related

firebaseio.com refusing connections after many requests

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.

cURL php request with ip and port

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

localhost xampp curl port 80 error

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

laravel curl not working on azure

I am connecting my two apps (laravel with python) using curl command. It is working absolutely fine on my local server without changing a single line but when it gives me following error when on azure server
POST https://myweb.com/searchJobs 500 ()
my code is
$ch = curl_init();
//URL to send the request to
curl_setopt($ch, CURLOPT_URL, 'http://python-scrapper-python001.azurewebsites.net/myfunction');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//We are doing a post request
curl_setopt($ch, CURLOPT_POST, 1);
//adding the post data to request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
//Return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
is there any security protocol that I need to update on azure or what can be possible issue?
Actually these lines were working fine on localhost but not on azure
$arr = "";
$arr['hello'] = '7';
so I changed them as
$arr = [];
$arr['hello'] = '7';
Though i don't no why azure didn't run it. If php convert it to an array on localhost than it must run on azure as well
Since you are using the default domain provided by azure appservice. Instead of just using the HTTP protocol try to use the HTTPS since this is already free on the default domain provided. in this case try this changes.
$ch = curl_init();
//URL to send the request to
curl_setopt($ch, CURLOPT_URL, 'https://python-scrapper-python001.azurewebsites.net/myfunction');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//We are doing a post request
curl_setopt($ch, CURLOPT_POST, 1);
//adding the post data to request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
//Return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);

php 5.3 cUrl / linux cmd cURL https

I have a problem setting up a https connection using cURL in php.
here is my php code to set up the connection.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endlocation);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CAINFO, $certificate);
curl_setopt($ch, CURLOPT_PORT, $portNumber);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
the folowing var are:
$endlocation = "https://lms.lifeline.nl/mailwebservice/inline/"
$data = json encoded data
$certificate = true path to .crt file
$portNumber = 443
when i execute the cURL my page breaks and i get a page error of the compound was reinitialized. as it happens to be i get back a false from the curl_exec.
how ever if i use this file not to directly set up a connection true php but by using linux command line on the same server:
$url = "https://lms.lifeline.nl/mailwebservice/inline";
exec("curl $url", $output, $status);
print_r($output); die;
then i do get a response back. is there somthing wrong whit my cURL exec in php format?. or is there somthing else that i need to look in to?

Categories