开发者

Importing XML-RPC data to SQL table

I want to be honest, so, I will tell you from the start that I'm not too good with this kind of stuff. I'm new to PHP/SQL etc., and I'm kinda stucked with this problem I have at this moment, so, any help will be appreciated. :)


My problem is next:

On one website, I have access(key) for using API. But, problem is that its XML-RPC. I have read all the guides, and try googling for some sort of solution, but I had no luck with that.

I want to import XML-RPC data directly to existing SQL table.


This is the method I want to use to call data I need:

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

And this is the code I have allready:

<?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;
}
?>

The result of that code you can find here: Test page

So yea, as you can see, function displayTree kinda sorts it up, all the data in that xml. But, I want to import that data to SQL, and the problem is that I dont know how.

I want to import each of those rows separately into SQL table called ,,client", so I can later sort it out. :)

So, please, any help will be reaaaaaally appreaciated, even if its just link to some actually helpfull page where I can find my solution. :)

Thanks in advance,

Lauren


I haven't actually looked at the format of your xml, so this is sample code, not code that will do your insert, If I have a chance late I come back and have a closer look at your data.

I use something like this to import xml using a stored procedure in ms_sql. I pass xml as a parameter to the stored procedure. The xml is then inserted into a temp table, which I can then process using standard 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 has a function ExtractValue(), which seems to have a similar purpose to 'from openxml'.

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

and this guy seems to have some php classes for using mysql with php data:

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

and

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜