Skip to content

Instantly share code, notes, and snippets.

@depoulo
Last active December 24, 2023 17:20
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 depoulo/ba2cb64702316a7b01d8 to your computer and use it in GitHub Desktop.
Save depoulo/ba2cb64702316a7b01d8 to your computer and use it in GitHub Desktop.
Get random gif from Giphy - might be against their TOS, please check yourself and use reasonably - I use it for our continuos integration status monitor, which of course is almost never red ;)
// get random image from Giphy
function getRandomGif($topic = 'fail') {
$data = file_get_contents("http://giphy.com/search/$topic");
preg_match_all('@href="/gifs/(.*?)"@', $data, &$matches);
$gifId = $matches[1][array_rand($matches[1])];
return "http://media2.giphy.com/media/$gifId/giphy.gif";
}
@Kevinrob
Copy link

Giphy have an API too! :octocat: 👍

function getRandomGif($topic = 'fail') {
  $gif = json_decode(file_get_contents('http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag='.$topic), true);
  return $gif['data']['image_url'];
}

Note: not the best clean and robust way for a HTTP call... Guzzle?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment