problems displaying json data retrieved using php curl -
first, reading. here code
what json data looks when viewed in browser using url,
{ "$totalresults": 1500, "$startindex": 1, "$itemsperpage": 3, "$resources": [ { "$uuid": "5e7b9312-52e5-4fe1-b3e4-633ca04c9764", "$httpstatus": "ok", "$descriptor": "", "name": "heinz tomato sauce sachets qty 200" }, { "$uuid": "1a0f9dca-c417-4cff-94d2-99d16438723f", "$httpstatus": "ok", "$descriptor": "", "name": "heinz brown sauce sachet qty 200" }, { "$uuid": "126fdb17-81ce-41b4-bdba-91d0a170262a", "$httpstatus": "ok", "$descriptor": "", "name": "heinz english mustard sachets qty 300" } ] }
test2.php
<?php // sage url $url = 'http://localhost:5493/sdata/accounts50/gcrm/-/commodities?select=name&count=3&format=json'; //sage username & password $username = 'test'; $password = 'test'; //initiaize $ch = curl_init(); //set options curl_setopt($ch, curlopt_url,$url); curl_setopt($ch, curlopt_userpwd, $username . ":" . $password); curl_setopt($ch, curlopt_httpget, true); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_header, 0); //execute $result = curl_exec($ch); //close curl session / free resources curl_close($ch); $json = json_decode($result); foreach ($json->{'$resources'} $mydata) { echo $mydata->name; }; ?>
errors im getting
notice: trying property of non-object in c:\xampp\htdocs\sage json\test2.php on line 29 warning: invalid argument supplied foreach() in c:\xampp\htdocs\sage json\test2.php on line 29
i’m not sure what's going wrong, works if same data file not if i’m retrieving @ rest server, , if shed light grateful
ajking11 reason getting invalid argument supplied foreach() $json vairiable overwritten , has no data try somthing this
<?php // sage url $url = 'http://localhost:5493/sdata/accounts50/gcrm/-/commodities?select=name&count=3'; //sage username & password $username = 'test'; $password = 'test'; //initiaize $ch = curl_init(); //set options curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_userpwd, $username . ":" . $password); curl_setopt($ch, curlopt_httpget, true); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_header, 0); //execute $result = curl_exec($ch); //close curl session / free resources curl_close($ch); $xml = simplexml_load_string($result); $data = json_encode($xml); $arr = json_decode($data,true); foreach ($arr['entry'] $k=>$v){ echo $v; // etc. }
note have removed &format=json , encoded later hope let me know how getting on
Comments
Post a Comment