我正在尝试读取 .php 中的 SimpleXMLElement 以补充搜索功能。我通过请求从一个名为 openthesaurus.de 的德语同义词 API 获取 XML file_get_contents。然后,我将收到的元素转换为 SimpleXMLElement 并尝试读取每个元素的属性term。(我知道下面的代码只关注synset['0'])。这样做是为了保持简单。
我的 .php 看起来像这样
$url = "https://www.openthesaurus.de/synonyme/search?q=". $keyword. "&format=text/xml";
$options = array('http' =>
array(
'method' => 'POST',
'header' => 'user_agent: my-site.xyz',
)
);
$context = stream_context_create($options);
$recive = file_get_contents($url, false, $context);
if (($recieve = file_get_contents($url))===false){
echo "Error fetching XML\n";
} else {
libxml_use_internal_errors(true);
$syn = new SimpleXMLElement($recieve);
if (!$syn) {
echo "Error loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
} else {
echo "content: <br>";
foreach($syn->synset[0]->term as $value){
echo $value. "<br>";
}
}
}
xml 看起来像这样:
<matches>
<metaData>
<apiVersion content="0.1.3"/>
<warning content="WARNING -- this XML format may be extended without warning"/>
<copyright content="Copyright (C) 2019 Daniel Naber (www.danielnaber.de)"/>
<license content="Creative Commons Attribution-ShareAlike 4.0 or GNU LESSER GENERAL PUBLIC LICENSE Version 2.1"/>
<source content="https://www.openthesaurus.de"/>
<date content="Sat Jun 27 09:48:33 CEST 2020"/>
</metaData>
不幸的是,除了中断之外,.php 没有输出任何内容。我也尝试过通过 print_r 输出,但这也没有帮助。但是 if 条件表明empty($value)该条件$value不为空。
慕莱坞森