帮助从 php 函数回显数组

发布于 2024-11-07 11:12:49 字数 1706 浏览 4 评论 0原文

好吧,我仍在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

維他命╮ 2024-11-14 11:12:49

您是否尝试过:

print_r($define);

而不是 echo

Have you tried:

print_r($define);

instead of echo ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文