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.
The user-agent header will be included with every request.
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. You can use the example feed in your system for testing.
{$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:
{$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}
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} -
<rss version="2.0">
<channel>
<title>Classic Albums</title>
<link>https://www.example.com</link>
<description>Channel Description</description>
<item>
<title>Fleetwood Mac - Rumours</title>
<link>https://fleetwoodmac.com</link>
<description>Rumours tends to be remembered as the album that was characterized by personal strife between the band members.</description>
<enclosure url="http://i142.photobucket.com/albums/r96/thisdayinmusic/Rumours_7938.png" length="123456789" type="image/jpeg" />
</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>
<enclosure url="http://i142.photobucket.com/albums/r96/thisdayinmusic/DSOTM.jpg" length="123456789" type="image/jpeg" />
</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>
<enclosure url="http://i142.photobucket.com/albums/r96/thisdayinmusic/ledzeppelin4-560x560.jpg" length="123456789" type="image/jpeg" />
</item>
</channel>
</rss> -
Classic Albums
Fleetwood Mac: Rumours
https://fleetwoodmac.com
Rumours tends to be remembered as the album that was characterized by personal strife between the band members.
url: http://i142.photobucket.com/albums/r96/thisdayinmusic/Rumours_7938.png
length: 123456789
type: image/jpeg
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.
url: http://i142.photobucket.com/albums/r96/thisdayinmusic/DSOTM.jpg
length: 123456789
type: image/jpeg
Led Zeppelin IV
http://www.ledzeppelin.com/
On 8th Nov 1971, Led Zeppelin released their fourth album.
url: http://i142.photobucket.com/albums/r96/thisdayinmusic/ledzeppelin4-560x560.jpg
length: 123456789
type: image/jpeg
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_2.xml')}
<h1>{$data->channel->title}</h1>
{foreach $data->channel->item as $record}
<h2>Title: {$record->title}</h2>
{$record->link}<br>
{$record->description}<br>
<img src="{$record->enclosure->attributes()->url}" style="width:150px; height:auto;" /><br><br>
{/foreach} -
<rss version="2.0">
<channel>
<title>Classic Albums</title>
<link>https://www.example.com</link>
<description>Channel Description</description>
<item>
<title>Fleetwood Mac - Rumours</title>
<link>https://fleetwoodmac.com</link>
<description>Rumours tends to be remembered as the album that was characterized by personal strife between the band members.</description>
<enclosure url="http://i142.photobucket.com/albums/r96/thisdayinmusic/Rumours_7938.png" length="123456789" type="image/jpeg" />
</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>
<enclosure url="http://i142.photobucket.com/albums/r96/thisdayinmusic/DSOTM.jpg" length="123456789" type="image/jpeg" />
</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>
<enclosure url="http://i142.photobucket.com/albums/r96/thisdayinmusic/ledzeppelin4-560x560.jpg" length="123456789" type="image/jpeg" />
</item>
</channel>
</rss> -
Classic Albums
Title: Fleetwood Mac: Rumours
https://fleetwoodmac.com
Rumours tends to be remembered as the album that was characterized by personal strife between the band members.
Title: 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.
Title: Led Zeppelin IV
http://www.ledzeppelin.com/
On 8th Nov 1971, Led Zeppelin released their fourth album.
Render data stored as attribute values in a "namespace" tag
Example of rendering all of the attribute key/value pairs from an XML node "content" with a namespace "media":
-
{$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>
{$record->description}<br><br>
{foreach $record->children($namespace['media'])->content->attributes() as $key=>$attrs}
{$key}: {$attrs}<br>
{/foreach}<br><br>
{/foreach} -
<rss xmlns:media="http://example.com" version="2.0">
<channel>
<title>Classic Albums</title>
<link>https://www.example.com</link>
<description>Channel Description</description>
<item>
<title>Fleetwood Mac - Rumours</title>
<link>https://fleetwoodmac.com</link>
<description>Rumours tends to be remembered as the album that was characterized by personal strife between the band members.</description>
<media:content url="http://i142.photobucket.com/albums/r96/thisdayinmusic/Rumours_7938.png" height="512" width="512" type="image/jpg" />
</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>
<media:content url="http://i142.photobucket.com/albums/r96/thisdayinmusic/DSOTM.jpg" height="500" width="500" type="image/jpg" />
</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>
<media:content url="http://i142.photobucket.com/albums/r96/thisdayinmusic/ledzeppelin4-560x560.jpg" height="560" width="560" type="image/jpg" />
</item>
</channel>
</rss> -
Classic Albums
Fleetwood Mac: Rumours
https://fleetwoodmac.com
Rumours tends to be remembered as the album that was characterized by personal strife between the band members.
url: http://i142.photobucket.com/albums/r96/thisdayinmusic/Rumours_7938.png
height: 512
width: 512
type: image/jpg
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.
url: http://i142.photobucket.com/albums/r96/thisdayinmusic/DSOTM.jpg
height: 500
width: 500
type: image/jpg
Led Zeppelin IV
http://www.ledzeppelin.com/
On 8th Nov 1971, Led Zeppelin released their fourth album.
url: http://i142.photobucket.com/albums/r96/thisdayinmusic/ledzeppelin4-560x560.jpg
height: 560
width: 560
type: image/jpg
Example of rendering a specific attribute "url" value in the XML node "content" with a namespace "media":
-
{$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} -
<rss xmlns:media="http://example.com" version="2.0">
<channel>
<title>Classic Albums</title>
<link>https://www.example.com</link>
<description>Channel Description</description>
<item>
<title>Fleetwood Mac - Rumours</title>
<link>https://fleetwoodmac.com</link>
<description>Rumours tends to be remembered as the album that was characterized by personal strife between the band members.</description>
<media:content url="http://i142.photobucket.com/albums/r96/thisdayinmusic/Rumours_7938.png" height="512" width="512" type="image/jpg" />
</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>
<media:content url="http://i142.photobucket.com/albums/r96/thisdayinmusic/DSOTM.jpg" height="500" width="500" type="image/jpg" />
</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>
<media:content url="http://i142.photobucket.com/albums/r96/thisdayinmusic/ledzeppelin4-560x560.jpg" height="560" width="560" type="image/jpg" />
</item>
</channel>
</rss> -
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.
In the next article, you can learn about getting recommended products.
Comments
0 comments
Please sign in to leave a comment.