I'm using the following code from a SO thread, but the file that is outputted is a corrupt PNG file. I can't view it in any image editor. The image I'm trying to grab can be found at this direct link.
<?php
function getimg($url) {
$headers[] = 'Accept: image/gif, image/png, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
$imgurl = 'http://download.videos.framebase.io/5a94a14f-aca6-4fb0-abd3-f880966358ab/6bf94ebb-4712-4895-9c85-fb8d4a19d50c/8f8b778f-6335-4351-82e7-6f50fd7fdd06/1351620000000-000020/thumbs/00001.png';
if(file_exists($imgsave)){continue;}
$image = getimg($imgurl);
file_put_contents($imgsave,$image);
?>
What am I missing? Is it the headers I'm using? Is it the file? I tried saving the file just manually within my browser, and it loaded fine with my image editors.
any ideas would be much appreciated, thanks!
-- 24x7
edit:
I know I marked this as solved, but on second thought, I still can't view the PNG's the server is saving. Here's what had been suggested so far:
<?php
function grab_image($url){
echo $url;
$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);
$saveto = 'recent-image.png';
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
grab_image("http://download.videos.framebase.io/2acd363b-ab1f-4305-8f1c-c1eaa6ebcc2a/abb6a3b7-414c-461b-a84c-56032060575d/e3682943-6959-456d-89db-8cd96647ddd4/1351620000000-000020/thumbs/00001.png");
?>
I've ran the script over and over again to no luck, once again, any help would be appreciated. Here's a sample of the PNG it outputs. thanks in advanced.
you can use normal function like
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);
$saveto = "D:/test.png";
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
grab_image('http://download.videos.framebase.io/5a94a14f-aca6-4fb0-abd3-f880966358ab/6bf94ebb-4712-4895-9c85-fb8d4a19d50c/8f8b778f-6335-4351-82e7-6f50fd7fdd06/1351620000000-000020/thumbs/00001.png','');
and make sure that in php.ini allow_url_fopen is enable..
Related
I am trying to get an image by url, and save it my web server, doesn't matter where, it could be next to this php file or maybe in an /images/ folder.
This is what I have so far, and it's not working.
$url = 'https://path-to-my-image/image.jpg';
$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);
$base = base64_encode($raw);
$image = imagecreatefromstring($raw);
file_put_contents( '/images/', $base );
Try it this way:
$img = file_get_contents('http://www.dan-dare.org/FreeFun/Images/CartoonsMoviesTV/BugsLifeWallpaper1024.jpg');
file_put_contents('img.jpg', $img);
from PHP - Copy image to my server direct from URL
try this
1
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
or try this
2
function getimg($url) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'php';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $user_agent); //check here
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
$imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg';
$imagename= basename($imgurl);
if(file_exists('./tmp/'.$imagename)){continue;}
$image = getimg($imgurl);
file_put_contents('tmp/'.$imagename,$image);
if not give a try to this one.
3
$url="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
$contents=file_get_contents($url);
$save_path="/path/to/the/dir/and/image.jpg";
file_put_contents($save_path,$contents);
Interestingly, I have not been able to find a working example of this. Using php, I'm trying to scrape/re display all the images of a given url, onto another website. I know how to do this with text, but images, I'm not sure. Anyone know of a good working example? I get how to grab all contents, but not specifically just the images. ex this does the whole page:
<?php
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL,
"https://en.wikipedia.org/wiki/Wikipedia:Picture_of_the_day");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
echo $result;
?>
Thanks much. -Wilson
*ideally actually this would just grab the first image, such as in the example above. But I won't get ahead of myself, just trying to get this function down.
You may use a file to save the result.
$fp = fopen($filename, 'a+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($dltotal, $dlnow, $ultotal, $ulnow) {
});
curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 1);
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 8);
curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$end_size = $begin_size + curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
Log::info('end_size='.$end_size);
curl_close($ch);
fclose($fp);
You can add /use PHP DOMXPath function to parsing your scrape result.
add this following script right after your code
$dom = new DOMDocument();
#$dom->loadHTML($result);
$xpath = new DOMXPath($dom);
//get all images
$images = $xpath->query ('//img/#src');
$img = array();
foreach ( $images as $image) {
$img[] = $image->nodeValue;
}
print_r($img);
** Edited Part
Try to change your CURL code with this one
$url = 'https://en.wikipedia.org/wiki/Wikipedia:Picture_of_the_day';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10');
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //untuk https
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);//untuk https
//curl_setopt($curl, CURLOPT_ENCODING , 'gzip');
$html = curl_exec($curl);
if(curl_error($curl)){
echo 'Curl error: ' . curl_error($curl);
$result = ''; //return empty if error
}
else {
$result = $html;
}
I have a script in php how download images from a partner website.
The script looks like
function getimg($url) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'php';
$process = curl_init($url);
curl_setopt($process, CURLOPT_USERPWD, "username:password");
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
The problem is because partner was put a password on his website. the website url is www.importatorarticolecopii.ro/feeds/general_feed2.php
I put the user and password in curl but not work...
Need some help
Your code is fine, the problem is from your given url
Increase your timeout as the url given above is too slow and try to don't exceed the maximum execution time of your server.
curl_setopt($process, CURLOPT_TIMEOUT, 60);
and use a user agent for example :
$user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/2php0050915 Firefox/1.0.7)';
and in future try to catch your errors to check what's your issue
if(curl_error($process))
{
echo 'error:' . curl_error($process);
}
The size of the save images 0 kb
Working code, except facebook
function imagedownload($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = parse_url($url);
curl_setopt($ch, CURLOPT_REFERER, $result['scheme'].'://'.$result['host']);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0');
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
$url="http://scontent-frt3-1.xx.fbcdn.net/v/t1.0-0/cp0/e15/q65/p320x320/13700038_850487491753797_6227258625184891703_n.jpg?oh=793ecde8db1a8e65789534907d08b25e&oe=57F1DDFF";
$konum="images/"
$yolla=imagedownload($url,$konum);
The size of the save images 0 kb
Working code, except facebook
It works if you remove the options to POST and do a regular GET request:
<?php
function imagedownload($url, $saveto)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = parse_url($url);
curl_setopt($ch, CURLOPT_REFERER, $result['scheme'] . '://' . $result['host']);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0');
$raw = curl_exec($ch);
curl_close($ch);
if (file_exists($saveto)) {
unlink($saveto);
}
$fp = fopen($saveto, 'x');
fwrite($fp, $raw);
fclose($fp);
}
$url = "http://scontent-frt3-1.xx.fbcdn.net/v/t1.0-0/cp0/e15/q65/p320x320/13700038_850487491753797_6227258625184891703_n.jpg?oh=793ecde8db1a8e65789534907d08b25e&oe=57F1DDFF";
$konum = "test.jpg";
$yolla = imagedownload($url, $konum);
I had try in my localhost this code and it working well.
<?php
ini_set('display_errors', 1);
function imagedownload($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);
}
$url="http://scontent-frt3-1.xx.fbcdn.net/v/t1.0-0/cp0/e15/q65/p320x320/13700038_850487491753797_6227258625184891703_n.jpg?oh=793ecde8db1a8e65789534907d08b25e&oe=57F1DDFF";
$konum="/var/www/html/jsPDF-master/examples/test.jpg";
$yolla=imagedownload($url,$konum);
?>
And result
And one note: Please make sure the directory for save image must have write permission.
Example: chmod -R 777 /var/www/html/jsPDF-master/examples
Or ensure that in php.ini allow_url_fopen is enable
i am trying to download the source file bellow using curl, but it is only creating destination file with 0 bytes, not downloading the original file and write the resource data. How can i solve this problem. I tried a example from this link: http://www.w3bees.com/2013/09/download-file-from-remote-server-with.html . But it also give the same result.
Anybody can help? my code is:
if(isset($_POST[$data_file_name_url])){
$source = 'http://mr-greens-class.wikispaces.com/file/messages/Calender.ics';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
$data = curl_exec ($ch);
$error = curl_error($ch);
curl_close ($ch);
$destination = $dir.'/test.ics';
$file = fopen($destination, "w+");
fputs($file, $data);
//file_put_contents($destination, $data);
fclose($file);
}
You should remove SSLVERSION if not use (your URL start with http, so I assume it's not SSL)
$path = 'myfile.ics';
$final_url = 'http://mr-greens-class.wikispaces.com/file/messages/Calender.ics';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $final_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_HEADER, 0);
// sometimes needed to prevent problem by bot filtering
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.103 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
if($data == false) {
$errno = curl_errno($ch);
$error_message = curl_error($ch);
//error_log( "cURL error ({$errno}):\n<br/> {$error_message}");
}
curl_close($ch);
file_put_contents($path, $data);