Skip to content

Instantly share code, notes, and snippets.

@deepak-rajpal
Created September 3, 2015 13:48
Show Gist options
  • Save deepak-rajpal/550913addd3411e73165 to your computer and use it in GitHub Desktop.
Save deepak-rajpal/550913addd3411e73165 to your computer and use it in GitHub Desktop.
Fetch XML Feeds using cURL and SimpleXML - Best and Tested
<?php
/* Fetch XML feeds using PHP SimpleXML and cURL */
/* Step 1) Parse xml url in curl and assign all data into a variable
Step 2) Pass Xml data into SimpleXML and access elements using returned object */
/* =================The SimpleXML =========================
The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.
This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default. The SimpleXML extension requires PHP 5.
Reference: http://php.net/manual/en/book.simplexml.php
========================================================== */
/* Function to return all external xml data as per given url - Used for market headlines and video feeds. */
function curl($feed_url){
$ch = curl_init();
/* Note: Shortcode issue when passing url using editor. (But fine in widgets)
In $feed_url there is ampersand (&) which is converted automatically into html entities by WordPress editor. So htmlspecialchars_decode or str_replace the '&amp;' into '&'. */
// $feed_url = str_replace("&amp;","&", $feed_url);
$feed_url = htmlspecialchars_decode($feed_url);
curl_setopt($ch, CURLOPT_URL,$feed_url);
curl_setopt($ch, CURLOPT_PROXY, WP_PROXY_HOST); // your proxy url
curl_setopt($ch, CURLOPT_PROXYPORT, WP_PROXY_PORT); // your proxy port number
// curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); // If displaying old result because of cache
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$curl_data = curl_exec($ch);
curl_close($ch);
return $curl_data;
}
/* ========= Starts: Shortcode to display "Market Headlines" Feeds Box (Example: Homepage Sidebar) ========== */
/* Shortcode Example: [market_headlines_box feed_url="http://headlinesite.com/headlines.xml"] */
function market_headlines_fx( $atts ) {
extract( shortcode_atts( array(
'feed_url' => '',
'title' => 'Market Headlines',
'link' => '#',
), $atts ) );
ob_start();
// Check if feed url is not set, and do not go further.
If ( empty($feed_url) ) {
echo "<strong > Please provide a feed url in shortcode such as: </strong>";
return;
} ;
?>
<div class="news" id="centernews">
<div class="topcenter newstopcenter">
<h2><a href="<?php echo $link; ?>" class="linkblank"><?php echo $title; ?></a></h2>
</div>
<?php
// Enables libxml errors so that fetching and displaying later.
libxml_use_internal_errors(true);
// Fetch all external xml assign to variable, so that later parse using SimpleXML
$xml_data = curl($feed_url);
// If you have xml data in another page, use simplexml_load_file(), and if you have in variable use SimpleXMLElement()
// $xml = simplexml_load_file($feed_url);
$xml = new SimpleXMLElement($xml_data);
if ($xml) {
foreach ($xml->children() as $second_gen) {
foreach ($second_gen->children() as $third_gen)
{
$headline_title = $third_gen->title;
$headline_date = $third_gen->date;
$headline_time = $third_gen->time;
$headline_teaser = $third_gen->teaser;
$headline_documentID = $third_gen->documentID;
$headline_link = $third_gen->link;
?>
<div class="headlinecontent">
<h3><a href="<?php echo $headline_link; ?>" target="_blank"><?php echo $headline_title; ?></a></h3>
<p class="date"><?php echo $headline_date.' &#8226; '.$headline_time; ?></p><p><?php echo $headline_teaser; ?>
</p>
</div>
<?php } } ?>
<div class="footer">
<a href="<?php echo $link; ?>">
<div class="morethin">More</div>
<div class="moreimg"><img border="0" src="<?php echo get_template_directory_uri(); ?>/images/more.png"></div>
</a>
</div>
</div>
<?php
}
else {
echo "<div class='headlinecontent'> <strong > There are following error fetching xml/feed: </strong> <br /><br />";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
echo "</div><div class='footer'></div>";
libxml_use_internal_errors(false);
}
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}
add_shortcode( 'market_headlines_box', 'market_headlines_fx' );
/* Ends: Shortcode to display "Market Headlines" Feeds */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment