1

It's gives 404 Not Found While downloading file from url. i want to downloads file from url links.

i am tried with curl in PHP.


$output_filename = "text.jpg";

$imageUrl = "https://m.media-amazon.com/images/M/MV5BZjZhYTkyMjgtNjFkZi00NDAyLTk5ZDgtMDYwZTBkYTI1ODllXkEyXkFqcGdeQXVyNTc5OTMwOTQ@._V1_.jpg";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageUrl);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);

$result = curl_exec($ch);
curl_close($ch);

print_r($result); // prints the contents of the collected file before writing..


// the following lines write the contents to a file in the same directory (provided permissions etc)
$fp = fopen($output_filename, 'w');
fwrite($fp, $result);
fclose($fp);

When i runs script it returns me 404 not found warning for file not found. i also try to found solution from internet but not getting exact result. i think it gives warning due to downloading file from regular expression url link.

file is exist while paste image url in browser as show below image.

enter image description here

Siddharth Rathod
  • 634
  • 1
  • 7
  • 21

1 Answers1

1
https://m.media-amazon.com/images/M/MV5BZjZhYTkyMjgtNjFkZi00NDAyLTk5ZDgtMDYwZTBkYTI1ODllXkEyXkFqcGdeQXVyNTc5OTMwOTQ@._V1_.jpg 

will show a http 404 error unless you attempt to use transfer compression (like gzip or deflate)

that's probably a bug in their image cache server, and someone should send a bugreport to the media-amazon.com sysadmin. in any case, by default curl does not attempt to use transfer compression, you have to explicitly enable it, and to enable it just set CURLOPT_ENCODING to emptystring, eg

curl_setopt($ch, CURLOPT_ENCODING, '');

that tells curl to attempt transfer compression, and you should get the image (but again, this is a bug in the server, someone should let them know so they can fix it!)

hanshenrik
  • 19,904
  • 4
  • 43
  • 89