Last active
August 29, 2015 14:01
-
-
Save openviewpartners/f596c040b489d1177ff4 to your computer and use it in GitHub Desktop.
PHP Class for parsing XML from the Reuters API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Parse Investor XML Feeds | |
* | |
* XML feeds pulled in from Reuters, parsed, | |
* cached and displayed on homepage. | |
* | |
* @param int $compid The account identifier for access to the XML API | |
* @param bool $debug Sets a debug mode for faster testing | |
*/ | |
if ( !class_exists( 'Reuters' ) ): | |
class Reuters { | |
static $compid = XXXXXX; | |
static $debug = false; | |
/** | |
* HTTP Request to API | |
* | |
* @param string $feed The URL for a given Reuters XML feed | |
*/ | |
static function http_request( $feed = null ) { | |
// Validate feed | |
if ( ! $feed ) | |
return new WP_Error( 'ReutersFeeds_error', __( 'No feed provided.' ) ); | |
// HTTP request | |
// | |
// Note: Reuters does not send proper error | |
// response codes with their XML | |
$response = wp_remote_get( $feed, array( | |
'compress' => false | |
) ); | |
$body = wp_remote_retrieve_body( $response ); | |
// Convert XML to JSON | |
$xml = new SimpleXMLElement( $body, LIBXML_NOCDATA ); | |
$json = json_encode( $xml ); | |
$obj = json_decode( $json ); | |
// Error handling | |
if ( isset($obj->Errors) ) | |
return new WP_Error( 'xml_api_error', __( $obj->Errors->Error ), $feed ); | |
return $obj; | |
} | |
/** | |
* Fetch an HTTP response | |
* | |
* @param string $type Determine the type of output this is, currently this can be 'events' or 'news' | |
* @param int $size How many items to return | |
*/ | |
static function generate_output( $type = null, $size = 4 ) { | |
// Output for NEWS type | |
if ( $type == 'news' ) { | |
// Make HTTP request | |
$response = self::http_request( 'http://xml.corporate-ir.net/irxmlclient.asp?compid=' . self::$compid . '&reqtype=newsreleases_2' ); | |
if ( is_wp_error($response) ) | |
return $response->get_error_message(); | |
$entries = $response->NewsReleases->NewsRelease; | |
} | |
// Output for EVENTS type | |
elseif ( $type == 'events' ) { | |
// Make HTTP request | |
$response = self::http_request( 'http://xml.corporate-ir.net/irxmlclient.asp?compid=' . self::$compid . '&reqtype=events2' ); | |
if ( is_wp_error($response) ) | |
return $response->get_error_message(); | |
$entries = $response->Events->Event; | |
$entries = array_reverse( $entries ); | |
} | |
// No type available | |
else { | |
return new WP_Error( 'ReutersFeeds_error', __( 'Non-existent or invalid $type provided.' ) ); | |
} | |
// Validate | |
if ( !$entries ) | |
return new WP_Error( 'ReutersFeeds_error', __( 'Non-existent or invalid $entries provided.' ) ); | |
// Format output | |
$i = 0; | |
$output = ''; | |
foreach ( $entries as $entry ) { | |
$i++; | |
// Format entry | |
$entry_func = "{$type}_entry"; | |
$output .= self::$entry_func( $entry ); | |
// Limit feed size | |
if ( $i >= $size ) | |
break; | |
} | |
// Output | |
return $output; | |
} | |
/** | |
* News display | |
* | |
* @param string $type Determine the type of output this is, currently this can be 'events' or 'news' | |
*/ | |
static function display( $type = null ) { | |
// Check required | |
if ( ! $type ) | |
return false; | |
// Generate cache if it doesn't exist | |
$label = __CLASS__ . "_{$type}_output"; | |
if ( false === ( $output = get_transient( $label ) ) || self::$debug ) { | |
$output = Reuters::generate_output( $type, 2 ); | |
set_transient( $label, $output, HOUR_IN_SECONDS ); | |
} | |
// No content | |
if ( empty($output) || is_wp_error( $output ) ) | |
$output = "<article><p>There are currently no upcoming $type scheduled.</p></article>"; | |
if ( !is_wp_error( $output ) ) | |
echo $output; | |
} | |
/** | |
* Format news entry | |
* | |
* @param array $entry Data for a given entry | |
*/ | |
static function news_entry( $entry = null ) { | |
if ( !$entry ) | |
return; | |
// Data | |
$entry = (array) $entry; | |
$date = explode( ' ', $entry['Date'] ); | |
$date = str_replace( '/', '.', $date[0] ); | |
$title = apply_filters( 'the_title', $entry['Title'] ); | |
$id = $entry['@attributes']->ReleaseID; | |
$link = "http://investors.keryx.com/phoenix.zhtml?c=" . self::$compid . "&p=irol-newsArticle&id={$id}"; | |
// HTML | |
$output = "<article>"; | |
$output .= "<p><time itemprop=\"date\">$date</time><a href=\"$link\" class=\"truncate\" data-length=\"155\">$title Keryx Biopharmaceuticals Announces Zerenex™ (Ferric Citrate Coordination Complex) Phase 2 Results in Non-Dialysis Dependent Chronic Kidney Disease Selected as a Late Breaking Oral Presentation at the Upcoming National Kidney Foundation</a></p>"; | |
$output .= "</article>"; | |
return $output; | |
} | |
/** | |
* Format event entry | |
* | |
* @param array $entry Data for a given entry | |
*/ | |
static function events_entry( $entry = null ) { | |
if ( !$entry ) | |
return; | |
// Data | |
$entry = (array) $entry; | |
$strtotime = strtotime( $entry['@attributes']->EventStartDate ); | |
$date = date( 'n.j.Y', $strtotime ); | |
$title = apply_filters( 'the_title', $entry['EventTitle'] ); | |
$id = $entry['@attributes']->EventID; | |
$link = "http://investors.keryx.com/phoenix.zhtml?c=" . self::$compid . "&p=irol-EventDetails&EventId={$id}"; | |
// Only show future events | |
$today = strtotime( "00:00:00" ); | |
$output = ''; | |
if ( $strtotime >= $today ) { | |
$output .= "<article>"; | |
$output .= "<p><time itemprop=\"date\">$date</time><a href=\"$link\">$title</a></p>"; | |
$output .= "</article>"; | |
} | |
return $output; | |
} | |
} | |
endif; // end class_exists |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment