0

I have this API

https://en.wikipedia.org/w/api.php?action=query&titles=Bruno%20Mars&prop=pageimages&format=json&piprop=original

when I try to fetch data from the API I get this error

Recoverable fatal error: Object of class stdClass could not be converted to a string

Here's my code

$url = "https://en.wikipedia.org/w/api.php?action=query&titles=Bruno%20Mars&prop=pageimages&format=json&piprop=original";
                $cSession = curl_init();
                curl_setopt($cSession,CURLOPT_URL, $url);
                curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($cSession,CURLOPT_HEADER, false);
                curl_setopt($cSession, CURLOPT_HTTPHEADER, array('User-Agent:http://localhost:8888/aweimer/'));

                if (curl_errno($cSession)) {
                print "Error: " . curl_error($cSession);
                exit();
                }

                $result = curl_exec($cSession);
                $jsonData = json_decode($result);
                echo $source = $jsonData->original->source;
  • As I see the response from the link you gave, there is no `original` key at the first level. There is only `batchcomplete` and `query` so your `$jsonData->original` is nothing else than `null` and therefore your code doesn't like `null->source`. – Altherius Jul 15 '20 at 14:52
  • replace the code with ** $jsonData = json_decode($result); **. ** echo $source = $jsonData->original->source; ** , This to my code and get the image path ** $jsonData = json_decode($result,true); ** . ** print_r($jsonData['query']['pages']['27005455']['original']['source']); ** – Rohit Jadhav Jul 15 '20 at 15:43

0 Answers0