Get XML Feed (getSimpleXML) – Cordial Knowledge Base

How can we help?

Overview

The data stored within an externally hosted XML feed can be referenced and used within any message body.

Our example

For our example, we'll be using external XML data consisting of a list of classic albums.

Using the tabs below you can view the message HTML and Smarty, the XML data being referenced and the rendered output that will display when a message is sent or previewed. Copy and paste the code below into a message and preview or send the message to see the results.

{$data=$utils->getSimpleXML('http://p13.zdassets.com/hc/theme_assets/656597/200049057/simpleXML_example_1.xml')}
<h1>{$data->channel->title}</h1>
{foreach $data->channel->item as $record}
    <h2>{$record->title}</h2>
    {$record->link}<br>
   {$record->description}<br><br>
{/foreach}
<rss version="2.0">
<channel>
  <title>Classic Albums</title>
  <link>https://www.example.com</link>
  <description>A list of classic albums.</description>
  <item>
    <title>Fleetwood Mac - Rumours</title>
    <link>http://fleetwoodmac.com</link>
    <description>Rumours tends to be remembered as the album that was characterized by personal strife between the band members.</description>
  </item>
  <item>
    <title>Pink Floyd - Dark Side of the Moon</title>
    <link>http://www.pinkfloyd.com/</link>
    <description>No-one in March 1973 could have imagined that an album released in that month would still be thrilling listeners 38 years later, but it's true. </description>
  </item>
  <item>
    <title>Led Zeppelin IV</title>
    <link>http://www.ledzeppelin.com/</link>
    <description>On 8th Nov 1971, Led Zeppelin released their fourth album.</description>
  </item>
</channel>
</rss>

Classic Albums

Fleetwood Mac: Rumours

http://fleetwoodmac.com

Rumours tends to be remembered as the album that was characterized by personal strife between the band members.

Pink Floyd: Dark Side of the Moon

http://www.pinkfloyd.com/

No-one in March 1973 could have imagined that an album released in that month would still be thrilling listeners 38 years later, but it's true.

Led Zeppelin IV

http://www.ledzeppelin.com/

On 8th Nov 1971, Led Zeppelin released their fourth album.

getSimpleXML method

In order to access remote XML data (such as an RSS feed) within a message, use the getSimpleXML method, which is a custom extension of Smarty provided by Cordial.

The getSimpleXML method has 2 parameters: url and cacheMinutes. They must be written in this order and separated by commas. Only "url" is required.

getSimpleXML($url, $cacheMinutes)

Parameter Description Required Expected Default
url URL for the XML resource. required valid URL NA
cacheMinutes Number of minutes to store the feed data in memory. This optimizes send time if the system doesn't have to request the feed for every individual message. optional integer 0

Set the URL

For the purpose of this example, we hosted the XML document and included the link below.

{$data=$utils->getSimpleXML('http://p13.zdassets.com/hc/theme_assets/656597/200049057/simpleXML_example_1.xml')}

In the example above, a variable "data" is assigned to the resulting data set returned from the remote XML document. The getSimpleXML method will get the data from the URL for each message that is rendered per contact.

Set the cacheMinutes

A cache can be set in memory for the data so the system doesn't have to request the feed for every individual message. This optimizes the time it takes to compile and send a message as well as reduces the load of the server hosting the data feed (your IT team will thank you). You can specify the number of minutes that the data is stored in memory with this parameter. In the example below, the data would be retrieved from the server 1 time every 20 minutes.

{$data=$utils->getSimpleXML('http://p13.zdassets.com/hc/theme_assets/656597/200049057/simpleXML_example_1.xml',20)}

Render the data in a message

After the data is available with the getSimpleXML method, you can use the data in the message.

Loop through data with {foreach}

A {foreach} statement is the most common way to loop through the data array and render the results in a message.

Example:

Render data that is stored as attribute values in an "enclosure" tag

Example of rendering all of the attribute key/value pairs from an XML node "enclosure":

{$data=$utils->getSimpleXML('http://p13.zdassets.com/hc/theme_assets/656597/200049057/simpleXML_example_2.xml')}
<h1>{$data->channel->title}</h1>
{foreach $data->channel->item as $record}
    <h2>{$record->title}</h2>
    {$record->link}<br>
   {$record->description}<br><br>
   {foreach $record->enclosure->attributes() as $key=>$attrs}
        {$key}: {$attrs}<br>
     {/foreach}<br><br>
{/foreach}

Example of rendering a specific attribute "url" value in the XML node "enclosure":

{$data=$utils->getSimpleXML('http://p13.zdassets.com/hc/theme_assets/656597/200049057/simpleXML_example_3.xml')}
{$namespace = $data->getNamespaces(true)}
<h1>{$data->channel->title}</h1>
{foreach $data->channel->item as $record}
    <h2>{$record->title}</h2>
    {$record->link}<br>
   Description: {$record->description}<br>
    <img src="{$record->children($namespace['media'])->content->attributes()->url}" style="width:150px; height:auto;" /><br><br>
{/foreach}

Classic Albums

Fleetwood Mac: Rumours

https://fleetwoodmac.com

Description: Rumours tends to be remembered as the album that was characterized by personal strife between the band members.

Pink Floyd: Dark Side of the Moon

http://www.pinkfloyd.com/

Description: No-one in March 1973 could have imagined that an album released in that month would still be thrilling listeners 38 years later, but it's true.

Led Zeppelin IV

http://www.ledzeppelin.com/

On 8th Nov 1971, Led Zeppelin released their fourth album.