Friday, 26 January 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...