Skip to content

Commit

Permalink
add categorySearch() and issuingCountryLookup()
Browse files Browse the repository at this point in the history
  • Loading branch information
willamowius committed May 21, 2023
1 parent b8ac074 commit c243e95
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions EANSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function __construct($accessToken) {
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);
Expand All @@ -29,6 +30,7 @@ function barcodeLookup($ean, $lang = 1) {
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);
Expand Down Expand Up @@ -60,6 +62,17 @@ function productSearch($name, $page = 0) {
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);
Expand All @@ -74,6 +87,13 @@ function verifyChecksum($ean) {
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);
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@ foreach ($eanList as $product) {
echo "$product->ean is $product->name\n";
}

$eanList = $eanSearch->categorySearch(45, 'Thriller');
foreach ($eanList as $product) {
echo "$product->ean from Music category is $product->name\n";
}

$eanList = $eanSearch->barcodePrefixSearch(4007249146);
foreach ($eanList as $product) {
echo "$product->ean is $product->name\n";
}

$ean = '5099750442227';
$country = $eanSearch->issuingCountryLookup($ean);
echo "$ean was issued in $country\n";

//$ean = '5099750442227';
//$barcode = $eanSearch->barcodeImage($ean);
//header("Content-Type: image/png");
// echo $barcode;
```

0 comments on commit c243e95

Please sign in to comment.