您如何下载安全图像

时间:2018-07-19 05:19:36

标签: php image download

我正在尝试下载受保护的图像,除了下面的链接之外,我还可以下载其他图像。

https://store.bbcomcdn.com/images/store/skuimage/sku_EVL4900077/image_skuEVL4900077_largeImage_X_450_white.jpg

这是我的代码:

<?php
    function downloadFile ($url, $path) {

  $newfname = $path;
  $file = fopen ($url, "rb");
  if ($file) {
    $newf = fopen ($newfname, "wb");

    if ($newf)
    while(!feof($file)) {
      fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
    }
  }

  if ($file) {
    fclose($file);
  }

  if ($newf) {
    fclose($newf);
  }
 }

downloadFile ("https://store.bbcomcdn.com/images/store/skuimage/sku_EVL4900077/image_skuEVL4900077_largeImage_X_450_white.jpg", "name.jpg");
?>

1 个答案:

答案 0 :(得分:0)

我建议您对文件进行CURL处理。只要确保设置了所有似乎正在检查的标头即可。以下脚本的工作原理很不错:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://store.bbcomcdn.com/images/store/skuimage/sku_EVL4900077/image_skuEVL4900077_largeImage_X_450_white.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER,[
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Content-Type: application/json;charset=utf-8',
    'Expect: 100-continue',
    'Connection: Keep-Alive'
]);
$st = curl_exec($ch);
file_put_contents('name.jpg', $st);

curl_close($ch);