Tuesday, 22 May 2018

How to add FCO Travel Advice to your travel website

How to add FCO Travel Advice to your travel website

When it comes to content on the internet, search engines like Google, Bing, Yahoo and Yandex like content that is updated on a regular basis, and what could be better than travel content that updates itself every couple of hours throughout the day, every day of the week, Monday to Friday.

Introduction

FCO Online travel advice

The Foreign and Commonwealth Office (FCO) maintains an official website (https://www.gov.uk/foreign-travel-advice) giving travel advice for British citizens traveling overseas. This is important as if the FCO suggests that only 'essential travel' to a foreign country should be considered a traveler's travel insurance may be invalidated, this is why travelers should always check the latest FCO travel advice before booking a holiday or traveling overseas.

Although you can't use the content of the individual county travel advisory information from the Foreign and Commonwealth Office (FCO) travel advice website on your travel website, the FCO has released their latest travel advice updates as an atom feed for anyone to use. From the FCO online travel advice open data page on data.gov.uk (as shown in the screenshot above):

Foreign & Commonwealth Office (FCO) travel advice for you to re-use in your own applications, widgets and content. It contains current advice for UK travellers to specific countries, detailing topical issues, potential dangers.

It should be noted that the data is in an atom feed and not an Application Programming Interface (API) as suggested by their FCO online travel advice open data page on data.gov.uk.


FCO Online travel advice atom feed

An atom feed (.atom) is similar, though not exactly the same, as an RSS (Really Simple Syndication) feed (.rss), which are both basically XML (eXtensible Markup Language) formatted plain text (.txt) files.

The FCO Online travel advice atom feed looks like this (truncated under the first entry to save space):

<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>https://www.gov.uk/foreign-travel-advice</id>
  <link rel="alternate" type="text/html" href="https://www.gov.uk/foreign-travel-advice"/>
  <link rel="self" type="application/atom+xml" href="https://www.gov.uk/foreign-travel-advice.atom"/>
  <title>Travel Advice Summary</title>
  <updated>2018-05-21T17:06:00+01:00</updated>
  <author>
    <name>GOV.UK</name>
  </author>
  <entry>
    <id>https://www.gov.uk/foreign-travel-advice/burma#2018-05-21T17:06:00+01:00</id>
    <updated>2018-05-21T17:06:00+01:00</updated>
    <link rel="alternate" type="text/html" href="https://www.gov.uk/foreign-travel-advice/burma"/>
    <title>Burma</title>
    <link rel="self" type="application/atom+xml" href="https://www.gov.uk/foreign-travel-advice/burma.atom"/>
    <summary type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>Latest update: Summary, Safety and security section (Political situation) and Local travel (Kachin State) - additional information on various conflicts; on 15 May there were 2 explosions with no casualties on the train track between Mandalay and Myitkyina </p>      </div>
    </summary>
  </entry>....





The FCO Online travel advice atom feed has header information as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>https://www.gov.uk/foreign-travel-advice</id>
  <link rel="alternate" type="text/html" href="https://www.gov.uk/foreign-travel-advice"/>
  <link rel="self" type="application/atom+xml" href="https://www.gov.uk/foreign-travel-advice.atom"/>
  <title>Travel Advice Summary</title>
  <updated>2018-05-21T17:06:00+01:00</updated>
  <author>
    <name>GOV.UK</name>
  </author>

The first two lines of the file are the format of the feed, which shows it is an atom feed and not an rss feed (which would have <rss version="2.0">) as the second line of the file), then the URL of the atom feed is listed under <id> and the following two <link> elements, then we have the title of the atom feed under <title>, then the last time the atom feed was updated under <updated> and then lastly we have the author of the atom feed under <author>. This is all basic information that is included as standard in all atom feeds.

The FCO Online travel advice atom feed has multiple (country) entries like the one below:

  <entry>
    <id>https://www.gov.uk/foreign-travel-advice/burma#2018-05-21T17:06:00+01:00</id>
    <updated>2018-05-21T17:06:00+01:00</updated>
    <link rel="alternate" type="text/html" href="https://www.gov.uk/foreign-travel-advice/burma"/>
    <title>Burma</title>
    <link rel="self" type="application/atom+xml" href="https://www.gov.uk/foreign-travel-advice/burma.atom"/>
    <summary type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>Latest update: Summary, Safety and security section (Political situation) and Local travel (Kachin State) - additional information on various conflicts; on 15 May there were 2 explosions with no casualties on the train track between Mandalay and Myitkyina </p>      </div>
    </summary>
  </entry>

The entry lists a link to the information it displays under <id>, the last time it was updated under <updated>, a link to the main country page on the FCO website under <link>, the country name under the <title>, another link under <link> to an atom feed of the country information on the FCO website and then a short text description of the update under <summary> -> <div> -> <p>.

What we have to do is take this plain text file, read it into an array and then format the contents when we display it on a webpage.


How to add an atom feed to your website using PHP

Below is some very simple PHP code for reading in the atom feed into an a SimpleXMLElement object from the URL using PHP's built-in simplexml_load_file function and doing some basic formatting of it with some HTML headers and paragraphs when we display it on the webpage. 

<?php

  $atom = simplexml_load_file('https://www.gov.uk/foreign-travel-advice.atom');

  echo '<h1><a href="'. $atom->link[href] .'">' . $atom->title . '</a></h1>';

  $dmy = date('g:ia jS F Y', strtotime('+1 hour', strtotime($atom->updated)));
  echo '<p>Updated: <b>' . $dmy . '</b></p><br>';

  foreach ($atom->entry as $item) {
   echo '<h2><a href="'. $item->link[href] .'"> ' . $item->title . '</a></h2>';
   echo '<p>' . str_replace(array("Latest update: ", ""), "", $item->summary->div->p) . '</p>';
   $dmy2 = date('g:ia jS F Y', strtotime('+1 hour', strtotime($item->updated)));
   echo '<small>' . $dmy2 . '</small>';
   }

?>

We start by creating a variable $atom to hold our SimpleXMLElement object which we will use to output our keys( <title>)and values (Travel Advice Summary).

We output the values using the following format:

$atom is the root of the file, followed by the value we want, so $atom->title gives us the title of the atom feed. The following information lists what each line of the PHP code does.

We echo our header <h1> with a link to the main FCO travel advice website using $atom->link[href] and the title of the country by using $atom->title.

We then create a variable $dmy to hold the time and date of the last updated time and date of the atom feed which we then correct to British Summer Time by adding an hour to the time and date listed in the atom feed. We then echo the result to the webpage inside some HTML paragraph <p> tags.

We then create a foreach loop to iterate through the country entries as an $item and then echo out the country entry as an HTML header <h2> with a link to the FCO country webpage and the country name as the link text. Then we echo out the summary inside HTML paragraph <p> tags after removing the words 'Latest update: ' from the summary just to make it less repetitive on each country entry. We then echo out another corrected time and date, this time for the country entry using $dmy2 inside a pair of HTML <small> tags.


See a working example


See a working example on TripEdge at FCO Travel Advice 2018 our example has some extra formatting, but you can see that once you have the data displayed on the webpage you can format it any way you like, you could even add it to a sidebar on your travel insurance page for your website visitors, the possibilities are endless.




No comments:

Post a Comment