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;
 }
Example #2
0
 /**
  * Parses an OPML file.
  *
  * Feeds are recognized as <outline> elements with the attributes "text" and
  * "xmlurl" set.
  *
  * @param string $opml
  *   The complete contents of an OPML document.
  *
  * @return array
  *   An array of feeds, each an associative array with a "title" and a "url"
  *   element, or NULL if the OPML document failed to be parsed. An empty array
  *   will be returned if the document is valid but contains no feeds, as some
  *   OPML documents do.
  *
  * @todo Move this to a parser in https://www.drupal.org/node/1963540.
  */
 protected function parseOpml($opml)
 {
     $feeds = array();
     $xml_parser = drupal_xml_parser_create($opml);
     if (xml_parse_into_struct($xml_parser, $opml, $values)) {
         foreach ($values as $entry) {
             if ($entry['tag'] == 'OUTLINE' && isset($entry['attributes'])) {
                 $item = $entry['attributes'];
                 if (!empty($item['XMLURL']) && !empty($item['TEXT'])) {
                     $feeds[] = array('title' => $item['TEXT'], 'url' => $item['XMLURL']);
                 }
             }
         }
     }
     xml_parser_free($xml_parser);
     return $feeds;
 }