从动态 PHP 网址保存图像


Saving images from dynamic PHP url

我正在尝试从动态网址保存jpg文件,看起来像这样,

http://bks7.books.google.se/books?id=TL3JGsUOArkC&printsec=frontcover&img=1&zoom=1&&source=gbs_api

file_get_contents无法正确获取内容,这是我的代码,

<?php
$image_url = "http://bks7.books.google.se/books?id=TL3JGsUOArkC&printsec=frontcover&img=1&zoom=1&&source=gbs_api";
$img =  file_get_contents($image_url);
$folder = 'C:/xampp/htdocs/test/test.jpg';
file_put_contents($folder, file_get_contents($img));
?>

欣赏任何想法或替代方案作为"简单"的方法。

一个问题是你有 2 个file_get_contents调用。第一个电话:

$img =  file_get_contents($image_url);

返回从请求到 URL 的响应,并将其存储在 $img 变量中。第二个电话:

file_put_contents($folder, file_get_contents($img));

没有任何意义。相反,只需这样做:

file_put_contents($folder, $img );