i am trying to get data from:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
but if i want to get player_name variable with this code:
<?
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js);
var_dump($pjs->{'player_name'});
?>
i get error:
Notice: Trying to get property of non-object in **\htdocs\index.php on
line 9 + var_dump() returns: NULL
var_dump($pjs) returns:
array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) ["COACH"]=> int(4) ["LIMO"]=> int(97) ["ARRESTS"]=> int(272) ["GTA"]=> int(67) ["BURGLAR"]=> int(122) ["HEIST"]=> int(1) ["PLANE"]=> int(48) ["HELI"]=> int(12) ["FAILED"]=> int(312) ["OVERLOADS"]=> int(160) ["TRUCK_LOADS"]=> int(1275) ["ODOMETER"]=> int(28320798) ["TIME"]=> int(2078450) } ["achievements"]=> array(4) { [0]=> string(20) "Professional Trucker" [1]=> string(13) "Gravel Hauler" [2]=> string(12) "Delivery Boy" [3]=> string(7) "Wrecker" } } }
This is because $pjs is an one-element-array of objects, so first you should access the array element, which is an object and then access its attributes.
echo $pjs[0]->player_name;
Actually dump result that you pasted tells it very clearly.
The response is an array.
var_dump($pjs[0]->{'player_name'});
#Balamanigandan your Original Post :- PHP Notice: Trying to get property of non-object error
Your are trying to access the Null Object. From AngularJS your are not passing any Objects instead you are passing the $_GET element. Try by using $_GET['uid'] instead of $objData->token
Related
I'm getting a json response and have converted into array using json_decode.
var_dump gives the following:
array(1) {
["FlightInfoResult"]=>
array(2) {
["next_offset"]=>
int(1)
["flights"]=>
array(1) {
[0]=>
array(19) {
["ident"]=>
string(6) "AXB443"
["aircrafttype"]=>
string(4) "B738"
["filed_ete"]=>
string(8) "03:10:00"
["filed_time"]=>
int(1498358100)
["filed_departuretime"]=>
int(1498530900)
["filed_airspeed_kts"]=>
int(376)
["filed_airspeed_mach"]=>
string(0) ""
["filed_altitude"]=>
int(0)
["route"]=>
string(0) ""
["actualdeparturetime"]=>
int(0)
["estimatedarrivaltime"]=>
int(1498542900)
["actualarrivaltime"]=>
int(0)
["diverted"]=>
string(0) ""
["origin"]=>
string(4) "VOCI"
["destination"]=>
string(4) "OOMS"
["originName"]=>
string(26) "Cochin Int'l (Kochi Int'l)"
["originCity"]=>
string(20) "Kochi / Nedumbassery"
["destinationName"]=>
string(10) "Seeb Int'l"
["destinationCity"]=>
string(6) "Muscat"
}
}
}
}
I need to get the contents that comes within flights, ident:AXB443, etc..
I am not able to print any elements of the array.
This is multidimensional array. After json_decode you can get it via $variable['FlightInfoResult']['flights'][0]['ident'];
An variant on var_dump() is print_r(). This will give a simpeler, but in this case better to understand output.
i am trying to get data from:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
but if i want to get player_name variable with this code:
<?
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js);
var_dump($pjs->{'player_name'});
?>
i get error:
Notice: Trying to get property of non-object in **\htdocs\index.php on
line 9 + var_dump() returns: NULL
var_dump($pjs) returns:
array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) ["COACH"]=> int(4) ["LIMO"]=> int(97) ["ARRESTS"]=> int(272) ["GTA"]=> int(67) ["BURGLAR"]=> int(122) ["HEIST"]=> int(1) ["PLANE"]=> int(48) ["HELI"]=> int(12) ["FAILED"]=> int(312) ["OVERLOADS"]=> int(160) ["TRUCK_LOADS"]=> int(1275) ["ODOMETER"]=> int(28320798) ["TIME"]=> int(2078450) } ["achievements"]=> array(4) { [0]=> string(20) "Professional Trucker" [1]=> string(13) "Gravel Hauler" [2]=> string(12) "Delivery Boy" [3]=> string(7) "Wrecker" } } }
This is because $pjs is an one-element-array of objects, so first you should access the array element, which is an object and then access its attributes.
echo $pjs[0]->player_name;
Actually dump result that you pasted tells it very clearly.
The response is an array.
var_dump($pjs[0]->{'player_name'});
#Balamanigandan your Original Post :- PHP Notice: Trying to get property of non-object error
Your are trying to access the Null Object. From AngularJS your are not passing any Objects instead you are passing the $_GET element. Try by using $_GET['uid'] instead of $objData->token
Hello awesome programmers!
I am so sorry for the the novice question, however, I am having trouble finding a solution to my problem. I am attempting to run a for each loop through my array passed from my controller, however, the data being outputted is not the same as when I run a var_dump($array). I am thinking perhaps that I need to iterate through this object maybe? However, when I attempt to do so, I get a non-object error.
Controller:
$data['user_details'] = $this->ion_auth->user()->row();
View:
var_dump($user_details);
foreach($user_details as $item){
echo $item['email'];
}
The output of this is : "21nfn4111NR12" but should be roger#peterson.net!
I have also tried the object form:
var_dump($user_details);
foreach($user_details as $item){
echo $item->email;
}
However, it results in error trying to get property of non-object!
When I run the var dump I get the following:
object(stdClass)#21 (17) {
["id"]=>
string(1) "2"
["ip_address"]=>
string(14) "119.132.127.01"
["username"]=>
string(12) "roger petereson"
["password"]=>
string(40) "fdZxF/RQo4nZKmbA5XQlwefbc8f8e5c74899c3d0"
["salt"]=>
NULL
["email"]=>
string(19) "roger#peterson.net"
["activation_code"]=>
NULL
["forgotten_password_code"]=>
NULL
["forgotten_password_time"]=>
NULL
["remember_code"]=>
string(22) "44hjOlloLTIrkSrjSBVNie"
["created_on"]=>
string(10) "1404939094"
["last_login"]=>
string(10) "1405099607"
["active"]=>
string(1) "1"
["first_name"]=>
string(6) "Roger"
["last_name"]=>
string(5) "Peterson"
["is_owner"]=>
string(1) "1"
["user_id"]=>
string(1) "2"
}
The following line:
$this->ion_auth->user()->row();
returns an object not an array (check your var_dump output), so you just need to
echo $user_details->email;
I have some JSON that I am trying to decode -- it is correctly extracting the value, but also generating an error.
Here is the var_dump:
["success"]=>
bool(true)
["providers"]=>
array(2) {
[0]=>
object(stdClass)#5 (15) {
["address1"]=>
string(14) "3240 W Lake St"
["address2"]=>
NULL
["city"]=>
string(11) "Minneapolis"
["crossStreet"]=>
string(26) "Lake Street & Market Plaza"
["description"]=>
string(55) "test location28402 description of services/prices/hours"
["distance"]=>
float(0.42900015862223)
["lat"]=>
float(44.948469)
["lon"]=>
float(-93.321155)
["name"]=>
string(17) "testlocation28402"
["phone"]=>
string(10) "6125551212"
["precise"]=>
bool(true)
["state"]=>
string(2) "MN"
["url"]=>
string(41) "http://www.testlocation28402.com?id=28402"
["urlCaption"]=>
string(25) "www.testlocation28402.com"
["zip"]=>
string(9) "554164512"
}
[1]=>
object(stdClass)#6 (15) {
["address1"]=>
string(19) "4335 Excelsior Blvd"
["address2"]=>
NULL
["city"]=>
string(16) "Saint Louis Park"
["crossStreet"]=>
NULL
["description"]=>
string(55) "test location26358 description of services/prices/hours"
["distance"]=>
float(0.91979730006713)
["lat"]=>
float(44.935773)
["lon"]=>
float(-93.33489)
["name"]=>
string(17) "testlocation26358"
["phone"]=>
string(10) "6125551212"
["precise"]=>
bool(true)
["state"]=>
string(2) "MN"
["url"]=>
string(41) "http://www.testlocation26358.com?id=26358"
["urlCaption"]=>
string(25) "www.testlocation26358.com"
["zip"]=>
string(9) "554164811"
}
}
["errors"]=>array(0) {
}
}
Above is the var_dump of the variable $json. I am trying to extract the CITY field with:
$json = json_decode($surescript);
foreach ($json as $providers){
foreach($providers as $onespot) {
echo "*";
echo $onespot->city;
echo "\n";
}
}
This outputs:
Warning: Invalid argument supplied for foreach() in /home/content/96/7973196/html/channels/MIL432/storeFinder_2.php on line 153
*Minneapolis
*Saint Louis Park
So, it is getting the correct nested variable, but also throwing off an error on the second foreach loop.
What am I doing incorrectly?
Thanks in advance for your help.
You're iterating over everything in the JSON in your outer loop, meaning that it's trying to parse the success section, the providers section, as well as the errors section.
When you try to parse the success, you can't iterate over the contents in the inner loop, hence the error. The errors section is an array, so you can iterate over it - since there's nothing in it, no error/warning for trying to access city, which wouldn't exist in it.
To only parse the providers:
$json = json_decode($surescript);
foreach ($json->providers as $provider){
echo "*";
echo $provider->city;
echo "\n";
}
So in my code I have:
var_dump($poll);
which prints:
object(stdClass)#2 (9) {
["parent"]=> object(stdClass)#3 (3) {
["className"]=> string(5) "Venue"
["__type"]=> string(7) "Pointer"
["objectId"]=> string(10) "HRSCYpe2FK"
}
["no"]=> int(0)
["updatedAt"]=> string(24) "2011-11-06T23:37:17.917Z"
["creator"]=> object(stdClass)#4 (3) {
["className"]=> string(5) "_User"
["__type"]=> string(7) "Pointer"
["objectId"]=> string(10) "96K81tdpM4"
}
["createdAt"]=> string(24) "2011-11-06T23:37:14.591Z"
["yes"]=> int(1)
["objectId"]=> string(10) "U8ly32582W"
["question"]=> string(20) "Negozio conveniente?"
["reports"]=> object(stdClass)#5 (0) { }
}
and then I want to do:
var_dump($poll["parent"]["objectId"]);
but then it gives me a 500 internal server error. Any idea why this is?
UPDATE:
Pulling from the error log it says:
PHP Fatal error: Cannot use object of type stdClass as array
Looks like your $poll variable is a stdclass in which case, you would access the properties like so
var_dump($poll->parent->objectId)
As for your 500 status, at a guess I'd say PHP is configured to trigger this for errors.
It's because you're tying to do a var_dump on an object and not an array.
$poll->parent->objectId;
object :: object :: property