cURL php request with ip and port - php

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

Related

php curl_exec() Connection refused when retrieving a remote image

I want to retrieve a remote hosted image with php. The image exists, I can access to it with my browser. However the result of the curl_exec() is empty and the curl_error() says:
Failed to connect to img107.xooimage.com port 80: Connection refused
Here is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$image = curl_exec($ch);
if( empty($image) ){
echo("Impossible to retrieve the file !<br>
error: ".curl_error($ch)."<br>");
}
If I can open the image with my browser, then why the connection is refused when I use curl?
Remark: it works for images from my own domain, but not with external images like the one in the example.
EDIT: It actually seems not to be a php problem, since I couldn't even perform a curl or a ping from the host server of my website:
curl http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png > image.png
Connection Refused
ping http://img107.xooimage.com
ping: unknown host http://img107.xooimage.com
ping http://www.google.com
ping: unknown host http://www.google.com
Perhaps my hosting provider applied some limitations/firewalls.
You need to close the curl before reading it
$ch = curl_init('http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
$image = curl_exec($ch);
if (curl_errno($ch)) {
print_r(curl_error($ch));
die;
}
curl_close($ch);
return $image;
After testing, the following does save the given image into the file image.png along the script, and is readable. Is it what was missing?
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$image = curl_exec($ch);
var_dump($image); // Binary content
$save = fopen('image.png', 'w');
fwrite($save, $image);
fclose($save);
curl_close($ch);
This is probably not a happy conclusion, at most it's a workaround, but here is what I finally did.
I exported all the images URL from my website database. I created a bash script based on this command:
curl http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png > image.png
To save an image from the internet by using a curl.
I ran the script from my personal computer which didn't have the same limitation as my website host.
I could retrieve all the pictures.
If you're not managing the rights/limitations on your host, you probably can't do much more.

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.

Failed to connect to : Connection refused - cURL 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.

Curl error code 7 could not connect the host on port 80

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);
*/

PHP Curl+connection refused

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.

Categories