You are here

class FeedsHTTPFetcherAppendHeaders in Feeds HTTPFetcher Append Headers 7

Fetches data via HTTP and appending custom headers.

Hierarchy

Expanded class hierarchy of FeedsHTTPFetcherAppendHeaders

1 string reference to 'FeedsHTTPFetcherAppendHeaders'
feeds_httpfetcher_append_headers_feeds_plugins in ./feeds_httpfetcher_append_headers.module
Implements hook_feeds_plugins().

File

plugins/FeedsHTTPFetcherAppendHeaders.inc, line 51
Home of the FeedsHTTPFetcherAppendHeaders and related classes.

View source
class FeedsHTTPFetcherAppendHeaders extends FeedsFetcher {

  /**
   * Implements FeedsFetcher::fetch().
   */
  public function fetch(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    $append_headers = $this
      ->prepareAppendHeaders($this->config['append_headers']);
    $fetcher_result = new FeedsHTTPFetcherAppendHeadersResult($source_config['source'], $append_headers);

    // When request_timeout is empty, the global value is used.
    $fetcher_result
      ->setTimeout($this->config['request_timeout']);
    return $fetcher_result;
  }

  /**
   * Clear caches.
   */
  public function clear(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    $url = $source_config['source'];
    feeds_include_library('http_request.inc', 'http_request');
    http_request_clear_cache($url);
  }

  /**
   * Implements FeedsFetcher::request().
   */
  public function request($feed_nid = 0) {
    feeds_dbg($_GET);
    @feeds_dbg(file_get_contents('php://input'));
    try {
      feeds_source($this->id, $feed_nid)
        ->existing()
        ->import();
    } catch (Exception $e) {

      // In case of an error, respond with a 503 Service (temporary) unavailable.
      header('HTTP/1.1 503 "Not Found"', NULL, 503);
      drupal_exit();
    }

    // Will generate the default 200 response.
    header('HTTP/1.1 200 "OK"', NULL, 200);
    drupal_exit();
  }

  /**
   * Override parent::configDefaults().
   */
  public function configDefaults() {
    return array(
      'append_headers' => '',
      'request_timeout' => NULL,
    );
  }

  /**
   * Override parent::configForm().
   */
  public function configForm(&$form_state) {
    $form = array();
    $form['append_headers'] = array(
      '#type' => 'textarea',
      '#title' => t('Append headers'),
      '#description' => t('Enter request headers that should be appended in the HTTP request. One header per line in the format key|value.'),
      '#default_value' => $this->config['append_headers'],
    );

    // Per importer override of global http request timeout setting.
    $form['request_timeout'] = array(
      '#type' => 'textfield',
      '#title' => t('Request timeout'),
      '#description' => t('Timeout in seconds to wait for an HTTP get request to finish.</br>' . '<b>Note:</b> this setting will override the global setting.</br>' . 'When left empty, the global value is used.'),
      '#default_value' => $this->config['request_timeout'],
      '#element_validate' => array(
        'element_validate_integer_positive',
      ),
      '#maxlength' => 3,
      '#size' => 30,
    );
    return $form;
  }

  /**
   * Expose source form.
   */
  public function sourceForm($source_config) {
    $form = array();
    $form['source'] = array(
      '#type' => 'textfield',
      '#title' => t('URL'),
      '#description' => t('Enter a feed URL.'),
      '#default_value' => isset($source_config['source']) ? $source_config['source'] : '',
      '#maxlength' => NULL,
      '#required' => TRUE,
    );
    return $form;
  }

  /**
   * Override parent::sourceFormValidate().
   */
  public function sourceFormValidate(&$values) {
    $values['source'] = trim($values['source']);
    if (!feeds_valid_url($values['source'], TRUE)) {
      $form_key = 'feeds][' . get_class($this) . '][source';
      form_set_error($form_key, t('The URL %source is invalid.', array(
        '%source' => $values['source'],
      )));
    }
  }
  public function prepareAppendHeaders($append_headers) {
    $headers = array();
    $append_headers = explode("\n", $append_headers);
    if (is_array($append_headers)) {
      foreach ($append_headers as $header) {
        $headers[] = str_replace('|', ': ', $header);
      }
    }
    return $headers;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsHTTPFetcherAppendHeaders::clear public function Clear caches.
FeedsHTTPFetcherAppendHeaders::configDefaults public function Override parent::configDefaults().
FeedsHTTPFetcherAppendHeaders::configForm public function Override parent::configForm().
FeedsHTTPFetcherAppendHeaders::fetch public function Implements FeedsFetcher::fetch().
FeedsHTTPFetcherAppendHeaders::prepareAppendHeaders public function
FeedsHTTPFetcherAppendHeaders::request public function Implements FeedsFetcher::request().
FeedsHTTPFetcherAppendHeaders::sourceForm public function Expose source form.
FeedsHTTPFetcherAppendHeaders::sourceFormValidate public function Override parent::sourceFormValidate().