2017-02-25 42 views
1

林使用以下获得的XML文件的内容来访问XML文件:尝试使用使用simplexml_load_file /串

<?php 

parse_str($_SERVER['QUERY_STRING']); 

$file_loc="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=".$search."; 

$raw_xml = simplexml_load_file($file_loc) or die("Error: Cannot create object"); 

$xml = simplexml_load_string('<xml_container>'.$raw_xml.'</xml_container>'); 

$xml = json_decode(json_encode($xml) , 1); 

echo "<br> print_r:"; 
print_r($xml); 

echo "<br> vardump:"; 
var_dump($xml); 


?> 

一个实例使用“ACTN3”作为检索词,即,

https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ACTN3 

应该返回:

<eSearchResult> 
<Count>248</Count> 
<RetMax>20</RetMax> 
<RetStart>0</RetStart> 
<IdList> 
<Id>28195972</Id> 
<Id>28177749</Id> 
<Id>28177711</Id> 
<Id>28154975</Id> 
<Id>28139640</Id> 
<Id>27966742</Id> 
<Id>27913923</Id> 
<Id>27861536</Id> 
<Id>27821924</Id> 
<Id>27819725</Id> 
<Id>27798356</Id> 
<Id>27747845</Id> 
<Id>27601773</Id> 
<Id>27584214</Id> 
<Id>27508148</Id> 
<Id>27442335</Id> 
<Id>27361258</Id> 
<Id>27294501</Id> 
<Id>27274666</Id> 
<Id>27188902</Id> 
</IdList> 
<TranslationSet/> 
<TranslationStack> 
<TermSet> 
<Term>ACTN3[All Fields]</Term> 
<Field>All Fields</Field> 
<Count>248</Count> 
<Explode>N</Explode> 
</TermSet> 
<OP>GROUP</OP> 
</TranslationStack> 
<QueryTranslation>ACTN3[All Fields]</QueryTranslation> 
</eSearchResult> 

而不是我的代码返回

print_r:Array () 
vardump:array(0) { } 

如何检索XML(例如,从https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ACTN3)并打印出来?

回答

1

是的,它返回的XML,但问题是要附加字符串下面object.Try:

$file_loc="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ACTN3"; 

$raw_xml = simplexml_load_file($file_loc) or die("Error: Cannot create object"); 
echo "<pre>"; 
print_r($raw_xml); 
+0

没错,就是做到了。谢谢 – haz