如何使用 xml-rpc 中的数据更新数据库表?
我想对您立即回复表示感谢。我将非常感谢您的帮助。
更新 我刚刚在这个网站上读到有人提到 xml 数据类型和存储过程。现在我认为搜索 xml-rpc 和更新数据库是不够的。我正在寻找如何使用 xml 数据来更新数据库。但如果您仍然对我下面的问题有想法,我将不胜感激。
我在 Google 上搜索了这个网站,了解通过 xml-rpc 从远程服务器提取的数据的可能性以及如何更新数据库表,但我找不到任何关于如何做这样的事情的信息。我确实找到了大量关于 xml-rpc 的好信息(我对它是全新的),并且更新数据库表的 SQL 命令非常简单。但是我到底如何使用通过 xml-rpc 获取的数据来更新表呢?
我想这是因为我可能没有完全理解xml-rpc。我有一个购物车,我现在想做的就是从供应商处获取库存水平。当用户单击产品链接时,我需要获取实时库存水平并将这些库存水平数字插入到该特定产品的产品库存表中(因此它是动态的)。
我看了一下 php 文件,其中购物车脚本看起来从数据库中获取了商品的库存水平。看来我会在抓取之前添加两件事。 1. 用于按 sku 获取库存水平的 xml-rpc 调用,以及 2. 用于将该数据插入数据库的 SQL 更新命令。
但是,一旦我执行 xml-rpc 调用并返回库存水平,这些数据会去哪里?根据我的阅读,这些数据似乎是通过某种打印或直接在网络浏览器上显示结果的方式向用户显示的。但我需要将这些数据移动到数据库表中,而不是这样做。
有什么想法吗?这可能吗?
这是我编辑的函数,用于尝试获取库存水平并更新表格:我已将这两部分添加到现有函数中 - // 按 sku 从供应商处获取库存水平,< strong>//按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' /></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'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' /></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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过多次尝试和错误我想通了。如果其他人有同样的问题,我会分享。我编写的这段代码根据产品 ID 从数据库中获取 sku 编号。然后,它通过 xml-rpc 根据 sku 向供应商请求库存水平,获取响应,然后更新数据库中产品 ID 的当前库存水平。
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.
这些信息应该就在您的回复中。您是否尝试过 var_dump($response) 看看您有什么信息?
Xml-Rpc 它只是用于客户端和服务器之间通信的协议。它实际上并不是很先进,并且数组结构很容易用于通信,没有问题。
所以服务器响应的内容应该就在 var $response 中。
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.