function drush_xkcd_fetch

8.0.x drush_xkcd_fetch($search = '')
6.x drush_xkcd_fetch($search = '')
7.x drush_xkcd_fetch($search = '')
4.x drush_xkcd_fetch($search = '')
5.x drush_xkcd_fetch($search = '')
master 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 '', 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.


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

See also




examples/, line 121
Example drush command.


function drush_xkcd_fetch($search = '') {
  if (empty($search)) {
  elseif (is_numeric($search)) {
    drush_xkcd_display('' . $search);
  elseif ($search == 'random') {
    $xkcd_response = @json_decode(file_get_contents(''));
    if (!empty($xkcd_response->num)) {
      drush_xkcd_display('' . 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('' . 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) {
    else {
      drush_set_error('DRUSH_XKCD_SEARCH_FAIL', dt('The search failed or produced no results.'));