Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从GoogleBooksAPI获取封面图像_Php_Image_File Get Contents_Cover_Google Books - Fatal编程技术网

PHP从GoogleBooksAPI获取封面图像

PHP从GoogleBooksAPI获取封面图像,php,image,file-get-contents,cover,google-books,Php,Image,File Get Contents,Cover,Google Books,我正试图从谷歌图书服务获取缩略图中的图像。通过file_get_contents读取php中的地址可以获取该书的所有网页,而我只想获取封面图像。我知道可以通过HTMLIMG元素的src标记完成,但我需要图像服务器端。有办法吗?谢谢 下面是我使用的代码: $context = [ 'http' => [ 'method'=>"GET", 'header' => "Accept:image/png\r\nAccept-Language:it-

我正试图从谷歌图书服务获取缩略图中的图像。通过file_get_contents读取php中的地址可以获取该书的所有网页,而我只想获取封面图像。我知道可以通过HTMLIMG元素的src标记完成,但我需要图像服务器端。有办法吗?谢谢

下面是我使用的代码:

$context = [
    'http' => [
        'method'=>"GET",
        'header' => "Accept:image/png\r\nAccept-Language:it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3\r\nCache-Control:max-age=0\r\nConnection:  keep-alive\r\nUser-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:33.0) Gecko/20100101 Firefox/33.0\r\n"
     ]
];
$context = stream_context_create($context);
$result = @file_get_contents($bookThumbnail, false, $context);

你是说刮掉它?如果你那样做,可能会有法律问题

但是看看他们的网站(而且只有一个结果),图像位于html的这一部分:

<div class="bookcover"><a href="http://books.google.com/books?id=btIQAAAAYAAJ&amp;printsec=frontcover&amp;source=gbs_ge_summary_r&amp;cad=0" ><img src="http://bks0.books.google.com/books?id=btIQAAAAYAAJ&printsec=frontcover&img=1&zoom=1&edge=curl&imgtk=AFLRE70zzwoUCkgdipXt2aghe0i9TUKTDTH4s71IXTEPNlUNi9dN-aiJDCtxor4tL3yqq8KrHzEiHTyysqSFu2vH2QNm3JNCjyjm3f-B_N2VKYZqrcaqHfUXDdOea2f3FBR3nFIUxtk7" alt="Front Cover" title="Front Cover" width=128 border=1 id=summary-frontcover ></a></div>


使用类似的东西来提取它。应该很直截了当。不过,您可能不想只对图像使用file_get_contents(),可以使用curl或其他方法。

听起来您需要这样的方法

$dat = file_get_contents('https://www.googleapis.com/books/v1/volumes/H1w9AwAAQBAJ');

$arr = json_decode($dat,1);

$info = $arr['volumeInfo'];

$imagedata = file_get_contents($info['imageLinks']['thumbnail']);

file_put_contents('/images/thumb.jpg', $imagedata);

// just to see it 
$img = imagecreatefromstring($imagedata);
header('Content-Type: image/jpg');
imagejpeg($img);
imagedestroy($img);

发布一些您尝试过的代码??这就是我尝试过的,但是$imagedate包含了本书的整个网页,而不仅仅是图像。您甚至懒得直接在浏览器中加载吗?它返回json编码的数据,而不是不谈论json的“网页”。问题在于['imageLinks']['thumbnail']:如果在浏览器中打开,则可以获得封面,但不能通过文件获取内容