问题
So i am having a killer issue. I can't get this xml data to show. It was working 3 months ago and now it's not. Any suggestions to fix this bugger would help me out a ton!
Yes - I have checked to see if the link is working, and live and it is.
<?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 } ?>
回答1:
I have noticed that hosts are becoming more restrictive in file_get_contents calls lately. Calls that were working a few months ago are not working now.
The best solution is to make your calls through curl, which is not too bad once you get it set up.
Sample code:
$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;
}
$htmlFile will have the contents of the file when the call to curl()
returns.
You can get the absolute hosting path of your server from the hosting account control panel. The user agent is not too important, especially if it is your server.
The tricky part is the cert file. You can get 'standard' ca certs from: http://curl.haxx.se/docs/caextract.html
If you are calling to your own server, you should be able to create your own self-signed cert: http://www.tldp.org/HOWTO/SSL-Certificates-HOWTO/x160.html
Copy the cert to your /certs directory and name according to the call in CURLOPT_CAINFO (see code above).
You can also change CURLOPT_SSL_VERIFYPEER to FALSE and not use a cert, but that will turn off verification altogether, which is not considered safe (use at own risk) (see http://php.net/manual/en/function.curl-setopt.php for more info).
Hope that helps!
来源:https://stackoverflow.com/questions/16576875/wordpress-simplexml-load-file-parse-error