Trying to parse a JSON API array result using PHP / CURL. Can retrieve the JSON without an issue but finding quite difficult to parse any data I want from the returned JSON. Using the below PHP code.
<?php
$url = "https://openlibrary.org/search.json?q=green+eggs+and+ham&page=1";
$headers = array(
"Content-type: application/json;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\""
);
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($cURL);
//print_r($result);
foreach (json_decode($result, true) as $book) {
echo '<p><h3>';
echo 'title : '.$book['docs']['title_suggest'];
echo 'author : '.$book['docs']['author_name'].'<br />';
echo 'pubdate : '.$book['docs]['publish_date'].'<br />';
echo 'isbn: '.$book['docs']['ia'][0].'<br />';
echo '</h3></p>';
}
curl_close($cURL);
?>
Help much appreciated! Cheers!