phpjsonduckduckgo-api

How can I get search results based on DuckDuckGo API?


I want to decode json file resulted from DuckDuckGo API into a readeble html or PHP string.

I try with PHP json_decode, but nothing:

$object = json_decode($string, true);
echo $object['RelatedTopics']['Result'];

Any ideas?


Solution

  • From the JSON responde you posted, it is possible to see that RelatedTopics is an array. So you must first access an element of this array to then access the Result key:

    echo $object['RelatedTopics'][0]['Result'];
    

    Or in a loop just to test:

    foreach ($object['RelatedTopics'] as $rel)
        echo $rel['Result'];