Friday, January 26, 2018

parsing - Error when copying img with a cURL script (php)



I made a nice simple userscript:
When I browse the web, I can "bookmark" any image in 1 click
My userscript





  1. Grab the img src

  2. Grab the url of the webpage

  3. Copy the .jpg .png .gif to my server



Everything works perfectly, BUT in some cases, the script cannot copy the file...
Actually the file is created but do not contains the img data, it only contains the content of an error webpage:








403 Forbidden

Forbidden


You don't have permission to access /data/x/xxx_xxx_x.jpg on this server.


Additionally, a 403 Forbidden
error was encountered while trying to use an ErrorDocument to handle the request.




Apache Server at xxxxxxxx.net Port 80





The "copy" code (php):



$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $urlimg);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
set_time_limit(300); # 5 minutes for PHP

curl_setopt($ch, CURLOPT_TIMEOUT, 300); # and also for CURL
$path = $dirpix.'/'.$aa.'/'.$mm;
if ( ! is_dir($path)) {
mkdir($path);
}
$outfile = fopen($path.'/'.$id.'.'.$ext, 'wb');
curl_setopt($ch, CURLOPT_FILE, $outfile);
curl_exec($ch);
fclose($outfile);
curl_close($ch);



Maybe the website blocks that kind of "copy" script?
Thanks!


Answer



2 things I can think of here are,




  1. Set a user agent to your curl request. Because from what you say, you are able to view the image but curl is getting 403 error, it could very well be userAgent filtering on server side.


  2. Add referer to your curl request. You can send the referer information from your userscript to the php script. You'd have to post or get window.location.href's value.




No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...