How do I update a database table using data from xml-rpc?
I'd like to say thank you for any replies right away. I will truly appreciate your help.
UPDATE I just read on this site someone mention xml datatypes and stored procs. Now I thinking searching for xml-rpc and update database wasn't enough. I'm searching for how to use xml data to update a database. But if you still have thoughts on my questions below, I'd appreciate them.
I've Googled and searched this site on the possibility and the how of using data pulled from a remote server via xml-rpc to update a database table and I could find nothing about how to do such a thing. I did find tons of good info on xml-rpc (I'm brand new at it) and a SQL command to update a database table is pretty simple. But how on earth do I use the data I grab using xml-rpc to update a table?
I think it's because I probably don't fully understand xml-rpc. I have a shopping cart and all I want to do right now is pull the stock levels from my supplier. When users click on the link to a product I need to grab the real time stock levels and insert these stock level numbers into my product inventory table for that particular product (so it's on the fly).
I took a peek at the php file where the cart script looks grabs the stock level of the item from the database. It seems I would add two things right before that grab. 1. The xml-rpc call to get stock levels by sku and 2. A SQL update command to insert that data into the database.
But once I do the xml-rpc call is made and it returns the stock levels, where does this data go? From my reading, it looks like this data is revealed to user by some kind of print or display results directly to the web browser. But instead of doing that I need to move this data into a database table.
Any thoughts? Is this possible?
This is the function I've edited to try to grab the stock levels and update the table: I've added these two sections to the existing function - // Grab inventory level by sku from supplier, // Update inventory table by sku
/**
* Show the inventory management quick view on the manage products page if inventory tracking is on for a product
*
* @return void
**/
private function GetInventoryLevels()
{
$GLOBALS['ISC_CLASS_ADMIN_ENGINE']->LoadLangFile('products');
if(isset($_REQUEST['p']) && isset($_REQUEST['i']) && isset($_REQUEST['v']) && isset($_REQUEST['t'])) {
$prodId = (int)$_REQUEST['p'];
$invType = (int)$_REQUEST['i'];
$variationId = (int)$_REQUEST['v'];
$combinations = array();
// First determine if inventory tracking is by product or by option
if ($invType == 1) {
**// Grab inventory level by sku from supplier**
$server_url = "http://gg.com/ttt/webservices/index.php";
$prodcurrentinv = "";
if (function_exists('xmlrpc_encode_request')) {
$request = xmlrpc_encode_request("catalog.getStockQuantity(sku)", array($prodcurrentinv));
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
)));
$file = file_get_contents($server_url, false, $context);
$response = xmlrpc_decode($file);
if (xmlrpc_is_fault($response)) {
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print '<pre>';
print_r($response);
print '</pre>';
}
} else {
print '<div style="color:red;">Sorry, you don\'t seem to have the xmlrpc module compiled in.</div>';
}
print '<hr/>';
require_once 'XML/RPC2/Client.php';
// since we're using a 'catalog' function, we need to make sure it prefixes the function
// name when it's called on the server. (The XML_RPC2 docs suggest that you could use
// 'catalog.getStockQuantity(sku)' as the class function name, but that's not correct.
$options = array(
'prefix' => "catalog."
);
$client = XML_RPC2_Client::create($server_url, $options);
$result = $client->getStockQuantity(sku)($prodcurrentinv);
print '<pre>';
print_r($result);
print '</pre>';
print '<hr/>';
**// Update inventory table by sku**
$query = sprintf("update prodcurrentinv from [|PREFIX|]products where productcode='%d'",)
// Simply query the products table for current and low stock levels
$query = sprintf("select prodcurrentinv, prodlowinv from [|PREFIX|]products where productid='%d'", $GLOBALS['ISC_CLASS_DB']->Quote($prodId));
$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
if($row = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
printf("<b style='font-size:13px; padding-bottom:5px'>%s</strong>", GetLang("UpdateInventoryLevels"));
echo "<table border='0'>";
echo "<tr>";
echo "<td valign='top'><img src='images/nodejoin.gif' style='padding-top:5px' /></td>";
printf("<td>%s:</td>", GetLang("CurrentStock"));
printf("<td><input type='text' size='3' value='%d' name='stock_level_%d' id='stock_level_%d' /></td>", $row['prodcurrentinv'], $prodId, $prodId);
echo "</tr>";
echo "<tr>";
echo "<td>";
printf("<td>%s:</td>", GetLang("LowStockLevel"));
printf("<td><input type='text' size='3' value='%d' name='stock_level_notify_%d' id='stock_level_notify_%d' /></td>", $row['prodlowinv'], $prodId, $prodId);
echo "</tr>";
echo "</table>";
printf("<input class='StockButton' type='button' value='%s' onclick='UpdateStockLevel(%d, 0)' style='margin-left:110px' /> <img src='images/ajax-blank.gif' id='loading%d' />", GetLang("Save"), $prodId, $prodId);
}
} else {
$optionIds = array();
// Fetch out the variation combinations for this product
$query = "SELECT * FROM [|PREFIX|]product_variation_combinations WHERE vcproductid='".$prodId."'";
$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
while($combination = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
$combinations[] = $combination;
$optionIds = array_merge($optionIds, explode(",", $combination['vcoptionids']));
}
$optionIds = array_unique($optionIds);
// Now fetch out the options we need to get
if(!empty($optionIds)) {
$optionIds = implode(",", $optionIds);
// Get the combination options
$variations = array();
$query = "SELECT * FROM [|PREFIX|]product_variation_options WHERE voptionid IN (".$optionIds.")";
$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
while($variation = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
$variations[$variation['voptionid']] = array($variation['voname'], $variation['vovalue']);
}
}
printf("<b style='font-size:13px'>%s</strong><div style='padding:20px 20px 0px 20px'>", GetLang("UpdateInventoryLevels"));
foreach($combinations as $row) {
$output = "";
$options = explode(",", $row['vcoptionids']);
foreach($options as $option) {
$output .= isc_html_escape($variations[$option][0]) . ": " . isc_html_escape($variations[$option][1]) . ", ";
}
$output = trim($output, ', ');
echo "<strong><em>" . $output . "</em></strong>";
echo "<br />";
echo "<table border='0' style='padding-bottom:10px'>";
echo "<tr>";
echo "<td valign='top'><img src='images/nodejoin.gif' style='padding-top:5px' />&l开发者_运维知识库t;/td>";
printf("<td>%s:</td>", GetLang("CurrentStock"));
printf("<td><input type='text' size='3' value='%d' name='stock_level_%d_%d' id='stock_level_%d_%d' /></td>", $row['vcstock'], $prodId, $row['combinationid'], $prodId, $row['combinationid']);
echo "</tr>";
echo "<tr>";
echo "<td>";
printf("<td>%s:</td>", GetLang("LowStockLevel"));
printf("<td><input type='text' size='3' value='%d' name='stock_level_%d_%d' id='stock_level_notify_%d_%d' /></td>", $row['vclowstock'], $prodId, $row['combinationid'], $prodId, $row['combinationid']);
echo "</tr>";
echo "</table>";
}
echo "</div>";
printf("<input class='StockButton' type='button' value='%s' onclick='UpdateStockLevel(%d, 1)' style='margin-left:130px' /> <img src='images/ajax-blank.gif' id='loading%d' />", GetLang('Save'), $prodId, $prodId);
}
}
}
I figured it out after much trial and error. I'll share in case others have the same question. This bit of code I worked out grabs the sku number from the database based on the product ID. Then it requests the stock level from the supplier based on the sku via xml-rpc, gets the response back and then updates the current inventory level in the database for the product id.
//Grab the stock level of the item from supplier server
$query = sprintf("SELECT prodcode FROM [|PREFIX|]products where productid='%d'",
$GLOBALS['ISC_CLASS_DB']->Quote($prodId));
$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
$product_sku = mysql_fetch_row($result);
$product_sku2 = $product_sku[0];
$server_url = "http://m.com/fvg/webservices/index.php";
$request = xmlrpc_encode_request("catalog.getStockQuantity", array($product_sku2));
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
)));
$file = file_get_contents($server_url, false, $context);
$response = xmlrpc_decode($file);
$query = sprintf("UPDATE [|PREFIX|]products SET prodcurrentinv='$response' where productid='%d'", $GLOBALS['ISC_CLASS_DB']->Quote($prodId));
$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
The information should be right there in your response. Did you try a var_dump($response) do see what info do you have ?
Xml-Rpc it's just a protocol used to communicate between a client and a server. It's not actually very advance and the array structure is easy to used for communication without a problem.
So what the server responds should be right there in the var $response.
精彩评论