hunter
hunter

Reputation: 1101

How to make a PHP XML/RPC call to UPC database

hay i am working on barcode reader project when i call upcdatabase from my php script it give me errors. i use the php example provided by www.upcdatabase.com

the code is

<?php error_reporting(E_ALL);
ini_set('display_errors', true);

 require_once 'XML/RPC.php';

 $rpc_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Set your rpc_key here
 $upc='0639382000393';
 // Setup the URL of the XML-RPC service
 $client = new XML_RPC_Client('/xmlrpc', 'http://www.upcdatabase.com');
 $params = array( new XML_RPC_Value( array(
  'rpc_key' => new XML_RPC_Value($rpc_key, 'string'),
  'upc' => new XML_RPC_Value($upc, 'string'),
  ), 'struct'));
 $msg = new XML_RPC_Message('lookup', $params);
 $resp = $client->send($msg);
 if (!$resp)
            {
  echo 'Communication error: ' . $client->errstr;
  exit;
 }
 if(!$resp->faultCode())
 {
  $val = $resp->value();
  $data = XML_RPC_decode($val);
  echo "<pre>" . print_r($data, true) . "</pre>";
 }else{
  echo 'Fault Code: ' . $resp->faultCode() . "\n";
  echo 'Fault Reason: ' . $resp->faultString() . "\n";
 }
?>

when i check the $upc='0639382000393'; into upc data base view this then it works fine but i run this script into the browser then it give the following error

Array
(
    [status] => fail
    [message] => Invalid UPC length
)

Upvotes: 0

Views: 2097

Answers (1)

Wiseguy
Wiseguy

Reputation: 20883

Unfortunately, their API appears rather short on documentation.

There are three types of codes the site mentions on the Item Lookup page:

  • 13 digits for an EAN/UCC-13
  • 12 digits for a Type A UPC code, or
  • 8 digits for a Type-E (zero-supressed) UPC code.

Right after the page mentions those three types, it also says,

Anything other than 8 or 12 digits is not a UPC code!

The 13-digit EAN/UCC-13 is a superset of UPC. It includes valid UPCs, but it has many other values that are not valid UPCs.

From the Wikipedia article on EAN-13:

If the first digit is zero, all digits in the first group of six are encoded using the patterns used for UPC, hence a UPC barcode is also an EAN-13 barcode with the first digit set to zero.

Having said that, when I removed the leading zero from $upc, it worked as expected. Apparently the Item Lookup page has logic to remove the leading zero, while the API does not.

Array
(
    [upc] => 639382000393
    [pendingUpdates] => 0
    [status] => success
    [ean] => 0639382000393
    [issuerCountryCode] => us
    [found] => 1
    [description] => The Teenager's Guide to the Real World by BYG Publishing
    [message] => Database entry found
    [size] => book
    [issuerCountry] => United States
    [noCacheAfterUTC] => 2011-01-22T14:46:15
    [lastModifiedUTC] => 2002-08-23T23:07:36
)

Alternatively, instead of setting the upc param, you can set the original 13-digit value to the ean param and it will also work.

Upvotes: 3

Related Questions