【问题标题】:Importing XML-RPC data to SQL table将 XML-RPC 数据导入 SQL 表
【发布时间】:2011-09-02 17:12:12
【问题描述】:

我想说实话,所以,我会从一开始就告诉你,我不太擅长这种东西。我是 PHP/SQL 等的新手,我现在有点被这个问题困扰,所以,任何帮助都将不胜感激。 :)


接下来是我的问题:

在一个网站上,我拥有使用 API 的访问权限(密钥)。但是,问题在于它的 XML-RPC。我已阅读所有指南,并尝试在谷歌上搜索某种解决方案,但我没有运气。

我想将 XML-RPC 数据直接导入现有的 SQL 表。


这是我想用来调用我需要的数据的方法:

Name: "money.received"
Arguments: array ("API Key", "PlayerName","Minimum Timestamp (0 is fine for full history)")
Returns: array(array (from, amount, timestamp))

这是我已经准备好的代码:

<?php 
$request2 = xmlrpc_encode_request("money.received", array('key','bware96', '0'));
$context2 = stream_context_create(array('http' => array(
    'method' => "POST",
    'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
    'content' => $request2
)));

$file2 = file_get_contents("http://www.test.net/xmlrpc.php", false, $context2);
$response2 = xmlrpc_decode($file2);
if ($response2 && xmlrpc_is_fault($response2)) {
    trigger_error("xmlrpc: $response2[faultString] ($response2[faultCode])");
} else {
    echo "<B>Money Received</B><BR>";
   # var_dump($response2);
    echo "<BR>";



        echo "<table border='1'>";
     echo displayTree($response2);
        echo "</table>";


}

function displayTree($var) {
     $newline = "\n";
     foreach($var as $key => $value) {
         if (is_array($value) || is_object($value)) {
             $value = $newline . "<tr>" . displayTree($value) . "</tr>";
         }

         if (is_array($var)) {
             if (!stripos($value, "<li>")) {
                $output .=  "<td>" . $value . "</td>" . $newline;
             }
             else {
                $output .= $value . $newline;
             }

         }


     }

     return $output;
}
?>

您可以在此处找到该代码的结果:Test page

是的,正如您所见,函数 displayTree 对它进行了排序,该 xml 中的所有数据。但是,我想将该数据导入 SQL,问题是我不知道如何。

我想将这些行中的每一行分别导入名为 ,,client" 的 SQL 表中,以便稍后将其整理出来。:)

所以,请,任何帮助都会受到重视,即使它只是链接到一些真正有用的页面,我可以在其中找到我的解决方案。 :)

提前致谢,
劳伦

【问题讨论】:

  • 您能否发布一份由您的 XML-RPC 调用返回的有效负载的副本? XML-RPC 只是一种进行远程函数调用的方法。它应该像函数调用一样,返回一些东西。根据某些格式的内容,将确定您需要做什么才能将其插入数据库。

标签: php sql import xml-rpc


【解决方案1】:

我实际上并没有查看您的 xml 格式,所以这是示例代码,而不是用于插入的代码,如果我有机会迟到,我会回来仔细查看您的数据。

我使用类似这样的东西通过 ms_sql 中的存储过程导入 xml。 我将 xml 作为参数传递给存储过程。然后将 xml 插入到一个临时表中,然后我可以使用标准 sql 对其进行处理。

CREATE PROCEDURE [dbo].[procXMLImport] 
@pvchCustomXML varchar(max) = null, 
@piError int = 0 output,    -- return error code as output parameter for c++ code
@pvchError varchar(200) = '' output

as
begin

    declare @rc int;

    create table #import
    ( id int, val varchar(200)) 


    if ( @pvchCustomXML is not null)
    begin

        declare @xml_id int

        exec @rc = sp_xml_preparedocument @xml_id OUTPUT, @pvchCustomXML ;

        if (@@error != 0 or @rc != 0) 
        begin
            exec sp_xml_removedocument @xml_id;

            set @pvchError = 'sp_xml_preparedocument failed'
            set @piError = -1
            return @piError;
        end 

        -- put values into temps table
        -- not strictly required but seperates potential errors

        insert  #import
        select  id, val

        from    
        openxml ( @xml_id , 'Custom/Lines/Line', 1 ) -- 'Custom/Lines/Line' specifies where in the xml structure to extract the data from
        with    ( id int, val varchar(200) ) 

        if (@@error != 0) 
        begin
            exec sp_xml_removedocument @xml_id;
            set @pvchError = 'import failed'
            set @piError = -2
            return @piError;
        end;

        --clean up xml , no longer required
        exec sp_xml_removedocument @xml_id;

    end

    select * from #import   
end

Mysql有一个函数ExtractValue(),看起来和'from openxml'有类似的用途。

http://dev.mysql.com/doc/refman/5.1/en/xml-functions.html

而且这家伙似乎有一些使用 mysql 和 php 数据的 php 类:

http://www.phpclasses.org/package/782-PHP-Insert-XML-in-MySQL-and-export-MySQL-to-XML.html

【讨论】:

  • SQL Server 2005/2008 大大改进了他们处理 XML 的方法。虽然上面建议的代码仍然有效,但使用 xml 数据类型的新语法使用起来更加直接。
猜你喜欢
  • 2011-12-16
  • 2013-09-08
  • 2021-10-26
  • 2010-10-17
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 2018-05-06
相关资源
最近更新 更多