You are here

public function BiblioCrossRefClient::fetch in Bibliography Module 7.2

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

File

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

Class

BiblioCrossRefClient

Code

public function fetch() {
  $this->query = $this->url . '?pid=' . $this->pid . '&noredirect=true&format=unixref&id=doi%3A' . $this->doi;
  $request_options = array(
    'method' => 'POST',
  );
  $result = drupal_http_request($this->query, $request_options);
  if ($result->code != 200) {
    drupal_set_message(t('HTTP error: !error when trying to contact crossref.org for XML input', array(
      '!error' => $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 ($error = (string) $sxml->doi_record->crossref->error) {
    drupal_set_message($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;
}