function drush_xkcd_fetch

8.0.x xkcd.drush.inc drush_xkcd_fetch($search = '')
6.x xkcd.drush.inc drush_xkcd_fetch($search = '')
7.x xkcd.drush.inc drush_xkcd_fetch($search = '')
4.x xkcd.drush.inc drush_xkcd_fetch($search = '')
5.x xkcd.drush.inc drush_xkcd_fetch($search = '')
master xkcd.drush.inc drush_xkcd_fetch($search = '')

Example drush command callback. This is where the action takes place.

The function name should be same as command name but with dashes turned to underscores and 'drush_commandfile_' prepended, where 'commandfile' is taken from the file 'commandfile.drush.inc', which in this case is 'sandwich'. Note also that a simplification step is also done in instances where the commandfile name is the same as the beginning of the command name, "drush_example_example_foo" is simplified to just "drush_example_foo". To also implement a hook that is called before your command, implement "drush_hook_pre_example_foo". For a list of all available hooks for a given command, run drush in --debug mode.

If for some reason you do not want your hook function to be named after your command, you may define a 'callback' item in your command object that specifies the exact name of the function that should be called. However, the specified callback function must still begin with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute") if you want that all hook functions are still called (e.g. drush_example_pre_foo_execute, and so on).

In this function, all of Drupal's API is (usually) available, including any functions you have added in your own modules/themes.

Parameters

An optional string with search keyworks, cartoon ID or "random".:

See also

drush_invoke()

drush.api.php

File

examples/xkcd.drush.inc, line 120
Example drush command.

Code

function drush_xkcd_fetch($search = '') {
  if (empty($search)) {
    drush_xkcd_display('http://xkcd.com');
  }
  elseif (is_numeric($search)) {
    drush_xkcd_display('http://xkcd.com/' . $search);
  }
  elseif ($search == 'random') {
    $xkcd_response = @json_decode(file_get_contents('http://xkcd.com/info.0.json'));
    if (!empty($xkcd_response->num)) {
      drush_xkcd_display('http://xkcd.com/' . rand(1, $xkcd_response->num));
    }
  }
  else {
    // This uses an API key with a limited number of searches per 
    $search_response = @json_decode(file_get_contents('https://www.googleapis.com/customsearch/v1?key=' . drush_get_option('google-custom-search-api-key', 'AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek') . '&cx=012652707207066138651:zudjtuwe28q&q=' . $search));
    if (!empty($search_response->items)) {
      foreach ($search_response->items as $item) {
        drush_xkcd_display($item->link);
      }
    }
    else {
      drush_set_error('DRUSH_XKCD_SEARCH_FAIL', dt('The search failed or produced no results.'));
    }
  }
}