PHP:使用curl获取文件的XML解析 - 无法解析为XML对象?

Hey guys so I have the following script which does seem to work as expected because the curl returns a blurb of text instead:

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

$xml_str = download_page('http://api.npr.org/list?id=3002');
echo $xml_str; //SPITS OUTS A BLURB OF TEXT
$xml = new SimpleXMLElement(file_get_contents("test.xml"));
$items = $xml->xpath('*/item');
print_r($items); //RETURNS Array()
foreach($items as $item) {
    echo "one time";
    echo $item['title'], ': ', $item['description'], "
";
}
?>

I have no idea how to download the XML page into a normal XML format rather than this blurb of text.

1个回答



尝试使用发送请求从服务器获取 text / xml </ code>。</ p>
\ n

  curl_setopt($ ch,CURLOPT_HTTPHEADER,array(“Accept:text / xml”)); 
</ code> </ pre>

或使用 application / xml </ code>。</ p>
</ div>

展开原文

原文

Try using sending a request to get text/xml from the server.

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: text/xml"));

or using application/xml.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐