aggregator.fetcher.inc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @file
  4. * Fetcher functions for the aggregator module.
  5. */
  6. /**
  7. * Implements hook_aggregator_fetch_info().
  8. */
  9. function aggregator_aggregator_fetch_info() {
  10. return array(
  11. 'title' => t('Default fetcher'),
  12. 'description' => t('Downloads data from a URL using Drupal\'s HTTP request handler.'),
  13. );
  14. }
  15. /**
  16. * Implements hook_aggregator_fetch().
  17. */
  18. function aggregator_aggregator_fetch($feed) {
  19. $feed->source_string = FALSE;
  20. // Generate conditional GET headers.
  21. $headers = array();
  22. if ($feed->etag) {
  23. $headers['If-None-Match'] = $feed->etag;
  24. }
  25. if ($feed->modified) {
  26. $headers['If-Modified-Since'] = gmdate(DATE_RFC1123, $feed->modified);
  27. }
  28. // Request feed.
  29. $result = drupal_http_request($feed->url, array('headers' => $headers));
  30. // Process HTTP response code.
  31. switch ($result->code) {
  32. case 304:
  33. break;
  34. case 301:
  35. $feed->url = $result->redirect_url;
  36. // Do not break here.
  37. case 200:
  38. case 302:
  39. case 307:
  40. if (!isset($result->data)) {
  41. $result->data = '';
  42. }
  43. if (!isset($result->headers)) {
  44. $result->headers = array();
  45. }
  46. $feed->source_string = $result->data;
  47. $feed->http_headers = $result->headers;
  48. break;
  49. default:
  50. watchdog('aggregator', 'The feed from %site seems to be broken, due to "%error".', array('%site' => $feed->title, '%error' => $result->code . ' ' . $result->error), WATCHDOG_WARNING);
  51. drupal_set_message(t('The feed from %site seems to be broken, because of error "%error".', array('%site' => $feed->title, '%error' => $result->code . ' ' . $result->error)));
  52. }
  53. return $feed->source_string === FALSE ? FALSE : TRUE;
  54. }