XML解析无法使用cURL

时间:2015-12-10 17:14:44

标签: php curl xml-parsing rss

尝试解析rss Feed时,如果我使用不需要身份验证的Feed,则此代码有效。所以我认为它必须是一个卷曲问题。请帮助,谢谢。

   <?php
    $curl = curl_init();
     curl_setopt_array($curl, Array(
    CURLOPT_URL            => 'http://insite.unthsc.edu/dailynews/feed/',
    CURLOPT_USERAGENT      => 'spider',
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_CONNECTTIMEOUT => 30,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_ENCODING       => 'UTF-8'
));
$data = curl_exec($curl);
curl_close($curl);
$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
//die('<pre>' . print_r($xml], TRUE) . '</pre>');
?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>

<?php foreach ($xml->channel->item as $item) {
    $creator = $item->children('dc', TRUE);
    echo '<h2>' . $item->title . '</h2>';
    echo '<h2>' . $item->category . '</h2>';    
}
?>

</body>
</html>

0 个答案:

没有答案