【发布时间】:2011-05-15 03:20:00
【问题描述】:
好吧,我仍在学习我的 php 函数,但这段特殊的代码让我陷入了困境。维基百科查询部分的信用转到http://www.barattalo.it/2010/08/29/php-bot-to-get-wikipedia-definitions/。我认为这看起来很有趣,并试图使用提供的代码来回显函数中的数据。这就是我所拥有的:
<?php
function wikidefinition($s) {
$url = "http://wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=1";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
$page = curl_exec($ch);
$xml = simplexml_load_string($page);
if((string)$xml->Section->Item->Description) {
return array((string)$xml->Section->Item->Text, (string)$xml->Section->Item->Description, (string)$xml->Section->Item->Url);
} else {
return "blank";
}
}
$define = wikidefinition("test");
echo $define;
?>
然而,这只是回显“数组” 我知道代码正在进入 if/else 语句,因为如果您更改 "$define = wikidefinition("test");" 中的输入对于一些随机键组合,例如“qoigenqnge”,它会回显“空白”我无法弄清楚为什么它只回显“数组”而不是数组内的数据。可能是一些愚蠢的事情,但我已经阅读了一段时间的数组并且找不到任何信息。任何帮助都会非常感谢!
【问题讨论】:
-
请格式化您的代码,只需一分钟,让您的问题更容易回答。
-
我的错,我忘了这样做。感谢您的提醒