0

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!

4
  • We can't help without seeing the JSON you are trying to decode... Commented Nov 26, 2019 at 5:18
  • Or why you thought the outmost layer was an array. Commented Nov 26, 2019 at 5:19
  • PD of How do I extract data from JSON with PHP? Commented Nov 26, 2019 at 5:20
  • Or dump ouput of JSON or Array and write what difficulties you are facing? Also write your desire output to help you better. Commented Nov 26, 2019 at 5:27

1 Answer 1

1
  1. Decode the JSON data 1st $obj_data = json_decode($result);
  2. Close the Curl when the operation is complete.
  3. For best practice receive data till "docs" and loop over it. This will help you!

     
    $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); curl_close($cURL); //Json Data decoded on PHP object $obj_data = json_decode($result); foreach ($obj_data->docs as $book) { //fetch object data echo 'title : '.$book->title_suggest; echo 'author : '.$book->author_name[0]; echo 'pubdate : '.$book->publish_date; echo 'isbn: '.$book->ia[0]; }

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

1 Comment

the JSON wanting to parse is the following -> openlibrary.org/search.json?q=the+lord+of+the+rings&page=2

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.