根据这个建议: Rss Reader in PHP
我已经改变了我的bridge.php
<?php
header ( 'Access-Control-Allow-Origin: *' );
$tmpFile = 'out.txt';
$val = "http://rss.news.yahoo.com/rss/topstories";
$curlHandle = curl_init ( $val );
$filePointer = fopen ( $tmpFile, "w" );
curl_setopt ( $curlHandle, CURLOPT_FILE, $filePointer );
curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2');
curl_setopt($curlHandle, CURLOPT_ENCODING , "gzip");
curl_setopt($curlHandle, CURLOPT_TIMEOUT,5);
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_exec ( $curlHandle );
curl_close ( $curlHandle );
fclose ( $filePointer );
$linesArr = file ( $tmpFile );
foreach ( $linesArr as $eachLine ) {
echo ($eachLine);
}
?>
“out.txt”成功创建:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>Yahoo News - Top Stories</title>
<link>http://news.yahoo.com/topstories/</link>
<description/>
<language>en-US</language>
<copyright>Copyright (c) 2014 Yahoo! Inc. All rights reserved</copyright>
<pubDate>Wed, 07 May 2014 22:54:13 -0400</pubDate>
<ttl>5</ttl>
<image>
<title>Yahoo News - Top Stories</title>
<link>http://news.yahoo.com/topstories/</link>
...
</rss>
<!-- fe927.global.media.sg3.yahoo.com compressed/chunked Thu May 8 03:00:48 UTC 2014 -->
由于帖子字数限制,我省略了rss Feed的内容正文。无论如何,“bridge.php”似乎运作良好。
但在我的JQuery脚本中,我无法成功捕获返回的xml内容。
$.ajax({type: "GET", dataType: "xml", url: 'bridge.php', success: handler, error: error_handler});
return false;
}
function error_handler(xhr, status, error) {
alert("error");
}
此.ajax()调用中的成功和错误处理程序都不能执行。为了调试我把.ajax(dataType:"text")
放在data.php中,并将一些调试代码放在data.php中,它可以捕获从PHP到JQuery的正确信息。
感谢您的任何建议。