-
Notifications
You must be signed in to change notification settings - Fork 2
/
EANSearch.php
114 lines (99 loc) · 3.55 KB
/
EANSearch.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
* A PHP class for EAN and ISBN name lookup and validation using the API on ean-search.org
*
* (c) Jan Willamowius
* Relaxed Communications GmbH, 2017 - 2021
* https://www.ean-search.org/ean-database-api.html
*
*/
class EANSearch {
private $accessToken;
private $ctx; # stream context with connect timeout setting
function __construct($accessToken) {
$this->accessToken = $accessToken;
$this->ctx = stream_context_create(array('http' => array('timeout' => 180)));
ini_set('default_socket_timeout', 180);
}
// only return the product name
function barcodeLookup($ean, $lang = 1) {
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=barcode-lookup&token=$this->accessToken&ean=$ean&language=$lang", false, $this->ctx);
if ($xml === FALSE) {
return '';
}
$response = new SimpleXMLElement($xml);
return $response->product->name;
}
// return all product data
function barcodeSearch($ean, $lang = 1) {
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=barcode-lookup&token=$this->accessToken&ean=$ean&language=$lang", false, $this->ctx);
if ($xml === FALSE) {
return array();
}
$response = new SimpleXMLElement($xml);
return $response->product;
}
// only return the book name
function isbnLookup($isbn) {
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=barcode-lookup&token=$this->accessToken&isbn=$isbn", false, $this->ctx);
if ($xml === FALSE) {
return '';
}
$response = new SimpleXMLElement($xml);
return $response->product->name;
}
function barcodePrefixSearch($prefix, $page = 0) {
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=barcode-prefix-search&token=$this->accessToken&prefix=$prefix&page=$page", false, $this->ctx);
if ($xml === FALSE) {
return array();
}
$response = new SimpleXMLElement($xml);
return $response->xpath('//product');
}
function productSearch($name, $page = 0) {
$name = urlencode($name);
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=product-search&token=$this->accessToken&name=$name&page=$page", false, $this->ctx);
if ($xml === FALSE) {
return array();
}
$response = new SimpleXMLElement($xml);
return $response->xpath('//product');
}
function categorySearch($category, $name = '', $page = 0) {
$name = urlencode($name);
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=category-search&token=$this->accessToken&category=$category&name=$name&page=$page", false, $this->ctx);
if ($xml === FALSE) {
return array();
}
$response = new SimpleXMLElement($xml);
return $response->xpath('//product');
}
function barcodeImage($ean) {
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=barcode-image&token=$this->accessToken&ean=$ean", false, $this->ctx);
$response = new SimpleXMLElement($xml);
return base64_decode($response->product->barcode);
}
function verifyChecksum($ean) {
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=verify-checksum&token=$this->accessToken&ean=$ean", false, $this->ctx);
$response = new SimpleXMLElement($xml);
return $response->product->valid;
}
function issuingCountryLookup($ean) {
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=issuing-country&token=$this->accessToken&ean=$ean", false, $this->ctx);
$response = new SimpleXMLElement($xml);
return $response->product->issuingCountry;
}
function setTimout($sec) {
$this->ctx = stream_context_create(array('http' => array('timeout' => $sec)));
ini_set('default_socket_timeout', $sec);
}
}