如何使用xml-rpc中的数据更新数据库表?

时间:2010-12-21 14:40:56

标签: php mysql database xml-rpc

我想立刻感谢您的回复。我将非常感谢你的帮助。

UPDATE 我刚刚在这个网站上看到有人提到xml数据类型和存储过程。现在我想搜索xml-rpc并更新数据库是不够的。我正在搜索如何使用xml数据来更新数据库。但如果您对我的问题仍有疑问,我会很感激他们。

我已经用Google搜索并搜索了这个网站的可能性以及如何使用从xml-rpc从远程服务器提取的数据来更新数据库表,而我怎么也找不到如何做这样的事情。我确实在xml-rpc上找到了很多好的信息(我是全新的)并且用于更新数据库表的SQL命令非常简单。但是,我究竟如何使用我使用xml-rpc获取的数据来更新表?

我认为这是因为我可能不完全了解xml-rpc。我有一个购物车,我现在想做的就是从我的供应商处提取库存水平。当用户点击产品链接时,我需要获取实时库存水平并将这些库存水平数字插入到我特定产品的产品库存表中(因此它是即时的)。

我看了一下php文件,其中购物车脚本看起来抓住了数据库中商品的库存水平。看来我会在抢夺之前添加两件事。 1.通过sku和2获取库存水平的xml-rpc调用。用于将该数据插入数据库的SQL更新命令。

但是一旦我做了xml-rpc调用并且它返回了库存水平,这些数据在哪里?从我的阅读中看起来,这些数据通过某种打印或直接显示给Web浏览器显示给用户。但我没有这样做,而是需要将这些数据移动到数据库表中。

有什么想法?这可能吗?

这是我编辑的功能,试图获取库存水平并更新表格:我已将这两个部分添加到现有功能中 - //通过sku从供应商处获取库存水平 //按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' />&nbsp; <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' />&nbsp; <img src='images/ajax-blank.gif' id='loading%d' />", GetLang('Save'), $prodId, $prodId);
    }
   }
  }

2 个答案:

答案 0 :(得分:1)

经过多次试验和错误,我发现了它。我将分享以防其他人有同样的问题。我编写的这段代码根据产品ID从数据库中获取sku编号。然后它通过xml-rpc从sku请求供应商的库存水平,获得响应,然后更新数据库中当前库存水平的产品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);

答案 1 :(得分:0)

信息应该在您的回复中。您是否尝试过var_dump($ response)确实看到了哪些信息?

Xml-Rpc它只是用于在客户端和服务器之间进行通信的协议。它实际上并不是非常先进,并且阵列结构很容易用于通信而没有问题。

所以服务器响应应该在var $ response中。