Skip to content

Instantly share code, notes, and snippets.

@brendandawes
Created January 9, 2012 17:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendandawes/1584039 to your computer and use it in GitHub Desktop.
Save brendandawes/1584039 to your computer and use it in GitHub Desktop.
Happiness Machine PHP code
<?php
function download($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
// get feelings from wefeelfine.com See the API for more details
$xmlUrl = "http://api.wefeelfine.org:8080/ShowFeelings?display=xml&feeling=happy&returnfields=feeling,sentence,country,city,postdate&limit=50"; // XML feed file/URL
$xmlStr = download($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$sentence = "";
$feeling = "";
$country = "";
$postdate = "";
$city = "";
// There's probably a better way to do this, but it works!
foreach($xmlObj->feeling[rand(0, 49)]->attributes() as $a => $b) {
if ($a == "sentence"){
$sentence = $b;
}
if ($a == "feeling"){
$feeling = $b;
}
if ($a == "country"){
$country = $b;
}
if ($a == "city"){
$city = $b;
}
if ($a == "postdate"){
$postdate = $b;
}
}
$d = "";
if ($postdate !="") {
$d .= "On ".$postdate." someone ";
}
if ($country !="") {
$d .= "in ".$country." ";
}
if ($feeling !="") {
$d .= "was feeling ".$feeling." and ";
}
if ($sentence !="") {
$d .= "said \n\"".$sentence."\"";
}
echo "data=".wordwrap($d, 30, "\n", false)."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment