Code is poetry

 

I’m doing the software thing again – it is so interesting ! I combine Flickr API stuff with  PHP programming and I am slowly unraveling their mysteries. “Code is poetry” – I think the wordpress people  have it right. I managed a simple ‘call by tag’ function that calls Flickr pictures. I managed to convert  the picture to binary, which shows when you hover the mouse over the picture. I’m putting this function under the  ‘Test‘ tab – it can be played with  if and when I am not testing something. Then, it will give error messages and other weird stuff,  because I’m working  trial-and-error style.

Am now wrestling with the authentication that Flickr demands for its more nifty API functions. No light at the end of that particular tunnel – yet.

 

And here’s the particular poetry that makes the above happen – great gulf between form and function:

 

<?php

/*
Template Name: testapi
*/
?>
<?php get_header(); ?>
<h2 >

<?php
echo “call flickr images by tag”;
?>
</h2>

<?php
/*
#
# get the tag
#
*/
?>

<?php

echo “<form action=\”http://www.visual-art-research.com/test-8/\” method=\”post\”>”;
echo “Write a tag: <input type=\”text\” name=\”ftag\” value=\”$tagname\”>”;
echo “<input type=\”submit\” />”;
echo “</form>”;
$tagname=$_POST[“ftag”];
?>

<?php
if (isset($tagname)) {
/*
#
# build the API URL to call
#
*/
?>
<?php
$params = array(
‘api_key’ => ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’,
‘method’ => ‘flickr.photos.search’,
‘tags’ => $tagname,
‘format’ => ‘php_serial’,
);

$encoded_params = array();

foreach ($params as $k => $v){
$encoded_params[] = urlencode($k).’=’.urlencode($v);
}
?>
<?php
/*
#
# call the API and decode the response
#
*/
?>

<?php
$url = “http://api.flickr.com/services/rest/?”.implode(‘&’, $encoded_params);
$rsp = file_get_contents($url);

$rsp_obj = unserialize($rsp);
/*
print_r ($rsp_obj);
*/
?>

<?php
/*
#
# display the first 20 photos (or an error if it failed)
#
*/
?>

<?php
$photos_total = $rsp_obj[‘photos’][‘total’];

if ($photos_total > 0){

echo “<h3 class= \”pagetitle\” >”;
echo “Displaying photo’s with tag \” $tagname \” (max 20 photo’s)”;
echo “<br/>”;
echo “</h3>”;

for ($i = 0; $i < 20 and $i < $photos_total; $i++) {
$photo_farm = $rsp_obj[‘photos’][‘photo’][$i][‘farm’];
$photo_owner = $rsp_obj [‘photos’][photo][$i][‘owner’];
$photo_server = $rsp_obj[‘photos’][‘photo’][$i][‘server’];
$photo_id = $rsp_obj[‘photos’][‘photo’][$i][‘id’];
$photo_secret = $rsp_obj[‘photos’][‘photo’][$i][‘secret’];
$photo_title = $rsp_obj[‘photos’][‘photo’][$i][‘title’];
$photo_URL = “http://farm”.$photo_farm.”.static.flickr.com/”.$photo_server.”/”.$photo_id.”_”.$photo_secret.”_s.jpg”;
$flickr_URL =”http://www.flickr.com/photos/”.$photo_owner.”/”.$photo_id.”/”;
/*
echo “<img src=”.$photo_URL.” width=150 height=150>”;
*/

# write the image to a file named $img and find length of $img
$img=file_get_contents($photo_URL);
$length = strlen ($img);

# add binary info to the #binTotString variable

for ($j = 0; $j < $length; $j++) {
$hexString = $img[$j];
$binNumeric = hexdec($hexString);
$binString = decbin($binNumeric);
$bin_length = strlen ($binString);
$binTotString .=$binString;
}
echo “<div id=contentnw>”;
echo “</div>”;
echo “<div>”;
echo “<div>”;
echo “<a href=”.$flickr_URL.” title='”.$binTotString.”‘> <img src=”.$photo_URL.” alt=”.$binTotString.”></a>”;
echo “</div>”;
echo “<h5>”;
echo “<a href=”.$flickr_URL.” width=150 height=150> View ‘”.$photo_title.”‘ at Flickr </a>”;
echo “</div>”;
echo “</h5>”;
}
}else{
echo “<br/>”;
echo “Sorry, no images with tag \” $tagname \””;
echo “<br/>”;
}
?>
<?php
}
?>

<?php get_footer(); ?>

« <-- previous post next post --> »