<?php
/**************************************************************************
Title: Moreover PHP API
Author: Michael Bell (mikeb@map.com)
Based upon news_parser written by Carter Comunale (carter@brasscity.com)
Version: 1.0
Date: July 10th, 2001
Description: This collection of classes are my attempt to create a complete
API in PHP to the Moreover.com content syndication service. It
has the ability to retrieve a listing of all current content
channels and categories as well as retrieve all articles for
those categories.
Classes: Moreover - Main API class
moreoverChannel - Channel class
moreoverCategory - Category class
moreoverArticle - Article class
**************************************************************************/
class moreoverArticle {
var $ArticleID;
var $URL;
var $HeadlineText;
var $Source;
var $MediaType;
var $Cluster;
var $Tagline;
var $DocumentURL;
var $HarvestTime;
var $AccessRegistration;
var $AccessStatus;
function moreoverArticle () {
// Constructor - Nothing needs to be initilized in this class.
}
}
class moreoverCategory {
var $CategoryName;
var $FeedName;
var $ArticleObjects;
function moreover_category () {
// Constructor - Just empty out the ArticleObjects array
$this->ArticleObjects = array ();
}
}
class moreoverChannel {
var $ChannelName;
var $CategoryObjects;
function moreoverChannel () {
// Constructor - Just empty out the CategoryObjects array
$this->CategoryObjects = array ();
}
}
class Moreover {
var $getArticleURL;
var $getChannelURL;
var $getSearchURL;
var $ArticleObjects;
var $ChannelObjects;
var $_XMLParser;
var $_CurrentTag;
var $_CurrentArticle;
var $_CurrentChannel;
var $_CurrentCategory;
function Moreover () {
// Constructor
// Empty out ArticleObjects array
$this->ArticleObjects = array ();
// Empty out ChannelObjects array
$this->ChannelObjects = array ();
// Setup the proper URL strings
$this->getArticleURL = "http://www.moreover.com/cgi-local/page?o=xml&c=";
$this->getSearchURL = "http://p.moreover.com/cgi-local/page?o=xml&k=";
$this->getChannelURL = "http://w.moreover.com/categories/nested_category_list.xml";
}
function getArticles () {
$this->_clearArrays ();
foreach (func_get_args () as $argument) {
if (is_array ($argument)) {
foreach ($argument as $arg) {
$this->_interactAndParse ($this->getArticleURL . urlencode ($arg));
}
}
else {
$this->_interactAndParse ($this->getArticleURL . urlencode ($argument));
}
}
return $this->ArticleObjects;
}
function getChannel ($channel) {
$output = array ();
$this->getChannels ();
foreach ($this->ChannelObjects as $channelObj) {
if ($channelObj->ChannelName == $channel) {
$output = $channelObj->CategoryObjects;
break;
}
}
return $output;
}
function getChannels () {
$this->_clearArrays ();
$this->_interactAndParse ($this->getChannelURL);
return $this->ChannelObjects;
}
function searchArticles () {
$this->_clearArrays ();
foreach (func_get_args () as $argument) {
if (is_array ($argument)) {
foreach ($argument as $arg) {
$this->_interactAndParse ($this->getSearchURL . urlencode ($arg));
}
}
else {
$this->_interactAndParse ($this->getSearchURL . urlencode ($argument));
}
}
return $this->ArticleObjects;
}
function _clearArrays () {
$this->ArticleObjects = array ();
$this->ChannelObjects = array ();
}
function _interactAndParse ($XMLFile) {
// Initilize the XML parser
$this->_XMLParser = xml_parser_create ('UTF-8');
xml_parser_set_option ($this->_XMLParser, XML_OPTION_CASE_FOLDING, TRUE);
xml_parser_set_option ($this->_XMLParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_set_element_handler ($this->_XMLParser, "_tagOpen", "_tagClose");
xml_set_character_data_handler ($this->_XMLParser, "_cdata");
xml_set_object ($this->_XMLParser, &$this);
if (!($fp = fopen ($XMLFile, 'r'))) {
echo "Could not open $XMLFile for parsing!\n";
}
while ($data = fread ($fp, 4096)) {
if (!($data = utf8_encode ($data))) {
echo 'ERROR'."\n";
}
if (!xml_parse ($this->_XMLParser, $data, feof ($fp))) {
die (sprintf ( "XML error: %s at line %d\n\n",
xml_error_string (xml_get_error_code ($this->_XMLParser)),
xml_get_current_line_number ($this->_XMLParser)));
}
}
fclose ($fp);
$this->_cleanUp ();
}
function _tagOpen ($parser, $tag, $attributes) {
$this->_CurrentTag = $tag;
switch ($tag) {
case "ARTICLE": $this->_CurrentArticle = new moreoverArticle ();
break;
case "CATEGORY": $this->_CurrentCategory = new moreoverCategory();
break;
case "CHANNEL": $this->_CurrentChannel = new moreoverChannel();
break;
}
}
function _tagClose ($parser, $tag) {
switch ($tag) {
case "ARTICLE": array_push($this->ArticleObjects, $this->_CurrentArticle);
break;
case "CATEGORY": array_push($this->_CurrentChannel->CategoryObjects, $this->_CurrentCategory);
break;
case "CHANNEL": array_push($this->ChannelObjects, $this->_CurrentChannel);
break;
}
}
function _cdata($parser, $cdata) {
switch ($this->_CurrentTag) {
case "URL":
if (!$this->_CurrentArticle->URL) {
$this->_CurrentArticle->URL = $cdata;
}
break;
case "HEADLINE_TEXT":
if (!$this->_CurrentArticle->HeadlineText) {
$this->_CurrentArticle->HeadlineText = $cdata;
}
break;
case "SOURCE":
if (!$this->_CurrentArticle->Source) {
$this->_CurrentArticle->Source = $cdata;
}
break;
case "MEDIA_TYPE":
if (!$this->_CurrentArticle->MediaType) {
$this->_CurrentArticle->MediaType = $cdata;
}
break;
case "CLUSTER":
if (!$this->_CurrentArticle->Cluster) {
$this->_CurrentArticle->Cluster = $cdata;
}
break;
case "TAGLINE":
if (!$this->_CurrentArticle->Tagline) {
$this->_CurrentArticle->Tagline = $cdata;
}
break;
case "DOCUMENT_URL":
if (!$this->_CurrentArticle->DocumentURL) {
$this->_CurrentArticle->DocumentURL = $cdata;
}
break;
case "HARVEST_TIME":
if (!$this->_CurrentArticle->HarvestTime) {
$this->_CurrentArticle->HarvestTime = $cdata;
}
break;
case "ACCESS_REGISTRATION":
if (!$this->_CurrentArticle->AccessRegistration) {
$this->_CurrentArticle->AccessRegistration = $cdata;
}
break;
case "ACCESS_STATUS":
if (!$this->_CurrentArticle->AccessStatus) {
$this->_CurrentArticle->AccessStatus = $cdata;
}
break;
case "CHANNEL_NAME":
if (!$this->_CurrentChannel->ChannelName) {
$this->_CurrentChannel->ChannelName = $cdata;
}
break;
case "CATEGORY_NAME":
if (!$this->_CurrentCategory->CategoryName) {
$this->_CurrentCategory->CategoryName = $cdata;
}
break;
case "FEED_NAME":
if (!$this->_CurrentCategory->FeedName) {
$this->_CurrentCategory->FeedName = $cdata;
}
break;
}
}
function _cleanUp () {
xml_parser_free ($this->_XMLParser);
}
}
?>
Example use...<PRE>
<?php
include ("moreover_api.php");
$mo_api = new Moreover ();
// Retrieve headlines in the "Broadcasting industry news" category.
print_r ($mo_api->getArticles ("Broadcasting industry news"));
// Retrieve headlines with the keyword "Linux".
print_r ($mo_api->searchArticles ("Linux"));
// Retrieve headlines with the keyword "Linux" then retrieve headlines with
// "MP3" and combine them into one responce.
print_r ($mo_api->searchArticles ("Linux", "MP3"));
// Retrieve headlines with the keyword "Linux" then retrieve headlines with
// "MP3" and combine them into one responce. (Same as above, different call)
print_r ($mo_api->searchArticles (array ("Linux", "MP3")));
// Rerieve all channels and categories that Moreover.com has available.
print_r ($mo_api->getChannels ());
// Rerieve all categories within the specified channel.
print_r ($mo_api->getChannel ("Business: general"));
?>
</PRE> |