I have the below php code which outputs me a json array from freebase api.
<?php
public function search($query, $filter = '', $start = 0, $limit = 10, $exact = 'false'){
if(!empty($query)){
$service_url = 'https://www.googleapis.com/freebase/v1/mqlread?query=';
$append = "[{\"id\":\"$query\",\"key\":[{\"namespace\":\"/wikipedia/en_title\",\"*\":null}]}]";
$url = $service_url. $append;
return json_decode(file_get_contents($url), true);
}
}
}
$freebase = new Freebase();
$result = $freebase->search('/m/03np_7');
$obj = json_encode($result);
echo $obj;
?>
My $obj output is as below.
{"result":[{"key":[{"namespace":"\/wikipedia\/en_title","value":"University_of_Texas_at_Arlington","type":"\/type\/key"}],"id":"\/m\/03np_7"}]}
I am trying to get the value field alone using php. I tried json_decode but it was saying it accepts only string and I have an array. How can I get the value alone from the Json_encode?
Related
I'm looking for script that can encode array values for keys. I need pass array value not key array for multiple values.
http_build_query will produce output like below:
&viewColumns%5B0%5D=date&viewColumns%5B1%5D=adv_responses
I need output like below:
&viewColumns=%5B%22date%22%2C%22adv_responses%22%5D
Please let me know if there is any solutions, the code is like this:
$data= array();
$data['group'] = 'cpcReportsAdvertiser';
$data['dimensions'] = array('date');
$data['viewColumns'] = array('date','adv_responses');
// $data['measures'] = ["adv_responses"];
$date = array('between',array('2019-07-01','2019-07-31'));
$dates = new stdClass;
$dates->date= $date;
$data['filters'] = $dates;
$data['attributes'] = [];
$sort = new stdClass;
$sort->column = 'adv_impressions';
$sort->direction = 'asc';
$data['sort'] = $sort;
$data['limit'] = 50;
$url = sprintf("%s?%s", $url, http_build_query($data));
print_r($url);exit;
To get viewColumns=%5B%22date%22%2C%22adv_responses%22%5D& which decodes to viewColumns=["date","adv_responses"] you would need to json_encode it:
$data['viewColumns'] = json_encode(array('date','adv_responses'));
So I've been working on this PHP API thing for a while, and it would save JSON to a PHP file. The code works, but only allows 1 object. How do I add another object without removing the old JSON (Appending JSON)
<?php
//-- Parameters --\\
$id = $_GET['id'];
$language = $_GET['language'];
$filter = $_GET['filter'];
$commands = $_GET['commands'];
$name = $_GET['name'];
$members = $_GET['members'];
//-- Read and Parse JSON --\\
$string = file_get_contents("./Data/Guilds.json");
$json_a = json_decode($string, true);
if(isset($json_a[$id])) {
}
else {
//-- Initialize Array --\\
$obj[$id] = Array();
//-- Set Array Objects --\\
$obj[$id]["language"] = $language;
$obj[$id]["filter"] = $filter;
$obj[$id]["commands"] = $commands;
$obj[$id]["information"]["name"] = $name;
$obj[$id]["information"]["members"] = $members;
//-- Encode and Write --\\
$fp = fopen('./Data/Guilds.json', 'w+');
fwrite($fp, $json);
fclose($fp);
}
The API is:
{"status":1,"data":[{"address":"0x5c48aebbbbdcf44f5f181edbb5b20a68210eacfe","balance":6000000000000000000,"nonce":null,"code":"0x","name":null,"storage":null,"firstSeen":null}]}
I am trying to get the value from Balance (6000000000000000000). I also need to divide this value with 1000000000000000000 in meantime...
I tried:
$obj = json_decode($json,true);
$address = $obj['address'];
$json = file_get_html('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
$v = (get_object_vars($obj->data));
$balance = $v['balance'];
$_SESSION['balance'] = $balance;
It doesn't work.
The same code works with the API below:
{"status":"success","data":{"address":"LU4P8fVNj8xv2dwRc7fdXmpaW2xuCSJvUK","is_unknown":false,"balance":0,"balance_multisig":0,"totalreceived":0.0340576,"nb_txs":11,"first_tx":{"time_utc":"2015-08-30T09:51:05Z","tx":"10f4b55d0bead8d3d84fe27433db20b63368d65bc043c651d59dbe1342d14098","block_nb":"842330","value":0.00704229,"confirmations":251241},"last_tx":{"time_utc":"2016-02-02T16:09:23Z","tx":"c666d3acf6f57fd86a2ccc9537ee022167da408f26193e4abbd7b8148fc518b3","block_nb":"932771","value":-0.01250452,"confirmations":160800},"is_valid":true},"code":200,"message":""}
What am I doing wrong?
Its because you're missing that the address is located in the first data array in the JSON string and in the second JSON string, no array exists inside the data array.
$obj = json_decode($json,true);
$address = $obj['data'][0]['address'];
$json = file_get_contents('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
echo $obj->data[0]->balance;
Im getting json files from a site and i want it to find which ever one gets the file and use that one. Im using the $_GET Method too. There are 2 ways of getting the same file but they both require an id or a custom url from steam.
My url example: www.mysite.com?id=123123123
2 ways of getting json file:
http://steamcommunity.com/id/ID/inventory/json/730/2
http://steamcommunity.com/CUSTOMURL/76561198051643107/inventory/json/730/2
How im decoding it:
$id = $_GET['id'];
$url = "http://steamcommunity.com/id/".$id."/inventory/json/730/2";
$content = file_get_contents($url);
$playerinfo = json_decode($content, true);
$InventoryStatus = $playerinfo['success'];
some ref
$id = $_GET['id'];
$handlers =array(
'getByJson','getByDB','getBySomething'
);
$result = array('handler'=>'','data'=>'');
then call $handlers
foreach($handlers as $m){
$methodResult = call_user_func($m,$id);
if($methodResult !==false){
$result = array('handler'=>$m, 'data' => $methodResult) ;
break;
}
}
handler something like this
function getByJson($id){
$url = "http://steamcommunity.com/id/".$id."/inventory/json/730/2";
$string = file_get_contents($url);
$isJson =is_string($string) && is_object(json_decode($string)) && (json_last_error() == JSON_ERROR_NONE) ? true : false;
if($isJson){
$playerinfo = json_decode($content, true);
return $playerinfo['success'];
}
return false;
}
function getByDB($id){
// get $result
if(condition){
return $result
}
return false;
}
function getBySomething($id){
// get $result
if(condition){
return $result
}
return false;
}
i have this JSON
{
"count":"3",
"num":"1",
"array":[
{
"id":"a_a",
"amount":56,
"duration":"0:12",
"time":1234566,
"type":0
},
{
"id":"a_a",
"amount":56,
"duration":"0:12",
"time":1234566,
"type":1
}
]
}
created it in android and send it by **HttpPost**
, i've tried a lot of ways to get the data in the php, my php file is this:
<?php
$response = array();
//$json_data = json_encode(stripslashes($_POST['jsonarray']))
$josn = file_get_contents("php://input");
$json_data = json_decode($josn,true);
$count = $json_data->{'count'};
$num = $json_data["num"];
$response["data"]=$count;
$response["data2"]=$num;
// echoing JSON response
echo json_encode($response);
?>
but $count and $num always returns null, any help please, and thanks.
$json_data = json_decode($josn,true);
this returns an array, not an object (which you are using later in the code). Use this to convert the json string to an object:
$json_data = json_decode($josn);
Or you could just use arrays, in which case your code should look like:
<?php
$response = array();
//$json_data = json_encode(stripslashes($_POST['jsonarray']))
$josn = file_get_contents("php://input");
$json_data = json_decode($josn, true); // using true to get array
$count = $json_data["count"]; // accessing array values as normal
$num = $json_data["num"]; // accessing array values as normal
$response["data"] = $count; // instead of setting $count first you could just add
$response["data2"] = $num; // the json_data array values directly to the response array
// echoing JSON response
echo json_encode($response);