1

I am working on the BooksRun API, and I can not display any data returned from the API using a PHP foreach. My code is below. Thanks in advance!

BooksRun API Reference

<?php
//PARAMETERS
$url = 'https://booksrun.com/api/v3/price/buy/0134093410?key=0t8rfbno7qc4lmaav9yz';

$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);

echo '<div>' . 'start : '.$obj_data->result->status . '</div>';
//echo '<div>' . 'date: '.$obj_data->result->offers->booksrun->new->price . '</div>';
//echo '<div>' . 'date: '.$obj_data->result->marketplace->used->price . '</div>';

foreach ($obj_data->result as $book) {
//fetch object data

echo '<div>' . 'seller: '.$book->offers->booksrun->rent->price . '</div>';
echo '<div>' . 'date: '.$book->marketplace->used->price . '</div>';

  }
?>
dennisgon
  • 337
  • 3
  • 14
Dango
  • 149
  • 2
  • 14
  • 1
    did you checked value of `$obj_data` ? anything coming in that? do `var_dump($obj_data)`; and check. Let us know what you got?Possibility is you are getting nothing – Alive to die - Anant Dec 10 '19 at 05:19
  • Dont post your original key here, and you dont need to loop through `result ` – Devsi Odedra Dec 10 '19 at 05:23
  • There's a key `"35"` after `"rent"`, so it should be `->rent->{"35"}->price`. I don't know what that 3t5 is, or if it changes dynamically. – Barmar Dec 10 '19 at 05:25

3 Answers3

1

Just make the following changes :

echo '<div>' . 'seller: '.$obj_data->result->offers->booksrun->rent->{"35"}->price . '</div>';
echo '<div>' . 'date: '.$obj_data->result->offers->marketplace[0]->used->price . '</div>';

$obj_data->result->offers->marketplace is an array so you can loop over this.

There is no need for looping over the result object as it only has one item in it.

Prabhjot Singh Kainth
  • 1,831
  • 2
  • 18
  • 26
  • What if the JSON dataset is sometimes a single result and sometimes and array? Also it seems to me the API dynamically displays the {"35"}, so the number changes all the time... kinda seems stupid to me, but they must have a reason for it... – Dango Dec 10 '19 at 05:40
  • If dataset returns multiple values then you will have to use foreach loop. Also if you are looking for dynamic key parsing then kindly refer to this https://stackoverflow.com/questions/31285360/how-to-parse-json-in-javascript-having-dynamic-key-value-pair – Prabhjot Singh Kainth Dec 10 '19 at 05:43
1

It might be useful if you add

curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false); 

after

curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
Lopes Wang
  • 34
  • 2
1

i think you need change the code because there is an array in json maybe you can change into this code

<?php
//PARAMETERS
$url = 'https://booksrun.com/api/v3/price/buy/0134093410?key=0t8rfbno7qc4lmaav9yz';

$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);
echo '<div>' . 'start : '.$obj_data->result->status . '</div>';
//echo '<div>' . 'date: '.$obj_data->result->offers->booksrun->new->price . '</div>';
//echo '<div>' . 'date: '.$obj_data->result->marketplace->used->price . '</div>';

//fetch object data
foreach ($obj_data->result->offers->booksrun->rent as $rent) {
    echo '<div>' . 'seller: '.$rent->price . '</div>';
}
foreach ($obj_data->result->offers->marketplace as $marketplace) {
    echo '<div>' . 'date: '.$marketplace->used->price. '</div>';
}
?>

the problem in your code is you want to open an object but actualy it's an array

dennisgon
  • 337
  • 3
  • 14