forked from hypercities/hypercities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookSearch.php
More file actions
62 lines (47 loc) · 1.38 KB
/
Copy pathbookSearch.php
File metadata and controls
62 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
$startIndex = 1;
$maxResults = 20;
function throw_error($message) {
die(json_encode(array("message" => $message)));
}
if (isset($_GET['startIndex'])) $startIndex = $_GET['startIndex'];
if (isset($_GET['keyword'])) {
$keyword = urlencode('"' . $_GET['keyword'] . '"');
} else {
throw_error("Keyword not found");
}
$url = "http://www.google.com/books/feeds/volumes?q=$keyword&min-viewability=partial&start-index=$startIndex&max-results=$maxResults";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$doc = simplexml_load_string($result);
$results = array();
//print_r ($doc);
class BookSearchResult {
public $id, $title, $author;
public function __construct ($id, $title, $author) {
$this->id = $id;
$this->title = $title;
$this->author = $author;
}
}
foreach ($doc->entry as $entry) {
//print_r ($entry);
//die();
$results[] = array (
"id" => (string)$entry->id,
"title" => (string)$entry->title,
"thumbnail" => (string)$entry->link[0]->attributes()->href,
//"author" => $entry->{"dc:creator"}
"author" => array_map(function ($author) {
return (string)$author;
}, $entry->xpath("dc:creator"))
);
//$results[] = new BookSearchResult($entry->id[0], $entry->title[0],$entry->{"dc:creator"});
}
$results = array (
"results" => $results
);
//echo json_encode($results, JSON_FORCE_OBJECT);
echo json_encode($results);
?>