帮助从 php 函数回显数组
好吧,我仍在学习 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”,它将回显“空白”我无法弄清楚为什么它只回显“数组”而不是数组内的数据。可能是一些愚蠢的事情,但我已经阅读数组有一段时间了,但找不到任何信息。任何帮助将非常感谢!
Alright I am still learning my functions in php but this particular piece of code has me stuck. Credit goes to http://www.barattalo.it/2010/08/29/php-bot-to-get-wikipedia-definitions/ for the wikipedia query segment. I thought this looked interesting and am trying to use the code provided to echo data from the function. This is what I have:
<?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;
?>
however this simply echos "array"
I know the code is getting to the if/else statement because if you change the input in "$define = wikidefinition("test");" to some random key combination such as "qoigenqnge" it will echo "blank" I cannot figure out why it is only echoing "array" instead of the data inside the array. Probably something stupid, but I have been reading up on arrays for a while and cant find any info. Any help will be great thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过:
而不是
echo
?Have you tried:
instead of
echo
?