Location: PHPKode > projects > Grocery List > include/upcdb.php
<?php
########################################################################
#
# Project: Grocery List
# URL: http://sourceforge.net/projects/grocery-list/
# E-mail: hide@address.com
#
# Copyright: (C) 2010, Neil McNab
# License: GNU General Public License Version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Filename: $URL: https://grocery-list.svn.sourceforge.net/svnroot/grocery-list/releases/1.0/include/upcdb.php $
# Last Updated: $Date: 2010-03-07 13:12:00 -0800 (Sun, 07 Mar 2010) $
# Author(s): Neil McNab
#
# Description:
#   Interfaces with www.upcdatabase.com XML-RPC API to get UPC info.
#
########################################################################

/*
upcdatabase XML-RPC information

help - returns string
        Show available functions and their parameters.

lookupEAN(ean string) - returns struct
        Lookup upc database entry.

lookupUPC(upc string) - returns struct
        Lookup upc database entry.
        DEPRECATED!  Use 'lookupEAN' instead.

writeEntry(username string, password string, ean string, description string, pkgsize string)
        Add or modify an entry in the database.
        This function is unimplemented at this time,
        but is here for API review.

calculateCheckDigit(partialean string) - returns string
        Parameter 'ean' should have 'C' or 'X' in
        place of the check digit (last character).
        Length of 'partialean' parameter should be
        11 or 12 digits, plus 'X' or 'C' character.

convertUPCE(upce string) - returns string
        Parameter 'upce' should be exactly 8 digits.
        Returns full EAN-13.

decodeCueCat(cuecatscan string) - returns struct
        Returns serial number, type, and code given
        CueCat scanner output.

latestDownloadURL - returns string
        Return URL of latest full database download.

All 'upc' parameters should be 12 digits long.
All 'ean' parameters should be 13 digits long.

*/

define('RPC_URL', 'http://www.upcdatabase.com/rpc');

function upc_lookup($Item) {
  $Item = trim($Item);

  if (!function_exists('xmlrpc_encode_request')) {
    print "<p>WARNING: Cannot do UPC lookups because <a href='http://us.php.net/xmlrpc'>XML-RPC</a> is not installed for PHP.</p>";
    return FALSE;
  }

  if (!is_numeric($Item)) {
    return TRUE;
  }
  $request = xmlrpc_encode_request('lookupEAN', sprintf("%013d", intval($Item)));

  $file = upc_http_post($request);

  if ($file === FALSE) {
    return FALSE;
  }

  $response = xmlrpc_decode($file);
  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } elseif (!$response) {
    print "<p>ERROR parsing response</p>";
  } else {
    return $response;
  }

    return FALSE;
}

function upc_http_post($request) {
  $file = FALSE;
  if (function_exists('curl_init')) {
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, RPC_URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $file = curl_exec($ch);
    if ( curl_getinfo($ch,CURLINFO_HTTP_CODE) !== 200 ) {
      return FALSE;
      //print 'Bad Data File '. RPC_URL . " " . curl_getinfo($ch,CURLINFO_HTTP_CODE); # . " " . $file);
    }
  } else {
  // PHP built-in method, not secure, using as failback only
    $context = stream_context_create(array('http' => array(
    'method' => "POST",
    'header' => "Content-Type: text/xml",
    'content' => $request
    )));
    $file = file_get_contents(RPC_URL, false, $context);
  }
  return $file;

}

?>
Return current item: Grocery List