You are here

public function BiblioCrossRefClient::fetch in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 modules/crossref/biblio.crossref.client.php \BiblioCrossRefClient::fetch()
  2. 7.2 modules/crossref/biblio.crossref.client.php \BiblioCrossRefClient::fetch()

File

modules/crossref/biblio.crossref.client.php, line 83

Class

BiblioCrossRefClient

Code

public function fetch() {
  $this->query = url($this->url, array(
    'query' => array(
      'pid' => $this->pid,
      'noredirect' => 'true',
      'format' => 'unixref',
      'id' => 'doi:' . $this->doi,
    ),
  ));
  $request_options = array(
    'method' => 'GET',
  );
  $result = drupal_http_request($this->query, $request_options);
  if ($result->code != 200) {
    drupal_set_message(t('HTTP error: %error (@number) when trying to contact crossref.org for XML input.', array(
      '%error' => empty($result->error) ? t('unspecified server error') : $result->error,
      '@number' => $result->code,
    )), 'error');
    return;
  }
  if (empty($result->data)) {
    drupal_set_message(t('Did not get any data from crossref.org'), 'error');
    return;
  }
  $sxml = @simplexml_load_string($result->data);
  if (!isset($sxml->doi_record)) {
    drupal_set_message(t('Failed to retrieve data for doi %doi', array(
      '%doi' => $this->doi,
    )), 'error');
    return;
  }
  if ($error = (string) $sxml->doi_record->crossref->error) {
    drupal_set_message(t('CrossRef Error: @error', array(
      '@error' => $error,
    )), 'error');
    return;
  }
  $this->nodes = array();
  $this->parser = drupal_xml_parser_create($result->data);

  // Use case-folding so we are sure to find the tag in.
  xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, FALSE);
  xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, TRUE);
  xml_set_object($this->parser, $this);
  xml_set_element_handler($this->parser, 'unixref_startElement', 'unixref_endElement');
  xml_set_character_data_handler($this->parser, 'unixref_characterData');
  if (!xml_parse($this->parser, $result->data)) {
    drupal_set_message(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser)), 'error');
  }
  xml_parser_free($this->parser);
  return $this->node;
}