Jump to content

Recommended Posts

Hey, I'm trying to get this code to work...its a barcode decoder works ok if u enter a 12 digit code but if u enter an 8 or 13 digit EAN or UPC-A it doesn't so i've tried to change it so it reverts to 12 digits each time, only thing is i can't get it to work fluidly and I don't really understand it. Any help would be much appreciated!

 

THANKS GUYS!

 

<?php
extract($_GET);
if(isset($upc))
{
?>
<form method="get" action="<?php print $_SERVER[php_SELF]; ?>">
<p><input type="text" name="upc" value=""/></p>
<p><input type="submit" value="Search"/></p>
</form>
<?php

require_once 'XML/RPC.php';

$rpc_key = 'c737eb5b608f31e5fa022718860f1578cf2cc2a6';	// Set your rpc_key here

// Setup the URL of the XML-RPC service
$client = new XML_RPC_Client('/xmlrpc', 'http://www.upcdatabase.com');

// Construct the entire parameter list (an array) for the call.
// The array contains a single XML_RPC_Value object, a struct.
// The struct is constructed from a PHP associative array, and each
// value needs to be an XML_RPC_Value object.

	//convert the string length to 12
$upc = str_pad(ltrim($upc,'0'), 12, '0', STR_PAD_LEFT);

$params = array( new XML_RPC_Value( array(
	'rpc_key' => new XML_RPC_Value($rpc_key, 'string'),
	'upc' => new XML_RPC_Value($_REQUEST['upc'], 'string'),
	), 'struct'));

// Construct the XML-RPC request.  Substitute your chosen method name
$msg = new XML_RPC_Message('lookup', $params);

//Actually have the client send the message to the server.  Save response.
$resp = $client->send($msg);

//If there was a problem sending the message, the resp will be false
if (!$resp)
{
	//print the error code from the client and exit
	echo 'Communication error: ' . $client->errstr;
	exit;
}

//If the response doesn't have a fault code, show the response as the array it is
if(!$resp->faultCode())
{

	//Store the value of the response in a variable
	$val = $resp->value();
	//Decode the value, into an array.
	$data = XML_RPC_decode($val);
	//Optionally print the array to the screen to inspect the values
	echo "<pre>" . print_r($data, true) . "</pre>";
	echo '<p>'.$data['status'].'</p>';
	echo '<p>'.$data['upc'].'</p>';
	echo '<p>'.$data['description'].'</p>';

}else{
	//If something went wrong, show the error
	echo 'Fault Code: ' . $resp->faultCode() . "\n";
	echo 'Fault Reason: ' . $resp->faultString() . "\n";

}	
}

?>

<p>Enter a 12 digit UPC Barcode</p>
<form method="get" action="<?php print $_SERVER[php_SELF]; ?>">
<p><input type="text" name="upc" value=""/></p>
<p><input type="submit" value="Search"/></p>
</form>

 

Link to comment
https://forums.phpfreaks.com/topic/231196-anyone-understand-this-code/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.