Download Image URL - PHP - 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);

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.

Download Instagram Image using PHP Curl or file_get_content

I have this image that I'm trying to download
https://www.instagram.com/p/CHZGLObDQV2/media/?size=l
The issue with all Instagram images is that when you clicked on that URL it redirects you to the original URL which is:
https://instagram.fcrk2-1.fna.fbcdn.net/v/t51.2885-15/e35/s1080x1080/124374363_458291788482210_6691254472190084475_n.jpg?_nc_ht=instagram.fcrk2-1.fna.fbcdn.net&_nc_cat=106&_nc_ohc=VuSNajTRd78AX_GZlsF&_nc_tp=15&oh=cc224ca1041c97c06ec124a98e0241ca&oe=5FD536C4
How do I download that original URL of Instagram using curl or file_get_contents?
I have this code:
$url = "https://www.instagram.com/p/CHZGLObDQV2/media/?size=l";
save_image($url,"/test/images/test.jpg");
function save_image($img,$fullpath){
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
}
This one does not save properly because it does not get the original URL.
Your help is greatly appreciated! Thank you!

Slack. How to download image in url_private property using PHP?

I am trying to download a png image from Slack and save it into a file but the only thing that gets saved into the file is HTML code for the login page of Slack.
This is what I have tried.
$url = 'https://files.slack.com/files-pri/XXXXXX-XXXXXXX/download/2016-07-11.png';
$ch = curl_init($url);
$fp = fopen('/assets/tmp/1.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer xoxp-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXXXXXXX'
));
curl_exec($ch);
curl_close($ch);
fclose($fp);
If I access the link in browser while logged into Slack, the image downloads just fine, but not through the PHP code. Any help would be appreciated.
I needed an extra permission scope to read files. I added files:read permission scope to my application from Slack Developer Console and the code works as expected.

Get file in PHP which is generated when visiting URL

In PHP, how do I get a file that is generated when you visit a URL. To explain this, if you visit this Google Drive URL:
https://docs.google.com/document/d/1ZUqkdVQZqpHhE25m-5wxhN1uzK7EvoZ81bmrwiwk3CY/export?format=doc
Download will start. How do I grab that download file in PHP?
Though in theory file_get_contents should have worked for this situation it did not for me. I'm behind proxy.
To get more control I had to use curl
I left proxy configuration as a part of code but commented it out.
<?php
$url = "https://docs.google.com/document/d/1ZUqkdVQZqpHhE25m-5wxhN1uzK7EvoZ81bmrwiwk3CY/export?format=doc";
$fileToSave = dirname(__FILE__) . '/localfile.doc';
// $proxy = "127.0.0.1:8080";
set_time_limit(0);
//This is the file where we save the information
$fp = fopen ($fileToSave, 'w+');
//Here is the file we are downloading.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Proxy stuff if you need it
// curl_setopt($ch, CURLOPT_PROXY, $proxy);
// ssl config here
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// get curl response
if(curl_exec($ch) === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
echo 'Done';
}
curl_close($ch);
fclose($fp);
$data=file_get_contents("https://docs.google.com/document/d/1ZUqkdVQZqpHhE25m-5wxhN1uzK7EvoZ81bmrwiwk3CY/export?format=doc") ;
Use this method to get the file content, I have used this method on a url that returned a CSV file upon hitting.

Incorrect MD5 checksum after downloading with CURL in PHP - file_get_contents works fine

I have a script where I have to download some files and to make sure that everything worked fine I'm comparing MD5 checksums.
I have found that the checksums are not correct when downloading with CURL. The script below demonstrates this. It downloads the Google logo and compares checksums.
$url = 'http://www.google.com/intl/en_ALL/images/logo.gif';
echo md5_file($url)."\n";
$path = 'f1';
file_put_contents($path, file_get_contents($url));
echo md5_file($path)."\n";
$path = 'f2';
$out = fopen($path, 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
echo md5_file($path)."\n";
the output is:
e80d1c59a673f560785784fb1ac10959
e80d1c59a673f560785784fb1ac10959
d83892759d58a1281e3f3bc7503159b5
The first two are correct (they match the MD5 checksum when I download the logo using firefox) and the result produced by curl is not OK.
any ideas how to fix that?
thanks for your help
UPDATE:
interestingly the code below works just fine and produces the correct output. The problem really only seems to exist when saving to a file. Unfortunately I have to save directly to a file since the files I'm downloading can get rather large.
$path = 'f3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
file_put_contents($path, curl_exec ($ch));
echo md5_file($path)."\n";
curl_close ($ch);
You're missing an fclose($out), which could account for md5_file seeing an incomplete file.
Try to add
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
To your curl options

Categories