2009-12-11 77 views
0

我試圖從多個XML源獲取數據並將該數據複製到本地數據庫。我試圖研究SimpleXML和我在互聯網上發現的其他一些東西,但我想知道用這樣的東西採取什麼最好的路線。從https URL獲取XML

我正在尋找的東西不僅會從安全位置獲取XML,還會將其轉換爲一系列數組。

回答

3

這是一個非常簡單的過程,您可以使用CURL和某種類型的XML來完成數組類。我會在這裏給你詳細的介紹。

的PHP:

/* run mozilla agent */ 
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); //make it act decent 
curl_setopt($ch, CURLOPT_URL, $url);   //set the $url to where your request goes 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //set this flag for results to the variable 
curl_setopt($ch, CURLOPT_POST, 1);   //if you're making a post, put the data here 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //as a key/value pair in $post 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //This is required for HTTPS certs if 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //you don't have some key/password action 

/* execute the request */ 
$result = curl_exec($ch); 
curl_close($ch); 

/* now parse the resulting XML using XMLToArray class from 
Razzaque Rupom http://groups.yahoo.com/group/phpresource/ */ 
require_once('lib/class.XmlToArray.php'); 
$parser = new XmlToArray($result); 
$data = $parser->createArray(); 

有你有它。

  • 米奇
+1

你可以在這裏找到他所指的XmltoArray類:http://www.weberdev.com/get_example-4416.html – Webnet 2009-12-11 17:22:25

0

使用SimpleXML時,您將XML作爲包含節點數組的對象。由於簡單性和整體性能,我個人更喜歡使用SimpleXML而不是其他XML解析器。在操作數據時,它很容易用作DOM操縱器,並且在用作XPath解析器時,只需輕鬆檢索少數幾個節點。

如果您遍歷一個非常大的XML文件,您可能會更好地使用加載懶惰的直線式SAX解析器,但是由於您通過網絡讀取,我認爲性能差異可以忽略不計。