如何对 UPC 数据库进行 PHP XML/RPC 调用

发布于 2024-10-12 21:47:42 字数 1362 浏览 3 评论 0原文

干草我正在研究条形码阅读器项目,当我从我的 php 脚本调用 upcdatabase 时,它​​给了我错误。我使用 www.upcdatabase.com 提供的 php 示例,

代码是

<?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";
 }
?>

当我检查 $upc='0639382000393' 时;进入upc数据库查看这个然后它工作正常但我在浏览器中运行这个脚本然后它给出出现以下错误

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

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
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2024-10-19 21:47:42

不幸的是,他们的 API 文档似乎相当简短。

该网站在 商品查找页面上提到了三种类型的代码:

  • EAN/13 位数字UCC-13
  • A 型 UPC 代码为 12 位数字,
  • E 型(零抑制)UPC 代码为 8 位数字。

在页面提到这三种类型之后,它还说,

除 8 或 12 位数字之外的任何数字都不是 UPC 代码!

13 位 EAN/UCC-13UPC。它包含有效的 UPC,但还有许多其他不是有效 UPC 的值。

来自有关 EAN-13 的维基百科文章

如果第一位数字为零,则第一组六位中的所有数字均使用 UPC 使用的模式进行编码,因此 UPC 条形码也是第一位数字设置为零的 EAN-13 条形码。

话虽如此,当我从 $upc 中删除前导零时,它按预期工作。显然,“项目查找”页面具有删除前导零的逻辑,而 API 则没有。

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
)

或者,您可以将原始 13 位值设置为 ean 参数,而不是设置 upc 参数,它也可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文