Skip to content

Commit

Permalink
auto uuencode search strings, set HTTP timeout, better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
willamowius committed Aug 5, 2021
1 parent a0e82c5 commit 09a994f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
37 changes: 29 additions & 8 deletions EANSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,81 @@
/*
* A PHP class for EAN and ISBN name lookup and validation using the API on ean-search.org
*
* (c) Jan Willamowius, 2017
* Relaxed Communications GmbH, 2017
* (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;
$ctx = stream_context_create(array('http' => array('timeout' => 180)));
# ini_set('default_socket_timeout', 180);
}

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");
. "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;
}

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");
. "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;
}

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");
. "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");
. "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 barcodeImage($ean) {
$xml = file_get_contents("https://api.ean-search.org/api?"
. "op=barcode-image&token=$this->accessToken&ean=$ean");
. "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");
. "op=verify-checksum&token=$this->accessToken&ean=$ean", false, $this->ctx);
$response = new SimpleXMLElement($xml);
return $response->product->valid;
}

function setTimout($sec) {
$ctx = stream_context_create(array('http' => array('timeout' => $sec)));
ini_set('default_socket_timeout', $sec);
}

}

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ echo "$ean is $product->name from category $product->categoryName issued in $pro
$ok = $eanSearch->verifyChecksum($ean);
echo "$ean is " . ($ok ? 'valid' : 'invalid') . "\n";

$eanList = $eanSearch->productSearch('iPod');
$eanList = $eanSearch->productSearch('Apple iPod');
foreach ($eanList as $product) {
echo "$product->ean is $product->name\n";
}
Expand Down

0 comments on commit 09a994f

Please sign in to comment.