Gathering Event Data from UKC Website

As a secondary feature on the main map page I wanted to add an event feed showing the latest events from the University of Kent. To do this I had to scrape the events page on the university website for the data I wanted and display it nicely styled on the map page. Originally I wanted to have access to the Kent database but I was not allowed to do this for security reasons.

The PHP function below loads in the HTML page from the university website and stores it as a document. This document can then be traversed using Xpath just like an XML document.

 

$html = file_get_contents('http://www.kent.ac.uk/calendar/'); //get the html returned from the following url

$kent_doc = new DOMDocument();

libxml_use_internal_errors(TRUE);

if(!empty($html)){ //if any html is actually returned

  $kent_doc->loadHTML($html);
  libxml_clear_errors(); //remove errors for yucky html

  $kent_xpath = new DOMXPath($kent_doc);

  $event = $kent_xpath->query('//div[contains(concat(" ", normalize-space(@class), " "), " event ")]');

I then loop through the returned data and add it into HTML containers. I then output the styled result to the map page wherever it is needed.

if($event->length > 0){
      foreach($event as $row){
		  $titlepath = $kent_xpath->query('.//div[@class="title"]', $row);
		  $title = $titlepath->item(0)->nodeValue;
		  $timepath = $kent_xpath->query('.//div[@class="time"]', $row);
		  $time = $timepath->item(0)->nodeValue;
		  $linkpath = $kent_xpath->query('.//div[@class="title"]/a/@href', $row);
		  $link = $linkpath->item(0)->nodeValue;
		  $linktitlepath = $kent_xpath->query('.//div[@class="categories"]/a', $row);
		  $linktitle = $linktitlepath->item(0)->nodeValue;

I have also added a check to stop the HTML displaying if there was an error retrieving the data from the university website. This is to stop errors showing if the way the data is displayed or the URL of the webpage is changed at the university’s end.

Be the first to leave a comment. Don’t be shy.

Join the Discussion

Thank you for taking the time to look over my most recent work. Please feel free to contact me for any further information, to hire me for a project or to simply have a good old chat.

Click here to hire me