How to call REST API using CURL in php?
The issue is because you are accesing quote and author incorrectly.
Output of print_r($json_objekat)
says that:
contents is stdClass Object
quotes is array
again quotes is having 0 as index which is again stdClass Object
So, try accessing quote and author as follows:
$json_objekat->contents->quotes[0]->quote
$json_objekat->contents->quotes[0]->author
Svetlana
Updated on February 28, 2020Comments
-
Svetlana about 4 years
I have problem when I am trying to call API using curl. My php code looks like this:
<html> <head> <body> <span style="z-index:50;font-size:0.9em;"> <img src="https://theysaidso.com/branding/theysaidso.png" height="20" width="20" alt="theysaidso.com"/> <a href="https://theysaidso.com" title="Powered by quotes from theysaidso.com" style="color: #9fcc25; margin-left: 4px; vertical-align: middle;"> theysaidso.com</a></span> <?php $service_url = 'http://quotes.rest/qod.json'; $curl = curl_init($service_url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $curl_response = curl_exec($curl); curl_close($curl); $json_objekat=json_decode($curl_response); echo $json_objekat->contents->quotes->quote.'<br>'; echo $json_objekat->contents->quotes->author.'<br>'; ?> </body> </head>
This code is saved as php file in my root directory on WAMP server. I use WAMP server. And it is written on this website http://quotes.rest/qod.json that you must insert this html too. When I open this php page on WAMP it shows alert:
Notice: Undefined property: stdClass::$contents in C:\wamp\www\IzdavackaKuca\javniServis.php on line 18
Notice: Trying to get property of non-object in C:\wamp\www\IzdavackaKuca\javniServis.php on line 18
Notice: Trying to get property of non-object in C:\wamp\www\IzdavackaKuca\javniServis.php on line 18
Notice: Undefined property: stdClass::$contents in C:\wamp\www\IzdavackaKuca\javniServis.php on line 19
Notice: Trying to get property of non-object in C:\wamp\www\IzdavackaKuca\javniServis.php on line 19
Notice: Trying to get property of non-object in C:\wamp\www\IzdavackaKuca\javniServis.php on line 19
JSON on this link http://quotes.rest/qod.json looks like this:
{ "success": { "total": 1 }, "contents": { "quotes": [ { "quote": "A loser doesn't know what he'll do if he loses, but talks about what he'll do if he wins, and a winner doesn't talk about what he'll do if he wins, but knows what he'll do if he loses.", "length": "184", "author": null, "tags": [ "failure", "inspire", "knowledge", "winning" ], "category": "inspire", "date": "2016-05-11", "title": "Inspiring Quote of the day", "background": "https://theysaidso.com/img/bgs/man_on_the_mountain.jpg", "id": "KhjRMynny89MKxcGkEKF_QeF" } ] } }
When I change last two lines in php to this:
echo $json_objekat['contents']['quotes']['quote'].'<br>'; echo $json_objekat['contents']['quotes']['author'].'<br>';
it alerts:
Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\IzdavackaKuca\javniServis.php on line 18
I realy don`t know how to call this server, when I code:
<?php $defaults = array( CURLOPT_URL => 'http://quotes.rest/qod.json', CURLOPT_POST => false, CURLOPT_HEADER => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYHOST => false, ); $curl = curl_init(); curl_setopt_array($curl, $defaults); $curl_response = curl_exec($curl); $json_objekat = json_decode($curl_response); // DUMP THE CURL-ERROR INFORMATION: var_dump(curl_error($curl)); curl_close($curl); ?>
it alerts:
string '' (length=0)
Please help!
-
Svetlana about 8 yearsThis works also just like this answer from Suyog. Thank you so muck!
-
Svetlana about 8 yearsJust this bracket after $quotes = $json_objekat->contents->quotes is redundant. Thats mistake, I know...