如何制作谷歌api php客户端

时间:2016-11-18 11:33:37

标签: php api google-api google-api-php-client

如何通过传递书籍的isbn编号来编写此代码以获取书籍的详细信息

$client = new \Google_Client();
$client->setApplicationName("BestBook");
$client->setAuthConfigFile('temp.json');
$client->addScope(\Google_Service_Drive::DRIVE);
$client->setDeveloperKey("AIzaSyD6OrKhhJiseBimFVJZ_7OV5tyPdg4LxZiY");

$service = new \Google_Service_Books($client);
$results = $service->volumes->listVolumes('Henry David Thoreau');

foreach ($results as $item) {
  echo $item['volumeInfo']['imageLinks']['smallThumbnail'], "<br /> \n";
}

当我通过一个isbn

时,任何想法为什么它显示10输出

1 个答案:

答案 0 :(得分:1)

请考虑Google图书为您提供ISBN_13和ISBN_10这一事实。考虑到这一点,您需要执行以下操作:

$client = new Google_Client();
$client->setApplicationName("BestBook");
$client->setDeveloperKey("asdfkjeriuIEWURkjfaskd");

$optParams = array(
    'filter' => 'ebooks'//,
    //'langRestrict' => 'es',
    //'orderBy' => 'relevance'
 );

$isbnNumber = 9781452052656;
$results = $service->volumes->listVolumes($isbnNumber, $optParams);

// HANDLE RESULT
foreach ($results as $item) {     

    if ($item['volumeInfo']['industryIdentifiers'][0]['identifier'] == $isbnNumber ) { // For ISBN_13 (for ISBN_10 use $item['volumeInfo']['industryIdentifiers'][1]['identifier'] )

    echo " - ".utf8_decode($item['volumeInfo']['title']), "<br />";

    } 
}

顺便说一句,既然您没有访问用户数据而只是访问应用程序数据,那么您不需要使用行$client->addScope(\Google_Service_Drive::DRIVE);$client->setAuthConfigFile('temp.json');

哦!我差点忘了!请不要在此发布您的API密钥,因为这是敏感信息!

希望这有帮助!