【问题标题】:Uncaught exception 'Exception' with message 'String could not be parsed as XML on line 48 [duplicate]第 48 行带有消息“无法将字符串解析为 XML”的未捕获异常“异常”[重复]
【发布时间】:2011-05-10 11:21:49
【问题描述】:

致命错误:堆栈跟踪中未捕获的异常“异常”和消息“字符串无法解析为 XML”:C:\xampp\htdocs\trendfeeds.php(48): SimpleXMLElement->__construct('') {main } 在第 48 行抛出

echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">';
echo '<channel>';
echo '<title>Games - Free Download - Freshwap</title>';
echo '<link>http://www.freshwap.com/</link>';
echo '<language>en</language>';
echo '<description>Games - Free Download - Freshwap</description>';

$url="http://www.google.com/trends/hottrends/atom/hourly";
$beg="<li>";
$end="</li>";
$tryscrape="1";
$end=str_replace('/','\/', $end);
$beg=str_replace('/','\/', $beg);
$end=str_replace('\"','"', $end);
$beg=str_replace('\"','"', $beg);

if($tryscrape==1)
{
$data = file_get_contents($url);
$regex = '/'.$beg.'(.+?)'.$end.'/';
$count=1;
preg_match_all($regex,$data,$match,PREG_SET_ORDER);
foreach ($match as $result) {
$link = $result[1];
$link=strip_tags($link);
echo '<item>';
echo '<title>'.$link.'</title>';
echo '<description><![CDATA[';

?>

<?php

function google_blog_feed($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);                      
curl_close($ch);
return $retValue;
}
$sXML = google_blog_feed('http://blogsearch.google.com/blogsearch_feeds?hl=en&q='.$link.'&ie=utf-8&num=10&output=rss');
$oXML = new SimpleXMLElement($sXML); // Error on this line
foreach($oXML->channel->item as $oDocuments){
$title = strip_tags($oDocuments->title);
$url = $oDocuments->link;
$description = $oDocuments->description;
echo $description;
}

?>

<?php

echo ']]></description>';
echo '<category><![CDATA[Google]]></category>';
echo '</item>';
}
}

echo '</channel>';
echo '</rss>';

?>

请帮我解决这个问题...

【问题讨论】:

  • Sooo,你自己试过什么?

标签: php xml rss


【解决方案1】:

尝试改变

$link=strip_tags($link);

$link=urlencode(strip_tags($link));

看起来空间可能会导致错误

【讨论】:

  • 另外,您需要将 google_blog_feed 函数移出 foreach 循环,否则在重新声明时会导致错误 :-)
猜你喜欢
  • 2023-03-26
  • 2014-08-19
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 2016-02-27
  • 1970-01-01
  • 2017-02-18
相关资源
最近更新 更多