andrewhthomas
8/3/2017 - 2:08 PM

UNM Google Books fetch

UNM Google Books fetch

<?php
$node         = node_load($data->nid);
$node_wrapper = entity_metadata_wrapper('node', $node);
$product      = $node_wrapper->field_product[0];
$product_isbn = str_replace('-','', $product->field_isbn->value());

$url = "https://www.googleapis.com/books/v1/volumes?q=isbn:" .  $product_isbn . "&country=US";
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'UNM Press');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
$preview = json_decode($query);
if ($preview->items[0]->id) {
	if ($preview->items[0]->accessInfo->embeddable == true) {
		echo '<br><div><a class="colorbox-load btn btn-secondary btn-block" href="https://books.google.com/books?id=' . $preview->items[0]->id .'&lpg=PP1&pg=PP1&output=embed&width=720&height=480&iframe=true">Google Preview</a></div>';
	}
}
?>