Så har jeg også lavet et billede galleri, hvor jeg har brugt den API, som flickr stiller gratis til rådighed. Man søger så efter billeder i en bestemt katagori, og flickr returnerer billeder fra deres database. Det eneste, man skal gøre, er at oprette en flickr account på http://flickr.com/signup og derefter ansøge om en API på http://flickr.com/services/api/keys/apply
Denne applikation minder meget om den forrige applikation, jeg lavede nemlig “amazon search” applikationen. Koden er også mange steder næsten den samme, og hvis man lavede endnu større applikationer, er det her, at det ville være en stor fordel at lave nogle genbrugelige AS3 “classes”, som man blot henviser til i sin “package”, og som man så måke bare behøver at ændre små ting i.
Jeg prøvede at kommentere AS3 koden storts set uden brug af bogen, og det var rart, at jeg faktisk selv kunne gennemskue, hvordan koden hang sammen, og hvilken betydning de forskellige variabler, funktioner osv har.
PHP koden kommenterede jeg dog med hjælp fra bogen, da der var flere koder, jeg ikke lige selv kunne gennemskue. Specielt fordi det er noget kode, som kommer fra flickr og er helt nyt for mig.
Galleriet kan ses på http://www.jesperf.dk/flashandphp/flickr_gallery/index.html , men igen så er der problemer med at få lov til at søge og hente billederne ned, da det har noget med “cross domain policy” at gøre, og det har jeg ikke tid til at undersøge lige nu, hvordan man løser. Et andet problem er, at min server kun understøtter PHP4, og da PHP’en i min kode gør brug af “simpleXML” library, som kun virker i PHP 5, så ville der også komme en fejlmelding, hvis det andet problem var løst. Jeg forstår ikke helt hvorfor, de kun understøtter PHP4, og jeg overvejer at sende dem en mail, hvor jeg spørger hvorfor.
Her kommer koden, og så kan man jo selv prøve det af derhjemme på ens “localhost” server, for der virker det fint , og billedrne fra flickr dukker helt planlagt op, når man søger.
Først AS3 som er i 2 “classes”:
1:
package
{
// imports the nescesary classes
import flash.display.MovieClip;
import flash.events.*;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoader;
public class PhotoGallery extends MovieClip
{
// path to the php file on the server
public var webServiceURL:String = “http://localhost/flashandphp/flickrGallery/photoGallery.php”;
// defines the variables to use later, the photos array and the movieclip to hold the photos
private var photos:Array;
private var photosContainer:MovieClip;
// this function runs everytime the search button is clicked (the constructor function)
public function PhotoGallery()
{
//this is only visible in the fla file and this way you can follow the process
trace(“Simple WebService Searcher Started”);
// makes new instance
photos = new Array();
// makes new instance
photosContainer = new MovieClip();
// places the photos movieclip on the stage
photosContainer.y = 75;
// adds an eventlistener to the search button
searchBtn.addEventListener(MouseEvent.CLICK, searchHandler);
// adds photos movieclip holder to the stage
addChild(photosContainer);
}
// this function is responsible for removing old photos if any
// and to search for photos when something is entered in the “enter keywords” textfield
public function searchHandler(e:MouseEvent):void
{
// it only runs if something is entered
if(searchTxt.text.length > 0)
{
// calls the removeOldPhotos function
removeOldPhotos();
// calls the loadSearchResults function and sends along the keywords entered as an argument
loadSearchResults(searchTxt.text);
}
}
// this function send the request to the php file on the server which connects to flick to search for results
// the php file the sends the requested data back
public function loadSearchResults(tags:String):void
{
//this is only visible in the fla file and this way you can follow the process
trace(“loadSearchResults()”);
var urlVariables:URLVariables = new URLVariables();
urlVariables.tags = tags;
// connects to the php file on the server
var urlRequest:URLRequest = new URLRequest(webServiceURL);
//the dat send to the php file is the tags entered in the “enter keywords” textfield
urlRequest.data = urlVariables;
// uses the loader class to load the data
var urlLoader:URLLoader = new URLLoader();
//when the data is fully loaded the loadedResultsHandler function is called
urlLoader.addEventListener(Event.COMPLETE, loadedResultsHandler);
urlLoader.load(urlRequest);
}
public function loadedResultsHandler(e:Event):void
{
//this is only visible in the fla file and this way you can follow the process
trace(“loadedResultsHandler()”);
// when the data is fully loaded
var urlLoader:URLLoader = URLLoader(e.target);
// its made valid xml data to work with in flash
var xml:XML = new XML(urlLoader.data);
// a for…each loop is used to look through each item in the xml file
for each(var item in xml..photo)
{
// makes a new instance of the photoItem movieclip from the library
var photoItem:PhotoItem = new PhotoItem();
//calls the setValues metod to set title and thumbnail photo
photoItem.setValues(item..title, item..thumb);
// places the photos on top of each other
photoItem.y = (photos.length * (photoItem.height + 50));
// adds the photitem to the photos array
photos.push(photoItem);
// adds photoItem to the photosContainer movieclip on the stage
photosContainer.addChild(photoItem);
}
}
// removes the old photos
private function removeOldPhotos():void
{
// only removes photos if there are any
if(photos.length > 0)
{
// makes new array to clear the old one
photos = new Array();
// removes the photosContainer movieclip
removeChild(photosContainer);
// and makes a new on and places and adds it to the stage
photosContainer = new MovieClip();
photosContainer.y = 75;
addChild(photosContainer);
}
}
}
}
2:
package
{
// imports the nescesary classes
import flash.display.MovieClip;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
public class PhotoItem extends MovieClip
{
// makes the variables to use later(and gives them the string datatype)
private var title:String;
private var thumb:String;
// the constuctor function is empty – doesent mean anything!
public function photoItem()
{
}
//This function takes the two argument from (photoItem.setValues(item..title, item..thumb)
// and datatypes them to strings
public function setValues(t:String, i:String):void
{
// makes the variables private, so they can’t accidently be changed or modified
title = t;
thumb = i;
// sets the title retrieved from the xml equal to the titleTxt text
titleTxt.text = title;
//places it on the stage in the titleTxt textfield component. And moves it out so there are room for the thumbnail
titleTxt.x = 100;
// calls the loadthumb function
loadThumb();
}
// this function loads the thumbnail images through the url provided from flikr which was sent to php and sent back to flash as xml
public function loadThumb():void
{
//this is only visible in the fla file and this way you can follow the process
trace(“load thumb”);
//loads the thumbnail image
var thumbLoader:Loader = new Loader();
var thumbURL:String = thumb;
var thumbURLRequest:URLRequest = new URLRequest(thumbURL);
thumbLoader.load(thumbURLRequest);
thumbLoader.x = 10;
addChild(thumbLoader);
}
// don’t know what this function does because the application runs perfect without
// but in the book he says: “this method is for passing back the image information”.
public function getValues():Object
{
return {title:title};
}
}
}
Php’en ser således ud:
<?php
// first step is to define the flickr API key that is provided from flickr
define(“FLICKR_ACCESS_KEY”, “flickr API key copy in here!”);
// flickr har a uniqe way of building image URLs and by later in this php code the use of sprintf function is used to work with that format
$flickrURLTemplate = “http://farm%s.static.flickr.com/%s/%s_%s_s.jpg”;
// if the search tags that comes from flash accidently is empty the php script stops and exits
if(empty($_GET['tags']))
{
exit();
}
// the seach tages from flash is stored in the searchTags variable
$searchTags = $_GET['tags'];
// next step is to pull in the search tags data and make a call to flickr to start the search process
$apiMethod = “flickr.photos.search”;
$request = “http://api.flickr.com/services/rest/?method=” . $apiMethod . “&tags=” . $searchTags .”&api_key=” . FLICKR_ACCESS_KEY. “&per_page=5″;
// the XML data returned is stores in the rawXml variable
$rawXml = file_get_contents($request);
// when its fully loaded it is passes along to the simplexml_load_string method which creates a proper object tha php can then parse
$xmlResponse = simplexml_load_string($rawXml);
// how many images to display is determined by loading the value that is located in the XML
$nodeCount = getAttribute($xmlResponse->photos, “perpage”);
// the process of building the custom XML document is done using a for.. each loop that graps each photo node and pulls in the required information
$xml = “<?xml version=\”1.0\” ?>\n<photos>”;
for($i=0; $i < $nodeCount; $i++)
{
// Flickr vars
// this is for building the image path, which is pased into the sprintf function
$farmID = getAttribute($xmlResponse->photos->photo[$i], “farm”);
$serverID = getAttribute($xmlResponse->photos->photo[$i], “server”);
$photoID = getAttribute($xmlResponse->photos->photo[$i], “id”);
$secret = getAttribute($xmlResponse->photos->photo[$i], “secret”);
$xml .= “ <photo>”;
$xml .= “ <title><![CDATA[" . getAttribute($xmlResponse->photos->photo[$i], “title”) . “]]></title>”;
$xml .= “ <thumb>” . sprintf($flickrURLTemplate, $farmID, $serverID, $photoID, $secret) . “</thumb>”;
$xml .= “ </photo>”;
}
$xml .= “</photos>”;
// sets the correct header type
header(“content-type: text/xml”);
// the data sent back to flash in an XML document
print $xml;
// the getAttribute function with two arguments taht helps build the right xml document.
function getAttribute($xml, $name)
{
foreach($xml->attributes() as $key=>$val)
{
if($key == $name)
{
return (string)$val;
}
}
// returns false if this comparison isnt true: ($key == $name)
return false;
}
?>