I have successfully been able to get a pubmed results page in xml format and write the contents to a local file "Publications.xml". The problem is when I use simplexml_load_file("Publications.xml"), it fails. Not able to figure out why.
<?php
$feed = 'http://www.ncbi.nlm.nih.gov/pubmed?term=carl&sort=pubdate&report=xml';
$local = 'Publications.xml';
$curtime = time();
$filemodtime;
if( (!file_exists($local)) || (time() - filemtime($local)) > 86400 )
{
$contents = file_get_contents($feed);
$fp = fopen($local,"w");
fwrite($fp, $contents);
fclose($fp);
}
$xml = simplexml_load_file($local) or ("Can't");
?>
On the last but the second line the parser fails and I get the message "Can't". I have double checked the xml file and it appears to be in a good shape.
If anyone can let me know about any workarounds for this one, I will be very grateful. Here's a copy of the xml file the PHP script above tries to read (http://pastebin.com/U0fEKmZL):
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<pre>
<PubmedArticle>
<MedlineCitation Status="Publisher" Owner="NLM">
<PMID Version="1">23314841</PMID>
<DateCreated>
<Year>2013</Year>
<Month>1</Month>
<Day>14</Day>
</DateCreated>
<Article PubModel="Print-Electronic">
<Journal>
<ISSN IssnType="Electronic">1432-0932</ISSN>
<JournalIssue CitedMedium="Internet">
<PubDate>
<Year>2013</Year>
<Month>Jan</Month>
<Day>12</Day>
</PubDate>
... (too long, see link)