所以我遇到了一个致命的问题。我无法显示此xml数据。3个月前它还能用,但现在不行了。任何解决这个问题的建议都会对我有很大帮助!
是的-我已经检查了链接是否正常工作,并且是实时的。
<?php
if(file_exists('http://blog.millcitychurch.org/blog/rss.xml')){ ?>
<h1>// THE LATEST FROM MILL city</h1>
<div class="feed">
<?php
$xml = file_get_contents('http://blog.millcitychurch.org/blog/rss.xml');
$url = 'http://blog.millcitychurch.org/blog/rss.xml';
$rss = simplexml_load_file($xml);
if($rss) {
$items = $rss->channel->item;
$i = 0;
foreach($items as $item) {
if (++$i > 4) {
// stop after 5 loops
break;
}
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$description = strip_tags($item->description);
$position=215; // Define how many character you want to display.
$message = $description;
$post_content = substr($message, $position, 1);
$post_content = substr($message,0,$position); // Display your message
echo '<div class="feed-desc">' ;
echo '<h2>'.$title.'</h2><p>';
echo $post_content;
echo '</p><div class="readmore"><a href="'.$link.'">... read more</a></div>';
echo '<div class="date">'.$published_on.'</div>';
echo '</div>';
}
} ?>
</div><! -- end .feed -->
<?php } ?>
发布于 2013-05-16 08:42:35
我注意到最近主机对file_get_contents调用的限制越来越多。几个月前还能用的呼叫现在不能用了。
最好的解决方案是通过curl拨打电话,一旦设置好,这并不是很糟糕。
示例代码:
$htmlFile = curl('http://blog.millcitychurch.org/blog/rss.xml');
function curl($url)
{
//Absolute Hosting Path
$absHostingPath = '/home/content/your_server_path/html';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_USERAGENT, array('User-Agent' => 'My User Agent'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_CAINFO, $absHostingPath."/certs/cacert.pem");
$data = curl_exec($ch);
$curlError = curl_error($ch);
if(strlen($curlError) > 0)
print(' Curl error: ' .$curlError.' ');
curl_close($ch);
return $data;
}
当对curl()
的调用返回时,$htmlFile将拥有该文件的内容。
您可以从托管帐户控制面板中获取服务器的绝对托管路径。用户代理并不太重要,特别是当它是您的服务器时。
棘手的部分是cert文件。你可以从以下网址获得“标准”证书:http://curl.haxx.se/docs/caextract.html
如果您正在调用您自己的服务器,您应该能够创建您自己的自签名证书:http://www.tldp.org/HOWTO/SSL-Certificates-HOWTO/x160.html
根据CURLOPT_CAINFO中的调用将证书复制到您的/certs目录和名称(参见上面的代码)。
您也可以将CURLOPT_SSL_VERIFYPEER更改为FALSE并不使用证书,但这将完全关闭验证,这不被认为是安全的(使用风险自负)(有关更多信息,请参阅http://php.net/manual/en/function.curl-setopt.php )。
希望这能有所帮助!
https://stackoverflow.com/questions/16576875
复制相似问题