【发布时间】:2017-06-02 06:03:59
【问题描述】:
由于 Google 的提要加载服务已关闭,我一直在努力寻找一种新方法来显示跨不同协议和域的 xml 提要。我需要将博客内容添加到网站上。网站是https,博客不是。我在这里取得了一些成功,,但只是获得了称号。我需要将标题包装在链接中,然后将其插入到 li 中。 可以正常工作,需要将其放入 DOM
这是我的代理路径是(/SSI/Processor/feedProxy.php):
<?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('https://www.external.com/feeds/feed.xml');
$oXML = new SimpleXMLElement($sXML);
$items = $oXML->entry;
$i = 0;
foreach($items as $item) {
$title = $item->title;
$link = $item->link;
echo '<li>';
foreach($link as $links) {
$loc = $links['href'];
echo "<a href=\"$loc\">";
}
echo $title;
echo "</a>";;
echo "</li>";
if(++$i == 3) break;
}
?>
编辑
我现在可以获取所有返回的 xml,并通过 PHP 进行格式化。我就是不能用 jQuery 把它插入到 DOM 中!
还有我的 jQuery:
$(function(){
$.ajax({
type: "GET",
url:'/SSI/Processor/feedProxy.php',
dataType: "html",
success: function(html) {
console.log(html);
//console log is showing the formatted HTML
$('#feeds').html(html);
});
});
我正在努力解决如何在 PHP 中解析它。它需要像这样输出:<li><a href="linkfromxml">link text</a></li> 并将其附加到 div。
【问题讨论】:
-
对不起,由于格式不一致,我看错了
-
不知道为什么你在真正发送 html 时会在 ajax 中询问表单 xml
-
我为格式化道歉。我已编辑问题以询问 html
-
但是你的dataType仍然是xml。
-
修复了它。我希望有比使用 PHP cURL 更简单的方法,我无法让 YQL 版本工作。解析 JSON 要简单得多。