[英]Importing XML-RPC data to SQL table
我想說實話,因此,我將從一開始就告訴您,我對這種東西不太好。 我是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;
}
?>
您可以在這里找到該代碼的結果: 測試頁
是的,正如您所看到的,函數displayTree會對該XML中的所有數據進行排序。 但是,我想將該數據導入到SQL中,問題是我不知道該怎么做。
我想將這些行中的每行分別導入到名為“,client”的SQL表中,以便稍后進行排序。:)
因此,請幫助,即使是僅鏈接到我可以找到我的解決方案的一些實際有用的頁面的幫助,也將得到切實的幫助。 :)
提前致謝,
勞倫
我實際上沒有看過您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/zh-CN/xml-functions.html
這個家伙似乎有一些使用mysql和php數據的php類:
http://www.phpclasses.org/package/782-PHP-Insert-XML-in-MySQL-and-export-MySQL-to-XML.html
和
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.