Tutorial: Convert RSS to JSON with PHP and import the JSON into Flash with AS 2
Recently we had a customer at the office who wanted a news feed from a related site displayed on their homepage. As a design element we wanted to extract the first image in the description part of each news item in the feed and display it in a separate container inside the flash file.
The PHP file, flavored with a RSS class and a JSON class
You need LastRSS from Oslab and Services_JSON from Pear. This is the full listing of the PHP file:
header('Content-type: application/json; charset=utf-8');<br />require_once('lastRSS.php');<br />require_once('JSON.php');<br /><br />$rss = new lastRSS; <br />$rss->cache_dir = './cache'; <br />$rss->CDATA = 'content';<br />$rss->cache_time = 1800; // half hour<br /><br />// Try to load and parse RSS file <br />if ($rs = $rss->get('http://www.yourdomain.no/rss')) { <br /><br /> for($i = 0; $i < count($rs['items']); $i++) {<br /> // Create and extra field to hold the image from description<br /> $rs['items'][$i]['image'] = "";<br /> // Does description contain a image? If yes but the url in the image field<br /> if(preg_match('/(?<=\")http:\/\/[^\"]+(?=\")/i', $rs['items'][$i]['description'], $matches) ) {<br /> $rs['items'][$i]['image'] = $matches[0];<br /> }<br /> // Remove html tags and fix the encoding of norwegian chars, both in title and description<br /> $rs['items'][$i]['title'] = utf8_encode(strip_tags($rs['items'][$i]['title']));<br /> $rs['items'][$i]['description'] = utf8_encode(strip_tags($rs['items'][$i]['description']));<br /> }<br /> // Create a JSON class and output it as JSON<br /> $json = new Services_JSON();<br /> echo $json->encode($rs);<br />} else { <br /> echo "Error: It's not possible to reach RSS file...\n"; <br />}<br />?> Parse JSON in Flash
You need a JSON AS 2 class from json.org. To display the RSS feed in grapical manners you might create a movieclip with two dynamic textfields, one for the title and one for the description. I also created a movieclip inside to hold the image. Create an invisible button on the top layer to connect the a getURL action. Place this code in the root layer of your flash file:
import JSON.*;<br /><br />var json = new JSON();<br />var thisObj = this;<br />var jsondata:LoadVars = new LoadVars(); <br />var jsonObj:Object = {};<br /><br />jsondata.onData = function(src:String) {<br /> if (src undefined) {
trace("Error loading content.");
return;
}
thisObj.jsonObj = json.parse(src);
thisObj.buildRSS();
};
// Load the PHP file that output JSON
jsondata.load("http://www.yourdomain.com/output_json.php");
var rss_container:MovieClip = this.rss_container;
function buildRSS() {
for (var i = 0; i
var post:Object = jsonObj.items[i];
var rss_item:MovieClip;
rss_item = rss_container.attachMovie('rss_item', 'rss_item'+i, 10+i);
rss_item._y = i*(rss_item._height);
// Fills the text boxes
rss_item.heading.text = post.title;
rss_item.entry.text = post.description.substr(0, 140) + "..";;
// Check if image, and load it..
if (post.image != "") rss_item.rss_img.loadMovie(post.image);
else rss_item.rss_img._alpha = 30;
// Create alternativ backgrounds
if (i%2 1) rss_item.bg.gotoAndStop(2);<br /> <br /> rss_item.layer.link = post.link;<br /> rss_item.layer.onPress = function() {<br /> getURL(this.link, "_blank");<br /> }<br /> }<br />} RSS direct into flash [not used here]
Flash supports XML, but you would like to use a own class for the RSS handling. While working with this project I found this AS 2 class from 31tools.com. It works fine as long as you are able to place a crossdomain.xml (flash security ;-) file on the server you are getting content from. It don’t seems to show norwegian characters right, but that might be a problem related to server my RSS file was located.