Note: This post is a follow-up to Coding Thesis Piece in Javascript
The Markup Including PHP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>PHP Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv=refresh content="7; URL=index.php" />
<link rel = "stylesheet" type = "text/css" href = "thesis.css" />
<script type="text/javascript" src = "script.js"> </script>
</head>
<body>
<div id = "tweets">
<p>
<?php
// Call Flickr's API and return JSON object, it comes wrapped inside jsonFlickrApi();
$flickrResponse = file_get_contents("http://www.flickr.com/services/rest/?method=
flickr.photos.getRecent&api_key={apikey}&format=json&per_page=1", true);
// Remove the "jsonFlickrApi(" from the beginning of the JSON object
$flickrJSON = str_replace("jsonFlickrApi({", "{", $flickrResponse);
// Remove the closing ")" from the JSON object
$flickrJSON = substr_replace($flickrJSON,"",-1);
// Run PHP's json_decode(); function to turn JSON object in PHP array
$json_flickrObject = json_decode($flickrJSON);
// Create variables containing the values of nodes needed to construct Flickr img URL
$farm = $json_flickrObject->photos->photo[0]->farm;
$id = $json_flickrObject->photos->photo[0]->id;
$server = $json_flickrObject->photos->photo[0]->server;
$secret = $json_flickrObject->photos->photo[0]->secret;
// Call Twitter's API, return JSON object
$twitterResponse = file_get_contents("http://search.twitter.com/search.json?q=the&rpp=1",
true);
// Run PHP's json_decode(); function to turn JSON object into PHP array
$json_tweetObject = json_decode($twitterResponse);
// Extract "text" from the first index of array "results" inside $tweetObject
print ($json_tweetObject->results[0]->text);
?>
</p>
<div id="img">
<?php
// Insert needed variables into string to create <img> tag to Flickr image
echo "<img src=\"http://farm$farm.static.flickr.com/$server/$id"._."$secret.jpg\" alt=\
"Flickr Image\" />";
?>
</div>
</div>
</body>
</html>
What’s Changed, Why It Matters
Building the page to be independent of Javascript is advantageous from an accessibility standpoint, particularly in that it allows the document to function properly even if a user’s browser has Javascript disabled, or if Javascript malfunctions due to another open window. Because PHP is a server-side scripting language, all functionality is taken care of before information is sent to the browser. I’d display a working model of this version but my host doesn’t have PHP5 installed, therefore the json_decode(); function does not work properly. However, just know that it renders the same, the key differece being that the source code displays all HTML generated by the PHP, whereas the previous version did not. Scripting on the server-side also keeps my api key (and any other important information in other cases) hidden from view of any user who uses the “view source” command. This project has been an exciting learning experience. This is my second attempt to code anything with PHP, I’m glad to say it worked out in only a couple hours. Enjoy! Questions? Comments?
Tags: 2009, API, Interactive, PHP, stephen bush, Stephen Wyatt Bush, UTC, wyattdanger, XHTML