Looping through JSON with PHP to get children -


i've been trying morning appear quite simple , i'm failing miserably.

i have api request returns valid json data , need loop through various values in php several nodes application.

here snippet of json

{ "results": [     {         "date": "2015-06-01",         "rates": [             {                 "id": 1592,                 "name": "weekend promotion",                 "room_rates": [                     {                         "room_type_id": 66,                         "room_type_code": "dlk",                         "sold": 0,                         "sell_limit": null,                         "availability": 25,                         "out_of_order": 0,                         "single": 85,                         "double": 85,                         "extra_adult": null,                         "child": null                     },                     {                         "room_type_id": 90,                         "room_type_code": "dlt",                         "sold": 0,                         "sell_limit": null,                         "availability": 11,                         "out_of_order": 0,                         "single": 85,                         "double": 85,                         "extra_adult": null,                         "child": null                     }, 

and i'm trying do, keep getting following error (edited update curl response):

notice: trying property of non-object in /vagrant/web/web/bookingstep2.php on line 135  warning: invalid argument supplied foreach() in /vagrant/web/web/bookingstep2.php on line 135  notice: trying property of non-object in /vagrant/web/web/bookingstep2.php on line 135  warning: invalid argument supplied foreach() in /vagrant/web/web/bookingstep2.php on line 135 

any ideas i'm doing wrong? line 135 this:

foreach($responsedata $mydata)

$url = 'https://myapi.com/availability?token=xx&from_date='.$from_date.'&to_date='.$to_date;  $options = array(     curlopt_returntransfer => true,   // return web page     curlopt_header         => false,  // don't return headers     curlopt_followlocation => true,   // follow redirects     curlopt_maxredirs      => 10,     // stop after 10 redirects     curlopt_encoding       => "",     // handle compressed     curlopt_useragent      => "test", // name of client     curlopt_autoreferer    => true,   // set referrer on redirect     curlopt_connecttimeout => 120,    // time-out on connect     curlopt_timeout        => 120,    // time-out on response );  $ch = curl_init($url); curl_setopt_array($ch, $options); $result = curl_exec($ch);  curl_close($ch);   // decode response $responsedata = json_decode($result, true);  // put everyting screen var_dump; var_dump($responsedata);  // print_r ( useful arrays ); //print_r($responsedata);  // list review ratings foreach; foreach($responsedata $mydata) {     foreach($mydata->results $values) {         echo $values->rates . "\n";     }  } 

i need able return following nodes....

results>rates>name results>rates>room_rates>room_type_id results>rates>room_rates>availability 

i'm stuck!

the var_dump return array, curl working correctly now.

simon

it looks curl request blame, you're not telling curl you're expecting response, curl passes boolean success initial request, sent successfully.

from php curl_exec page.

returns true on success or false on failure. however, if curlopt_returntransfer option set, return result on success, false on failure.

it's succeeding, you're not telling you're expecting response, returns true.

try this:

$url = 'https://myapi.com/api?token=xxx&from_date='.$from_date.'&to_date='.$to_date;  $options = array(     curlopt_returntransfer => true,   // return web page <-- important 1 tells curl want response.     curlopt_header         => false,  // don't return headers     curlopt_followlocation => true,   // follow redirects     curlopt_maxredirs      => 10,     // stop after 10 redirects     curlopt_autoreferer    => true,   // set referrer on redirect     curlopt_connecttimeout => 120,    // time-out on connect     curlopt_timeout        => 120,    // time-out on response );  $ch = curl_init($url); curl_setopt_array($ch, $options); $result = curl_exec($ch);  curl_close($ch); 

2nd error

now we've got curl working lets address result. remember when json converted each brace ({) produce object, , each square bracket ([) produce array. if follow json can see object back, containing property results. can access using $responsedata->results.

in second foreach you're trying access property of object. results isn't object, it's array (look @ json, contains [, it's array). rates follow through json string , you'll see need this:

foreach($responsedata->results[0]->rates $rate) {     echo $rate->id . "\n"; } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -