items);
$output = "
"; //oh no, styles are hardcoded!
//limit the list to 10
if($size > 10)
$size = 10;
//Show data from the feed
for($i = 0;$i<$size;$i++) {
$item = $rss->items[$i];
$href = $item['link'];
$eventid = substr($href,26,strlen($href)-27);
$title = $item['title'];
$category = $item['category'];
$date = date("F j",strtotime(substr($item['dc']['date'],0,10)));
$output .= "- " . htmlspecialchars(utf8_encode($title)) . " " . htmlspecialchars(utf8_encode($date)) . "
";
//output tags
$tags = getEventTags($eventid);
$output .= "- ";
foreach($tags as $tag)
$output .= "" . $tag . " ";
$output .= "
";
}
$output .= "
";
//show output
print($output);
//or cache, eg. with cronscript, don't pull events more often than every 15 minutes or so!
//write the file
$filename = 'cache/upcoming.html';
if (sizeof($rss->items) > 0 ) {
$handle = fopen($filename, 'w');
if (fwrite($handle, $output) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
//echo "Success, wrote ($output) to file ($filename)";
fclose($handle);
}
/*********** Get the tags out via upcoming web services **************/
//quick'n'dirty parsing, no fancy DOM parsers required
class XMLParser {
var $stack = array();
function XMLParser() {
$this->parser = xml_parser_create();
}
function parse($data) {
$this->stack = array(); // this keeps track of what tag level you're at
xml_set_object($this->parser, &$this);
xml_set_element_handler($this->parser, "_startElementHandler", "_endElementHandler");
xml_set_character_data_handler($this->parser, "_cdata");
$foo = xml_parse($this->parser,$data);
if(!$foo) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser)));
}
xml_parser_free($this->parser);
return $this->stack;
}
function _startElementHandler($parser, $name, $attribs) {
$tag=array("name"=>$name,"attrs"=>$attribs);
array_push($this->stack,$tag);
}
function _endElementHandler($parser, $name) {
$this->stack[count($this->stack)-2]['children'][] = $this->stack[count($this->stack)-1];
array_pop($this->stack);
}
function _cdata($parser, $cdata) {
if(trim($cdata)) {
$this->stack[count($this->stack)-1]['cdata']=$cdata;
}
}
}
//parses the response from curl
function parse_response($response) {
/*
***original code extracted from examples at
***http://www.webreference.com/programming
/php/cookbook/chap11/1/3.html
***returns an array in the following format which varies depending on headers returned
[0] => the HTTP error or response code such as 404
[1] => Array
(
[Server] => Microsoft-IIS/5.0
[Date] => Wed, 28 Apr 2004 23:29:20 GMT
[X-Powered-By] => ASP.NET
[Connection] => close
[Set-Cookie] => COOKIESTUFF
[Expires] => Thu, 01 Dec 1994 16:00:00 GMT
[Content-Type] => text/html
[Content-Length] => 4040
)
[2] => Response body (string)
*/
list($response_headers,$response_body) = explode("\r\n\r\n",$response,2);
$response_header_lines = explode("\r\n",$response_headers);
// first line of headers is the HTTP response cod
$http_response_line = array_shift($response_header_lines);
if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line,$matches)){
$response_code = $matches[1];
}
// put the rest of the headers in an array
$response_header_array = array();
foreach ($response_header_lines as $header_line) {
list($header,$value) = explode(': ',$header_line,2);
$response_header_array[$header] = $value;
}
return array($response_code,$response_header_array,$response_body);
}
//returns a binary
function curl_get ($url,$user_agent,$binary) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_PROXY, $proxy);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
if($binary)
curl_setopt ($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}
//wrapper for curl_get
function curl_get_new($url_page,$user_agent,$binary) {
$curl_response = curl_get($url_page,$user_agent,$binary);
$resp = parse_response($curl_response);
return $resp;
}
/*get the tags out
* @return $tags a list of tags
*/
function getEventTags($eventid) {
$user_agent = "Mozilla/5.0";
$url = "http://upcoming.org/services/rest/?api_key=***&method=event.getInfo&event_id=" . $eventid; //replace *** with your api key
$data = curl_get_new($url,$user_agent,false);
$x = new XMLParser();
$xmldata = $x->parse($data[2]);
$tagsString = $xmldata[0]['children'][0]['attrs']['TAGS'];
$tags = split(",",$tagsString);
return $tags;
}
?>