2012-04-26 37 views
2

不可否认,我是新手,只知道危险。如何使用CURL而不是file_get_contents重写此RSS阅读器?

由于安全考虑,主机服务器不允许file_get_contents。我如何使用curl而不是file_get_contents来重写下面的代码?

这是一个jQuery手机网站上的RSS阅读器。该代码是基于一个教程,我发现在这里:nets.tutplus.com/rssreader

<?php 
$siteName = empty($_GET['siteName']) ? 'Website' : $_GET['siteName']; 
$siteList = array(
'Website2', 
'Website3', 
'Website4', 
'Website5', 
'Website6', 
); 
// For security. 
if (!in_array($siteName, $siteList)) { 
$siteName = 'Website'; 
} 
$location = "http://query.yahooapis.com/v1/public/yql?q="; 
$location .= urlencode("SELECT * FROM feed where url='http://feeds.feedburner.com/$siteName'"); 
$location .= "&format=json"; 
$rss = file_get_contents($location, true); 
$rss = json_decode($rss); 
?> 

编辑:将这项工作?

$location = "http://query.yahooapis.com/v1/public/yql?q="; 
$location .= urlencode("SELECT * FROM feed where url='http://feeds.feedburner.com/$siteName'"); 
$location .= "&format=json"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $location); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, trim($request)); 
$rss = curl_exec($ch); 
curl_close($ch); 

$rss = json_decode($rss); 
+0

如果您的托管服务提供商不允许file_get_contents,他们肯定不会允许PHP cURL(它需要与PHP分开安装)。 – honyovk 2012-04-26 18:08:47

+0

谢谢你的抬头。 – James 2012-04-26 18:23:20

+0

您可能只需打开一个php.ini文件就可以打开file_get_contents(搜索您的提供商的详细信息) – 2012-04-26 18:28:55

回答

1

您可以完全删除CURLOPT_POST...行。 $request未设置,并且您不需要在此示例中发出发送请求来执行GET请求。您可能需要也可能不需要使用CURLOPT_FOLLOWLOCATION - 如果卷曲尚未根据需要运行,请将其打开。

+0

感谢您的帮助。我相信,现在我已经掌握了所有上面的代码。我是否也应该取出CURLOPT_POSTFIELDS? – James 2012-04-26 18:42:05

+0

如果不需要,你可以。 'file_get_contents'没有发布任何内容。 – 2012-04-26 18:45:08

+0

我没有信誉upvote,但你绝对帮助!只是一个fyi:我试图拿出file_get_contents,并没有奏效。我能够取出CURLOPT_FOLLOWLOCATION – James 2012-04-26 18:56:50