一尘不染

PHP cURL,提取XML响应

php

我在服务器上调用PHP
cURL方法,响应为XML类型。cURL将输出(在删除标记之后)保存在标量类型变量中。有没有一种方法可以将其存储在对象/哈希/数组中,以便于解析?


阅读 291

收藏
2020-05-26

共1个答案

一尘不染

<?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";
}
2020-05-26