PHP Trying to get property of non-object - php

First of all i know stackoverflow is full from this kind of erros but none of them is like mine so im posting this.
Im trying to get a JSON response from the api and as im trying to echo it im getting the Trying to get property of non-object error.
$apicalldata = file_get_contents("https://api.digitalocean.com/v1/droplets/?client_id=6356465465363546&api_key=f9a702abe442198a4168346435366436cc4cd2138dfc");
$call = json_decode($apicalldata);
echo $call->droplets->id;
This is the code im using. From this i'm expecting a response like this:
{
"status": "OK",
"droplets": [
{
"id": 100823,
"name": "test222",
"image_id": 420,
"size_id":33,
"region_id": 1,
"backups_active": false,
"ip_address": "127.0.0.1",
"private_ip_address": null,
"locked": false,
"status": "active",
"created_at": "2013-01-01T09:30:00Z"
}
]
}
Any ides why am i having this problem? Also is the $call->droplets->id correct?
Thanks for your time

It looks like droplets is an array. Give this a try.
$droplets = $call->droplets;
$myDroplet = $droplets[0];
$myDropletID = $myDroplet->id;
echo $myDropletID;
* Update after your comment *
$droplets = $call->droplets;
foreach($droplets as $droplet)
{
$dropletID = $droplet->id;
echo $dropletID;
}

try this
$call = json_decode($apicalldata);
foreach($call->Droplet as $adm)
{
echo "ID ".$adm->id."<br/>";
}

Related

Printing a nested JSON Array value [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 months ago.
I've been trying to find ways to print the individual data but can't seem to figured out where I'm going wrong.
I started with this but I get no results. Before this I tried nesting loops but also got nowhere either.
$data = curl_exec($ch);
$d = json_decode($data, true);
foreach($d as $k=>$v){
echo $v['value']['displayName'];
}
Then I tried the following with only got me some of the results. I'm not sure where I'm going wrong with this.
foreach(json_decode($data,true) as $d){
foreach($d as $k=>$v){
foreach($v as $kk=>$vv){
echo $kk.$vv;
}
}
}
The JSON looks like the following:
{
"value": [
{
"id": "",
"name": "",
"etag": "",
"type": "Microsoft.SecurityInsights/alertRules",
"kind": "Scheduled",
"properties": {
"incidentConfiguration": {
"createIncident": true,
"groupingConfiguration": {
"enabled": false,
"reopenClosedIncident": false,
"lookbackDuration": "PT5M",
"matchingMethod": "AllEntities",
"groupByEntities": [],
"groupByAlertDetails": null,
"groupByCustomDetails": null
}
},
"entityMappings": [
{
"entityType": "Account",
"fieldMappings": [
{
"identifier": "FullName",
"columnName": "AccountCustomEntity"
}
]
},
{
"entityType": "IP",
"fieldMappings": [
{
"identifier": "Address",
"columnName": "IPCustomEntity"
}
]
}
],
"queryFrequency": "P1D",
"queryPeriod": "P1D",
"triggerOperator": "GreaterThan",
"triggerThreshold": 0,
"severity": "Medium",
"query": "",
"suppressionDuration": "PT1H",
"suppressionEnabled": false,
"tactics": [
"Reconnaissance",
"Discovery"
],
"displayName": "MFA disabled for a user",
"enabled": true,
"description": "Multi-Factor Authentication (MFA) helps prevent credential compromise. This alert identifies when an attempt has been made to diable MFA for a user ",
"alertRuleTemplateName": null,
"lastModifiedUtc": "2022-11-14T02:20:28.8027697Z"
}
},
...
...
...
Here is how you can get the display name without a loop. Notice that the 0 is the key value of the array since it doesn't have a name.
We start from the value, and we move one layer deeper by selecting the first array 0. Now we need to select the properties and finally, we can get the displayName from there.
$displayName = $d["value"][0]["properties"]["displayName"];
echo($displayName);
/*
Here is a quick demonstration:
value:
{
0:
{
...
properties:
{
...
displayName: [We made it!]
}
}
}
*/
And here is a very good post that explains this in more detail
How to decode multi-layers nested JSON String and display in PHP?

Zoho - Get json value from returned data

I'm using zoho crm..
I get the json data below returned from a query
however I'm struggling to get the value of a string inside the returned data
Here is a sample of the data returned
"response": {
"result": {
"Deals": {
"row": {
"no": "1",
"FL": [
{
"val": "DEALID",
"content": "3508588000000206039"
},
{
"val": "SMOWNERID",
"content": "3508588000000176021"
},
{
"val": "Amount",
"content": "5000"
}
I'm trying to get the Amount value
Here is the PHP code
$json = file_get_contents($url);
$obj = json_decode($json);
$amount = $obj->result->Deals->row->FL['Amount'];
echo 'Deal Amount : £'.$amount;
Thanks In Advance
You need to change a bit
$amount = $obj->response->result->Deals->row->FL[2]->content;
//--------------^index------------------------^index---^column name need to be correct---
Dealing with Zoho response could get messy. You could use this library to help smoothen things for you.
In the meantime, $obj->response->result->Deals->row->FL[2]->content; should do the trick for you.

How can i get this url part using json_decode?

I've been trying to get the "URL" part of this piece of JSON using json_decode but i can't seem to figure out how to do it.
This is the json:
{
"status": 200,
"msg": "OK",
"result": {
"url": "https://ph2dmj.oloadcdn.net/uls/RAXxARA3DQs",
"valid_until": "2015-10-07 19:09:01"
}
}
Here is what i've tried:
$obj = json_decode($result);
echo $obj->{'url'};
If i replace "url" with "status" i get 200 returned so i can't understand why it won't give me the url when i enter URL?
Thanks
You need to access url through result like this:
$obj->result->url

navigate through multidimensional PHP array by relative path

I'm trying to use some JSON data in my website, but i got stuck while trying to read from.
Getting the data works well:
<?php
$data = json_decode(file_get_contents('http://ddragon.leagueoflegends.com/cdn/5.2.1/data/en_US/champion.json'));
?>
this is a little excerpt of the JSON data, but it lasts out to explain the problem
{
"type": "champion",
"format": "standAloneComplex",
"version": "5.10.1",
"data": {
"Aatrox": {
"version": "5.10.1",
"id": "Aatrox",
"key": "266",
"name": "Aatrox",
"title": "Die Klinge der Düsteren",
"info": {
"attack": 8,
"defense": 4,
"magic": 3,
"difficulty": 4
},
},
"Ahri": {
"version": "5.10.1",
"id": "Ahri",
"key": "103",
"name": "Ahri",
"title": "Die neunschwänzige Füchsin",
"info": {
"attack": 3,
"defense": 4,
"magic": 8,
"difficulty": 5
},
},
}
}
Question: How is it possible, to access the value of "key", without knowing the 'heading' (e.g. "Aatrox") ?
I tried $data->{'data'}[0]->{'key'}, but that doesn't work.
Second Question: I also tried to search for the value of "key", but had no success in building the path with this method in PHP. A try with JavaScript worked well, but I would prefer to have a server-sided solution.
Thanks for your help!
If you want an element at particular 'offset', use
array_values($data->data)[0]->key;
otherwise, use foreach:
foreach ($data->data as $heading=>$data) {
echo "The heading is $heading and key is {$data->key}";
}
Alternative very short way:
<?php
$data = json_decode(file_get_contents('http://ddragon.leagueoflegends.com/cdn/5.2.1/data/en_US/champion.json'), true);
$first = current($data['data']);
More complete example:
<?php
$data = json_decode(file_get_contents('http://ddragon.leagueoflegends.com/cdn/5.2.1/data/en_US/champion.json'));
$key = current($data->data)->key
Personally I prefer to convert JSON object into the more PHP friendly Array.
If you use json_decode(file_get_contents('http://ddragon.leagueoflegends.com/cdn/5.2.1/data/en_US/champion.json')));
You will be able to access the key value with something like this
$champions = json_decode(file_get_contents('http://ddragon.leagueoflegends.com/cdn/5.2.1/data/en_US/champion.json')));
foreach($champions['data'] as $key => $row){
echo $row['key'];
}
This will echo your keys.

Parse url with API to an array via simple html dom parser

I have a problem with starting a project of 'clan community statistics'.
I have a URL with the API of the statistics, and i want to show this in an array using a simple html dom parser. I want to get this effect:
{
"status": "ok",
"count": 1,
"data": {
"1": {
"members_count": 100,
"description": "Закрытый клан...",
"description_html": "<p>Закрытый клан....\n</p>",
"created_at": 1293024672,
"updated_at": 1375930001,
"name": "Wargaming.net",
"abbreviation": "WG",
"emblems": {
"large": "http://cw.worldoftanks.ru/media/clans/emblems/clans_1/1/emblem_64x64.png",
"small": "http://cw.worldoftanks.ru/media/clans/emblems/clans_1/1/emblem_24x24.png",
"medium": "http://cw.worldoftanks.ru/media/clans/emblems/clans_1/1/emblem_32x32.png",
"bw_tank": "http://cw.worldoftanks.ru/media/clans/emblems/clans_1/1/emblem_64x64_tank.png"
},
"clan_id": 1,
"members": {
"196632": {
"created_at": 1293126248,
"role": "private",
"updated_at": 1375930001,
"account_id": 196632,
"account_name": "Wrobel"
},
"18458": {
"created_at": 1360836543,
"role": "diplomat",
"updated_at": 1375930001,
"account_id": 18458,
"account_name": "alienraven"
},
"3100": { ....
}
},
"motto": "Орлы! Орлицы!",
"clan_color": "#e18000",
"owner_id": 1277137
}
}
}
My code
include('simple_html_dom.php');
$html = file_get_html('http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500009659');
...?
What should I do next to prepare an array and display it in the way which I want on my web page? Is there someone obliging to explain it to me? please.
Greetings Mary
try this
<?php
// example of how to modify HTML contents
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500009659');
$data = json_decode($html);
echo "<pre>";
print_r($data);
?>
I see the data is in json format. Just use:
$json = file_get_contents('http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500009659'); //gets the json
$obj = json_decode($json); //decode the json into object or array. You'll get an object
var_dump($obj); //view the object. Parse the object the way you want to.

Categories