Use PHP to Display EXIF Data from Flickr

Speacial Note: If you want this in a format you can copy/paste click here.

I recently added EXIF data to my photo pages in my portfolio with information like Camera used and the ISO setting. I decided it would be good to share this with the PHP community in case anyone was curious or is having trouble. I don’t work with XML too often, so there might be a more efficient way of doing this.

This is just a function for displaying the particular EXIF data. You should look at the output in case you want to change the information from clean to raw for particular things, or add data for checking if there is a clean and if not then return the raw data.

  1. function grabExif($id)
  2. {
  3.         $url            = "http://api.flickr.com/services/rest/?method=flickr.photos.getExif&api_key=YOUR_API_KEY&photo_id=";
  4.         $url            .= "$id";
  5.         $content        = file_get_contents("$url");
  6.         $xml            = new SimpleXMLElement($content);
  7.         //echo $content;
  8.         foreach ($xml->photo->exif as $exifData)
  9.         {
  10.                 foreach($exifData->attributes() as $a => $b) {
  11.                         //echo $a,’="’,$b,"\"\n";
  12.                         if(($b=="Model")&&($a=="label"))
  13.                         {
  14.                                 $model[count($model)] = (string) $exifData->raw;
  15.                         }
  16.                         else if(($b=="Make")&&($a=="label"))
  17.                         {
  18.                                 $make[count($make)] = (string) $exifData->raw;
  19.                         }
  20.                         else if(($b=="ISO Speed")&&($a=="label"))
  21.                         {
  22.                                 $ISO[count($ISO)] = (string) $exifData->raw;
  23.                         }
  24.                         else if(($b=="Aperture")&&($a=="label"))
  25.                         {
  26.                                 $aperture[count($aperture)] = (string) $exifData->clean;
  27.                         }
  28.                         else if(($b=="Exposure")&&($a=="label"))
  29.                         {
  30.                                 $shutterSpeed[count($shutterSpeed)] = (string) $exifData->raw;
  31.                         }
  32.                 }
  33.         }
  34.         if($model[0]!="")
  35.         {
  36.                 return "Taken with the " . $model[0] . " at " . $aperture[0] . " with an exposure time of " . $shutterSpeed[0] . "s using ISO" . $ISO[0];
  37.         }
  38.         else
  39.         {
  40.                 return "No exif data provided";
  41.         }
  42. }

Just an FYI, getting a syntax highlighter running in WordPress properly can get dicey. I went with Dean’s Code Highlighter and added

  1. $content = htmlspecialchars_decode($content);

to line 126 of deans_code_highlighter.php under

  1. function ch_the_content_filter($content)

in order to display the ampersands properly. Sometimes WordPress thinks it knows best when it comes to formatting posts. I may update this post later, but this should be enough to go on for all of you PHP/Flickr developers out there.