php curl_exec() Connection refused when retrieving a remote image - php

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.

Related

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

PHP file_get_contents & curl can get image files from some sites but not others. Why?

file_get_contents in PHP/Apache2 was getting user pictures from Facebook. It was working fine until recently. Now, it always times out after a minute, with this error in my Apache2 error.log:
PHP Warning: file_get_contents(https://graph.facebook.com/999999999/picture?width=200): failed to open stream: Connection timed out
Here is the code (I recently added $context to see if it made it work. It did not):
$context = stream_context_create(array('https' => array('header'=>'Connection: close\r\n')));
$fbprofileimage = file_get_contents('https://graph.facebook.com/'.$id.'/picture?width=100',false,$context);
I tried curl and it doesn't work:
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://graph.facebook.com/'.$id.'/picture?width=100');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'StockBet');
$fbprofileimage = curl_exec($curl_handle);
curl_close($curl_handle);
I found out that file_get_contents & curl will work with some sites, but not others.
The following works:
$context = stream_context_create(array('https' => array('header'=>'Connection: close\r\n')));
$fbprofileimage = file_get_contents('https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2017/08/03/104629909-GettyImages-630953738-bitcoin.240x160.jpg?v=1501760634',false,$context);
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2017/08/03/104629909-GettyImages-630953738-bitcoin.240x160.jpg?v=1501760634');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'StockBet');
$fbprofileimage = curl_exec($curl_handle);
curl_close($curl_handle);
The above code can get the following image files as well:
https://b.thumbs.redditmedia.com/SSGv_d2P6ymZVIq8Bm5IVqpcz6WZioCfbRh5rxMPcTc.jpg (Reddit)
https://try.alexa.com/wp-content/uploads/2017/01/alexa-logo.png
https://ir.ebaystatic.com/rs/v/fxxj3ttftm5ltcqnto1o4baovyl.png
(eBay)
https://images-na.ssl-images-amazon.com/images/G/01/Gateway/Unrec/T2/Amazon_GW_DesktopShoveler_5_200x200.CB503347722.png
https://bitcointalk.org/useravatars/avatar_7110.jpg
https://try.alexa.com/wp-content/uploads/2017/01/alexa-logo.png
The above code cannot get the following image files:
https://www.google.ca/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png
https://s.yimg.com/uu/api/res/1.2/7rUl71NcpOwqZeJexHuZhA--/Zmk9c3RyaW07aD0xOTM7cHlvZmY9MDtxPTk1O3c9MjIwO3NtPTE7YXBwaWQ9eXRhY2h5b24-/http://media.zenfs.com/en-US/homerun/aol_com_127/ce17fc9999cd894bf3a3dac1416b3230 (Yahoo)
https://c.disquscdn.com/next/a0cd712/marketing/assets/img/brand/disqus-logo-blue.svg (Disqus)
https://pbs.twimg.com/profile_images/464794058229964800/uwlkErTI_bigger.png (Twitter)
https://scontent-yyz1-1.xx.fbcdn.net/v/t45.1600-4/c0.31.284.149/p284x149/21337242_6083114387812_5783480223513182208_n.png?oh=3c78c007377ee96b844e91843dfdfddb&oe=5A183896
(Facebook)
https://www.ibm.com/cloud-computing/images/ca-en-watson-2-09112017-600x260-p1v1.jpg
Does anyone know why I can get image files from some sites but not others?
For my purposes, which is to get user pictures from Facebook, I got it to work by using the following code:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // need confirmed
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // need confirmed. I think this is key, as Facebook redirects to another URL and we need to follow
//curl_setopt($ch, CURLOPT_ENCODING,""); // not needed confirmed
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // need confirmed
curl_setopt($ch, CURLOPT_TIMEOUT,1); // need confirmed
curl_setopt($ch, CURLOPT_FAILONERROR,true); // not needed confirmed
//curl_setopt($ch, CURLOPT_VERBOSE, true); // not needed confirmed
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // not needed confirmed
curl_setopt($ch, CURLOPT_HEADER, true); // need confirmed
$fbprofileimage = curl_exec($ch);
if (curl_errno($ch)){
echo 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
//$info = rawurldecode(var_export(curl_getinfo($ch),true));
// Get the cookies:
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
//$responseHeader= substr($fbprofileimage,0,$skip);
$fbprofileimage= substr($fbprofileimage,$skip); // need confirmed
//echo "HEADER: $responseHeader\n"; // causes error
//echo "\n\nINFO: $info\n\nDATA: $fbprofileimage"; // causes error
}
I think what was key was this:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
because when getting https://graph.facebook.com/999999999/picture?width=200 (where 999999999 is the user's ID), Facebook redirects to another URL.

Download Image URL - PHP

I'm trying to create a script to download images to a local server and I'm having some issues with the images downloading. I have tested the following script on a standard image (http://www.urmob.co.uk/i/l/2860.jpg) and its working fine, however, when trying to download an image from the following location
https://s3-eu-west-1.amazonaws.com/mobile-phones-direct/phone/portraitimage/full/52122ad8-c918-450d-be10-5921c0a870cb.png
It has no file contents and therefore the image doesn't work. I believe it has something to do with the HTTPS? I originally tried the standard file_get_contents but that didn't work, now I'm trying a CURL version and it is still not working. The function I'm using looks like the below:
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
Does anyone know why this isn't working?
Thanks,
Craig
This is very simple:
test.php
<?php
// image source
$image_url = 'https://s3-eu-west-1.amazonaws.com/mobile-phones-direct/phone/portraitimage/full/52122ad8-c918-450d-be10-5921c0a870cb.png';
// download image
file_put_contents('downloaded_image.jpg', file_get_contents($image_url));
// display
echo '<img src="downloaded_image.jpg" >';
It works for me:
If you want to keep using the curl version and fix that ssl error, add this to your code:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
you are getting image from ssl url trying to remove https and use http
or try by adding :-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

Upload a URL as a file using PHP curl

I have images that are hosted on a CDN, and I want to upload that image to some other system via their API. Normally I would just use curl and put an # character in front of the file to upload. Since this is a URL, that won't work since it expects the file to be local. I don't want to copy it local first since the files could be videos and that could be time consuming.
It seems there should be a way to do this with PHP curl. I'm open to using sockets instead, but I'm not sure how to simulate how curl does the submission.
<?php
$save_file_url = file_get_contents($your_files_CDN_file_URL);
$fp = fopen('test','w');
fwrite($fp,$save_file_url);
$save_file = realpath('test') ;
$url = "http://URL_TO_SEND_FILE/get_file.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"file_is_this"=> "#".$save_file,
"app"=>'test'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
And you will recieve file at get_file.php.

TTY and/or ARF Response from Experian's API

I'm trying to use the PHP Curl library to connect to Experian's API.
When I post a HTTPS request to Experian, I get an HTTP 200 OK response, but nothing more. I am expecting a TTY or ARF response. Do anyone have insight in what I'm doing wrong?
Here's a snippet of my code below for reference
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://stg1.experian.com/lookupServlet1?lookupServiceName=AccessPoint&lookupServiceVersion=1.0&serviceName=NetConnectDemo&serviceVersion=2.0&responseType=text/plain'); //not the actual site
//For Debugging
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT,60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
//#2-Net Connect client receives ECALS response. This is the Net Connect URL.
$ecals_url = curl_exec($ch);
//#3-Net Connect client validates that the URL ends with “.experian.com”. If the URL is valid, the processing continues; otherwise, processing ends.
$host_name = parse_url( $ecals_url );
$host_name = explode(".", $host_name['host'] );
$host_name = $host_name[1].'.'.$host_name[2];
if( $host_name == "experian.com" )
{
//#4-Net Connect client connects to Experian using URL returned from ECALS.
echo "step 4 - connect to secure connection<br>";
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch,CURLOPT_URL,$ecals_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
curl_setopt($ch,CURLOPT_CERTINFO,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //times out after 10s
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec ($ch);
print_r ($result);
It has been a while since I messed with our Experian Net Connect service but I believe you have to base64 encode the username:password value for their system to take it.

Categories