欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php根据isbn书号查询amazon网站上的图书信息的示例

程序员文章站 2022-07-19 12:19:35
插件说明:插件根据提供的10位isbn书号,在amazon网站上查找该图书的详细信息。如果找到结果,则返回一个两元素的数组,其中第一个元素是书的标题,而第二个元素是该书封面...

插件说明:
插件根据提供的10位isbn书号,在amazon网站上查找该图书的详细信息。
如果找到结果,则返回一个两元素的数组,其中第一个元素是书的标题,而第二个元素是该书封面缩写图的url地址。
它需要以下参数:$isbn 10位isbn书号

复制代码 代码如下:

$isbn   = '007149216x';
$result = piphp_getbookfromisbn($isbn);
if (!$result) echo "could not find title for isbn '$isbn'.";
else echo "<img src='$result[1]' align='left'><b>$result[0]";

function piphp_getbookfromisbn($isbn)
{
   // plug-in 93: get book from isbn
   //
   // this plug-in looks up an isbn-10 at amazon.com and then
   // returns the matching book title and a thumbnail image
   // of the front cover. it requires this argument:
   //
   //    $isbn: the isbn to look up
   //
   // updated from the function in the book to take into
   // account changes to the amazon html.

   $find = '<meta name="description" content="amazon:';
   $url  = "http://www.amazon.com/gp/aw/d.html?a=$isbn";
   $img  = 'http://ecx.images-amazon.com/images/i';

   $page = @file_get_contents($url);
   if (!strlen($page)) return array(false);

   $ptr1 = strpos($page, $find) + strlen($find);
   if (!$ptr1) return array(false);

   $ptr2  = strpos($page, '" />', $ptr1);
   $title = substr($page, $ptr1, $ptr2 - $ptr1);

   $find = $img;
   $ptr1  = strpos($page, $find) + strlen($find);
   $ptr2  = strpos($page, '"', $ptr1);
   $image = substr($page, $ptr1, $ptr2 - $ptr1);

   return array($title, $img . $image);
}