0

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!

2
  • please format your code, it only takes a minute and makes answering your question much easier. Commented May 15, 2011 at 3:24
  • My bad I forgot to do that. Thanks for the heads up Commented May 15, 2011 at 3:31

1 Answer 1

5

Have you tried:

print_r($define);

instead of echo ?

Sign up to request clarification or add additional context in comments.

5 Comments

That was exactly what I`m gonna say :)
ah good call, I had previously tried print_r but had set it up wrong. That worked but I know print_r is more of a debugging command, any way to get a result that looks a little nicer than this? eg. just the definition of test?
@jangoforhire sounds like what you want to echo is one of the elements of the array - after you've dumped out the array, look at the keys, and try echoing that. Something like echo $define['definition'];
Actually, you should probably just return the definition in your function if that is all you are looking for. No need to allocate an array if you don't need to :)
got it I was just looking at that now. Thanks very much for your help! Answer marked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.