dtv11049 2014-03-15 02:33
浏览 105

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条回答 默认 最新

  • doufeng1249 2014-03-15 07:30
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试