Reputation: 33
Edit: I contact the support at scrapestack and confirmed that their api doesn't support image files.
I am trying to download a remote image using CURL with php. Below is my code. But whenever I try to open the downloaded image, I always get:
Cannot read this file. This is not a valid bitmap file, or its format is not currently supported.
Anyone know what is wrong with my code? Thank you.
$image ="http://api.scrapestack.com/scrape?access_key=TOKEN-HERE&url=https://i.imgur.com/Cbiu8Ef.png";
$imageName = pathinfo( $image, PATHINFO_BASENAME );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $image );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTP_CONTENT_DECODING, false);
$source = curl_exec( $ch );
$info = curl_getinfo($ch);
curl_close( $ch );
file_put_contents( $imageName, $source );
I am not able to open the file, when I tried to open it with sublime, it is stuck at Loading Image. When I open it with notepad, I got the following that looks like PNG image, but it is not a valid image. File starts with �PNG
IHDR � q�I� IDATx�k�]�u�o��(��_�M��m�8:���_r�G
You can see the file here: https://gofile.io/?c=cfsYf2
Looks like the problem is making the curl request through Scrapestack, because if I point the curl to image url directly, the image is downloaded correctly, like below:
$image ="https://i.imgur.com/Cbiu8Ef.png";
Upvotes: 1
Views: 2712
Reputation: 47
Edit: I played around with scrapestack a bit more today, it doesn't seem to support image scraping. It is best if you can reach out to their customer support and find out.
@Towsif is right, you are trying to get the page, not the actual image. I put something together really quick, try and see if this works for you.
$queryString = http_build_query([
'access_key' => 'replace this with your own token',
'url' => 'https://i.imgur.com/Cbiu8Ef.png',
]);
$ch = curl_init(sprintf('%s?%s', 'http://api.scrapestack.com/scrape', $queryString));
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$image_source = curl_exec( $ch );
curl_close( $ch );
file_put_contents( 'Cbiu8Ef.png' , $image_source );
Upvotes: 1
Reputation: 15827
It looks like the response you get is a corrupt PNG image.
If you are using PHP with version prior of 5.1.3 you need to specify an additional option for binary data transfers, like images:
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
If the above options doesn't solve the issue you may try setting
curl_setopt($ch, CURLOPT_HTTP_CONTENT_DECODING, false);
in case the response has the Content-Type
header set wrong letting curl do unwanted decoding on the raw output.
Upvotes: 0
Reputation: 320
Your problem is with this url.
$image ="http://api.scrapestack.com/scrape?access_key=TOKEN-HERE&url=https://i.sstatic.net/f4vJV.jpg";
If you go to this url
https://i.sstatic.net/f4vJV.jpg
You will see the image page but NOT the image path. pathinfo()
function does not work here and raises error.
If you right click on that image and Open image in new tab you will then see the image path, in this case that is
https://i.sstatic.net/pz1p0.png
So you may try with this url
$image ="http://api.scrapestack.com/scrape?access_key=TOKEN-HERE&url=https://i.sstatic.net/pz1p0.png";
Upvotes: 0