在wordpress中显示xml的php语法
我试图显示xml文件中的信息。它没有给我错误,但是数组是空的。 我正在使用wordrpess,我对php没有太多经验,所以,我不知道这是否是最好的方法 这是我的代码:在wordpress中显示xml的php语法,php,xml,wordpress,Php,Xml,Wordpress,我试图显示xml文件中的信息。它没有给我错误,但是数组是空的。 我正在使用wordrpess,我对php没有太多经验,所以,我不知道这是否是最好的方法 这是我的代码: <?php function pubmedQuery() { $xml = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[journal]+AND+breast+cancer+AND+20
<?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 ();
?>
这就是结果:
búsqueda结果:
Hay 0 resultados:数组
谢谢!请原谅我的英语 根据我的评论,您正在从中抓取的网站似乎有用户代理检测
function pubmedQuery() {
$context = stream_context_create(array(
'http'=>array(
'user_agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'
)
));
$xml = file_get_contents('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[journal]+AND+breast+cancer+AND+2008[pdat]', FALSE, $context);
$xml_file = simplexml_load_string($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 ();
函数pubmedQuery(){
$context=stream\u context\u create(数组(
“http'=>数组(
“用户代理”=>“Mozilla/5.0(Windows NT 5.1)AppleWebKit/536.11(KHTML,如Gecko)Chrome/20.0.1132.57 Safari/536.11”
)
));
$xml=文件\u获取\u内容('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[期刊]+和+乳腺癌+和+2008[pdat]',假,$context);
$xml\u file=simplexml\u load\u字符串($xml);
$results\u count=$xml\u file->count;
$results_id=array();
foreach($xml\u file->IdList->Id as$items){
$results\u id[]=$items;
}
返回“Hay”。$results\u count.“resultados:”.$results\u id;
}
//显示结果
echo'Resultados de búsqueda:'。pubmedQuery();
上面的代码将欺骗用户代理以获取
文件\u get\u内容
呼叫,因此网站将认为它是一个普通浏览器。根据我的评论,您正在从中删除的网站似乎有用户代理检测
function pubmedQuery() {
$context = stream_context_create(array(
'http'=>array(
'user_agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'
)
));
$xml = file_get_contents('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[journal]+AND+breast+cancer+AND+2008[pdat]', FALSE, $context);
$xml_file = simplexml_load_string($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 ();
函数pubmedQuery(){
$context=stream\u context\u create(数组(
“http'=>数组(
“用户代理”=>“Mozilla/5.0(Windows NT 5.1)AppleWebKit/536.11(KHTML,如Gecko)Chrome/20.0.1132.57 Safari/536.11”
)
));
$xml=文件\u获取\u内容('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[期刊]+和+乳腺癌+和+2008[pdat]',假,$context);
$xml\u file=simplexml\u load\u字符串($xml);
$results\u count=$xml\u file->count;
$results_id=array();
foreach($xml\u file->IdList->Id as$items){
$results\u id[]=$items;
}
返回“Hay”。$results\u count.“resultados:”.$results\u id;
}
//显示结果
echo'Resultados de búsqueda:'。pubmedQuery();
上述代码将欺骗用户代理以获取
文件内容
调用,因此网站会认为它是正常的浏览器。@Gavin是正确的。但是,您可以通过文件获取内容:
function pubmedQuery() {
$xml = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[journal]+AND+breast+cancer+AND+2008[pdat]';
$content = file_get_contents($xml);
$xml_file = simplexml_load_string($content);
$results_count = $xml_file->Count;
$results_ids = array();
foreach ( $xml_file->IdList->Id as $items ) {
$results_ids[] = $items;
}
return "Hay " . $results_count . " resultados: " . implode("\n",$results_ids);
}
//Show results
echo'<h3>Resultados de búsqueda:</h3>' . pubmedQuery ();
函数pubmedQuery(){
$xml='0http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[期刊]+和+乳腺癌+和+2008[pdat];
$content=file\u get\u contents($xml);
$xml\u file=simplexml\u load\u字符串($content);
$results\u count=$xml\u file->count;
$results_id=array();
foreach($xml\u file->IdList->Id as$items){
$results\u id[]=$items;
}
返回“Hay”。$results\u count.“resultados:”.intlode(“\n”,$results\u id);
}
//显示结果
echo'Resultados de búsqueda:'。pubmedQuery();
输出
结果:19008416 18927361 18787170 18487186 18239126
18239125
请注意内爆(“\n”,$results\u id)
,它返回具有找到的id的字符串,而不是返回文本数组,无论是否存在找到的id 加文是对的。但是,您可以通过文件获取内容:
function pubmedQuery() {
$xml = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[journal]+AND+breast+cancer+AND+2008[pdat]';
$content = file_get_contents($xml);
$xml_file = simplexml_load_string($content);
$results_count = $xml_file->Count;
$results_ids = array();
foreach ( $xml_file->IdList->Id as $items ) {
$results_ids[] = $items;
}
return "Hay " . $results_count . " resultados: " . implode("\n",$results_ids);
}
//Show results
echo'<h3>Resultados de búsqueda:</h3>' . pubmedQuery ();
函数pubmedQuery(){
$xml='0http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=science[期刊]+和+乳腺癌+和+2008[pdat];
$content=file\u get\u contents($xml);
$xml\u file=simplexml\u load\u字符串($content);
$results\u count=$xml\u file->count;
$results_id=array();
foreach($xml\u file->IdList->Id as$items){
$results\u id[]=$items;
}
返回“Hay”。$results\u count.“resultados:”.intlode(“\n”,$results\u id);
}
//显示结果
echo'Resultados de búsqueda:'。pubmedQuery();
输出
结果:19008416 18927361 18787170 18487186 18239126
18239125
请注意内爆(“\n”,$results\u id)
,它返回具有找到的id的字符串,而不是返回文本数组,无论是否存在找到的id 返回的XML实际上不包含任何结果,但是在浏览XML url时,它确实包含任何结果。我怀疑承载内容的服务器正在检测并阻止刮取?返回的XML实际上不包含任何结果,但是在浏览XML url时,它确实包含任何结果。我怀疑承载内容的服务器正在检测并阻止刮取?谢谢!两个答案都很好。但我不理解用户代理检测问题。我必须了解一下。很可能服务器需要用户代理才能正常工作,但也可能是他们故意阻止您查看结果,除非您的浏览器是有效的。在大多数情况下,file\u get\u contents、simplexml\u load\u file等都会在不使用用户代理的情况下发送HTTP请求,因此,欺骗者会告知您正在使用的服务器,例如chrome或firefox。谢谢!两个答案都很好。但我不理解用户代理检测问题。我必须了解一下。很可能服务器需要用户代理才能正常工作,但也可能是他们故意阻止您查看结果,除非您的浏览器是有效的。在大多数情况下,file\u get\u contents、simplexml\u load\u file等都会在不使用用户代理的情况下发送HTTP请求,因此,欺骗者会告知您正在使用的服务器,例如chrome或firefox。嗯。