John Tiernan

 
Navigation

Home
About
Computing Projects
Microcontroller Platforms
Art
Blog
Demos
Everything Else
Contact Me

Username:

Password:


Forgotten password? | Register!
Code Sample - Last.FM - User's Most Recent Shouts
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>johntiernan.co.uk Demo - last.fm</title>
</head>
<body>
<?php
// Include the API and config files

require 'lastfmapi/lastfmapi.php';
require 'config.inc.php';

$method = "user.getshouts";
$user = "";

//If user is set use it, otherwise default to creator's username
if(isset($_POST['user']))
{
	$user = strip_tags($_POST['user']);
}else{
	$user = "ananasjihad";
}

echo "<h6>Shouts for ".$user."</h6>";

//Construct URL for API
$source = "http://ws.audioscrobbler.com/2.0/?method=".$method."&user=".$user."&api_key=".$publicAPI;

//Run API query
$xml = simplexml_load_file($source);

//Debugging junk
//echo $xml->getName() . "<br />";
//$data = array(0 => array("body" => "body","author" => "author","date" => "date"));

//Go through API response and construct array from data
$pos = 0; //Position in array. Could use array append functions here...
foreach($xml->children() as $child)
{
  //echo $child->getName() . "<br />";
  foreach($child->children() as $spawn)
  {
     $data[$pos] = array("body" => $spawn->body, "author" => $spawn->author, "date" => $spawn->date);
     //echo $spawn->getName() . "-" . $spawn->body . "<br />";
     $pos++;
  }
  
}

//For each item in array, construct HTML and output
//Could be done above, but split up for simplicity
for($i=0;$i<count($data);$i++)
{
   echo $data[$i]["body"] . "<br /><a href=\"http://www.last.fm/users/".$data[$i]["author"]."\">"
    . $data[$i]["author"] . "</a> - " . $data[$i]["date"] . "<hr />";
}

?>

<!-- Output logo as per terms -->
<a href="http://www.last.fm"><img src="/images/lastfm.png" 
width="90px" height="28px" border="0" /></a>

</body>
</html>
		
 

© John Tiernan 2019 | This site uses 960 Grid System as a layout support. | Valid XHTML 1.0 Strict | Valid CSS