I’m trying to display information from an xml file. It doesn’t gives me error, but the array is empty.
I am using wordrpess and I have not much experience with php, so, i don’t know if this is the best way to do.
This is my code:
<?php
function pubmedQuery() {
$xml = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[journal]+AND+breast+cancer+AND+2008[pdat]';
$xml_file = simplexml_load_file( $xml );
$results_count = $xml_file->Count;
$results_ids = array();
foreach ( $xml_file->IdList->Id as $items ) {
$results_ids[] = $items;
}
return "Hay " . $results_count . " resultados: " . $results_ids;
}
//Show results
echo'<h3>Resultados de búsqueda:</h3>' . pubmedQuery ();
?>
And this is the result:
Resultados de búsqueda:
Hay 0 resultados: Array
thanks! and excuse my english!
@Gavin is right. However, you can get the content by
file_get_contents
:Outputs
Notice
implode("n",$results_ids)
which returns a string with the found id’s, instead of returning the text array, regardless if there is found id’s or not.As per my comment, the website you are scraping from appears to have user-agent detection.
The above code will spoof the user-agent for the
file_get_contents
call so the website will think it’s a normal browser.