使用PHP打印Google book api信息

时间:2013-11-06 11:59:43

标签: php json

我希望通过isbn编号搜索图书信息并检索信息并显示/存储信息的结果。

例如,我使用了以下url

https://www.googleapis.com/books/v1/volumes?q=isbn:9789381141977

{
 "kind": "books#volumes",
 "totalItems": 1,
 "items": [
  {
   "kind": "books#volume",
   "id": "j5jFmAEACAAJ",
   "etag": "7QZKCyFvQv0",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/j5jFmAEACAAJ",
   "volumeInfo": {
    "title": "Wireless Communication Systems",
    "authors": [
     "Rajeshwar Dass, Sr."
    ],
    "publishedDate": "2012-11-27",
    "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "9381141975"
     },
     {
      "type": "ISBN_13",
      "identifier": "9789381141977"
     }
    ],
    "pageCount": 200,
    "printType": "BOOK",
    "categories": [
     "Science"
    ],
    "contentVersion": "preview-1.0.0",
    "language": "en",
    "previewLink": "http://books.google.co.in/books?id=j5jFmAEACAAJ&dq=isbn:9789381141977&hl=&cd=1&source=gbs_api",
    "infoLink": "http://books.google.co.in/books?id=j5jFmAEACAAJ&dq=isbn:9789381141977&hl=&source=gbs_api",
    "canonicalVolumeLink": "http://books.google.co.in/books/about/Wireless_Communication_Systems.html?hl=&id=j5jFmAEACAAJ"
   },
   "saleInfo": {
    "country": "IN",
    "saleability": "NOT_FOR_SALE",
    "isEbook": false
   },
   "accessInfo": {
    "country": "IN",
    "viewability": "NO_PAGES",
    "embeddable": false,
    "publicDomain": false,
    "textToSpeechPermission": "ALLOWED",
    "epub": {
     "isAvailable": false
    },
    "pdf": {
     "isAvailable": false
    },
    "webReaderLink": "http://books.google.co.in/books/reader?id=j5jFmAEACAAJ&hl=&printsec=frontcover&output=reader&source=gbs_api",
    "accessViewStatus": "NONE"
   }
  }
 ]
}

现在我想从上面获取标题,作者,pagecount并使用PHP在我的页面中打印它?

1 个答案:

答案 0 :(得分:6)

你可能会发现这个有用的

$page = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=isbn:9789381141977");

$data = json_decode($page, true);

echo "Title = " . $data['items'][0]['volumeInfo']['title'];
echo "Authors = " . @implode(",", $data['items'][0]['volumeInfo']['authors']);    
echo "Pagecount = " . $data['items'][0]['volumeInfo']['pageCount'];