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>';

  }
?>
3
  • 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 Commented Dec 10, 2019 at 5:19
  • Dont post your original key here, and you dont need to loop through result Commented Dec 10, 2019 at 5: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. Commented Dec 10, 2019 at 5:25

3 Answers 3

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

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

Comments

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.

2 Comments

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...
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 stackoverflow.com/questions/31285360/…
1

It might be useful if you add

curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false); 

after

curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);

2 Comments

Because the API is HTTPS, this option allows you to skip the validation of the peer's certificate and get the $result.
Thank u buddy.That's a great suggestion.

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.