Skip to content

Instantly share code, notes, and snippets.

Created April 29, 2016 15:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/09ee76f76ad7fa57d60d33ce0a636fd3 to your computer and use it in GitHub Desktop.
Save anonymous/09ee76f76ad7fa57d60d33ce0a636fd3 to your computer and use it in GitHub Desktop.
<?php
$start = abs(date('w')-1); //monday = 0
$start = $start . "1"; //monday = 01 (first index), friday = 41
$feed = implode(file('https://www.googleapis.com/customsearch/v1?alt=atom&cx=xxx:yyy&key=abc&q=timesheet+meme&searchType=image&start='.$start));
$xml = simplexml_load_string($feed);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$img = "";
shuffle($array["entry"]); //random selection from ten results starting from $start to avoid meme repetition
foreach($array["entry"] as $entry) { //loops until a .jpg is found
$mystring = $entry["id"];
$findme = '.jpg';
$pos = strpos($mystring, $findme);
if ($pos !== false) {
$img = $mystring;
break;
}
}
//returns JPG as an actual image
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-type: image/jpeg');
echo file_get_contents($img);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment