Skip to content

Instantly share code, notes, and snippets.

@thomaswardiii
Last active December 25, 2015 22:09
Show Gist options
  • Save thomaswardiii/7047679 to your computer and use it in GitHub Desktop.
Save thomaswardiii/7047679 to your computer and use it in GitHub Desktop.
Bacon ipsum API command line script. Now with ~/.bacon default
#!/usr/bin/php
<?php
/**
* Bacon ipsum PHP-CLI script
*
* Generates sentences or paragraphs from baconipsum.com
*
* @version 1.0.1
* @author Thomas Ward III <[email protected]>
*/
define("BASE_URL", "http://baconipsum.com/api/?");
$shortopts = "mfp:s:ih";
$longopts = array(
"paragraphs:",
"sentences:",
"ipsum",
"meat",
"filler"
);
$paras = 0;
$sentences = 0;
$ipsum = false;
$meat = false;
$filler = false;
$options = array();
$cli_options = array();
$ini_options = array();
// Parse config file if it exists
if (file_exists($_SERVER['HOME'] . "/.bacon"))
{
$ini_options = parse_ini_file($_SERVER['HOME'] . "/.bacon");
}
$cli_options = getopt($shortopts, $longopts);
// Remove meat/filler options when the command line uses it, since they
// conflict with each other, so handle ini meat=true and --filler CLI param
if (isset($cli_options['m']) || isset($cli_options['meat']) ||
isset($cli_options['f']) || isset($cli_options['filler']))
{
unset($ini_options['m'], $ini_options['meat'], $ini_options['f'],
$ini_options['filler']);
}
// Merge the two, cli gets final say
$options = array_merge($ini_options, $cli_options);
if (isset($options['h']) || isset($options['help']))
{
echo "Usage: " . $_SERVER['PHP_SELF'] . " (-m|--meat)|(-f|--filler) [-p|--paragraphs #] [-s|--sentences #] [-l|--lorem] [-h|--help]\n\n";
echo "-m|--meat: All Meat (default)\n";
echo "-f|--filler: Meat and Filler\n";
echo "-p|--paragraphs #: Number of paragraphs (default: 5)\n";
echo "-s|--sentences #: Number of sentences (overrides paragraphs)\n";
echo "-i|--ipsum: Start with 'Bacon ipsum dolor sit amet'\n";
echo "-h|--help: This help\n";
exit();
}
// Paragraphs
if (isset($options['p']))
{
$options['paragraphs'] = (int)$options['p'];
}
if (isset($options['paragraphs']))
{
$paras = (int)$options['paragraphs'];
}
// Sentences
if (isset($options['s']))
{
$options['sentences'] = (int)$options['s'];
}
if (isset($options['sentences']))
{
$sentences = (int)$options['sentences'];
}
// Use bacon ipsum
if (isset($options['i']))
{
$options['ipsum'] = true;
}
if (isset($options['ipsum']))
{
$ipsum = true;
}
// Filler
if (isset($options['f']))
{
$options['filler'] = true;
}
if(isset($options['filler']))
{
$filler = true;
}
// Meat
if (isset($options['m']))
{
$options['meat'] = true;
}
if(isset($options['meat']))
{
$filler = false;
}
$url = BASE_URL . "type=" .
($filler === TRUE ? "meat-and-filler" : "all-meat") .
($paras > 0 ? "&paras=$paras" : "") .
($sentences > 0 ? "&sentences=$sentences" : "") .
($ipsum ? "&start-with-lorem=1" : "");
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($curl_handle);
if($return === FALSE)
{
echo "Oops! That's not good. We got an error trying to get bacon. Please try again\n";
}
else
{
$json_data = json_decode($return);
foreach($json_data as $bacon_paragraph)
{
echo $bacon_paragraph . "\n\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment