我在服务器上调用 PHP cURL 方法,响应是 XML 类型。 cURL 将输出(删除标签后)保存在标量类型变量中。有没有办法将它存储在对象/哈希/数组中以便于解析?
最佳答案
<?php
function download_page($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
$sXML = download_page('http://alanstorm.com/atom');
$oXML = new SimpleXMLElement($sXML);
foreach($oXML->entry as $oEntry){
echo $oEntry->title . "\n";
}
关于PHP cURL,提取 XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/561816/