When I make a request to my webservice one time I get:
funcion login returns => {
"token":"fsuehfisubfibiefasasdapmwineq","client":"Admin","permission":"ADMIN"
}
And I can just save something like the client or the token in a variable, doing for example:
$json = login($email,$password); - saving the JSON in the variable $json.
$values = json_decode($json); - decoding the $json and saving it on $values.
And know if I want to save the name or the token for future requests to the webservice I make a variable:
$token = $values->{'token'};
$client = $values->{'client'};
But my question is what i do when i get this from the webservice, because I did the same and tried somethings and I'm not getting anywhere.
funcion member returns = {
"Table":[
{
"client":"Admin",
"name":"Martin Lupin",
"age":"25",
"city":"Lisbon"
}
]
}
How do I access to "client", "name", "age", "city"?
I tried to do:
$name = $valuesclient->{'client'}; - and nothing
$name = $valuesclient->{'table'}->{'client'}; - and nothing
$name = $valuesclient->{'table'->{'client'}}; - and nothing
Can someone help me?
I'm still new to webhook. What I need to do here is to do a callback whenever there's a new registration on the registration platform called Bizzabo. This platform has provided Webhook integration by having us putting the Endpoint URL and select which action that will trigger the Webhook. I've also used Request Bin and it displays the data well.
However, how can I echo the JSON body data like how it displayed in Request Bin in my interface URL php?
This is how the Webhook integration looks like on Bizzabo
Data captured from Webhook when tested using Request Bin
Thank you!
Your need an endpoint which receives the callback instead Request Bin, then access it in the following way using file_get_contents('php://input') and json_decode()
For example http://example.com/bizzabo-callback-handler.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// fetch RAW input
$json = file_get_contents('php://input');
// decode json
$object = json_decode($json);
// expecting valid json
if (json_last_error() !== JSON_ERROR_NONE) {
die(header('HTTP/1.0 415 Unsupported Media Type'));
}
/**
* Do something with object, structure will be like:
* $object->accountId
* $object->details->items[0]['contactName']
*/
// dump to file so you can see
file_put_contents('callback.test.txt', print_r($object, true));
}
If the data receiving in compressed format(gzip) use gzdecode :
<?php
if (!function_exists('gzdecode')){
function gzdecode($data){
// strip header and footer and inflate
return gzinflate(substr($data, 10, -8));
}
}
// get compressed (gzip) POST request into a string
$comprReq = file_get_contents('php://input');
// get decompressed POST request
$decomprReq = gzdecode($comprReq);
// decode to json
$jsonData = json_decode($decomprReq, true);
// do your processing on $jsonData
?>
I am querying a 3rd party service which outputs json.
$data = json_decode($result,true);
var_dump ($data);
$data holds this:
{"response":{"status":"OK","token":"hbapi:187089:586e655ed1f9c:nym2","dbg_info":{"instance":"53.bm-hbapi.prod.ams1","slave_hit":false,"db":"master","parent_dbg_info":{"instance":"64.bm-hbapi.prod.nym2","slave_hit":false,"db":"master","parent_dbg_info":{"instance":"45.bm-api.prod.nym2","slave_hit":false,"db":"master","time":664.07299041748,"version":"1.17","warnings":[],"slave_lag":0,"start_microtime":1483629917.8571},"awesomesauce_cache_used":false,"count_cache_used":false,"uuid":"39e1c17a-7fe4-58ac-9486-c4dd5bbf96a3","warnings":[],"time":1159.7349643707,"start_microtime":1483629917.7835,"version":"1.17.150","slave_lag":0,"output_term":"not_found"},"awesomesauce_cache_used":false,"count_cache_used":false,"uuid":"286ca4bc-6964-50ad-b241-ff9df1304278","warnings":[],"time":1347.2578525543,"start_microtime":1483629917.6534,"version":"1.17.150","slave_lag":0,"output_term":"not_found","master_instance":"64.bm-hbapi.prod.nym2","proxy":true,"master_time":1159.7349643707}}}
I am trying to get the token value.
I tried
$token = $data["response"][0]["token"];
I get NULL
I also tried
$token = $data['response'][0]['token'];
And I still get NULL.
I have looked at How can I access an array/object? and other threads - can't find the issue.
$token = $data["response"]["token"];
json:
{
"response": {
"status": "OK",
"token": "hbapi:187089:586e655ed1f9c:nym2",
"dbg_info": {
}
}
}
Why are you accessing the pos [0] if isn't an array?
Instead of that try to access to the desired parameter like this:
$token = $data["response"]["token"];
change $data['response'][0]['token']; to $data['response']['token'];
$data['response'] is, it doesnot have the 0 index.
{
"status": "OK",
"token": "hbapi:187089:586e655ed1f9c:nym2",
"dbg_info": {
}
$data is an object (not an array), so you can access response as a property of this object: $data->response.
response is again an object. etc..
For example status can be called like this: $data->response->status.
You can see what is an object and what is an array using this code:
print "<pre>";
var_dump($data);
So in your case, to get token:
$token = $data->response->token
In $data you still have json, so you should call json_decode($data) one more time.
This is my JSON file which I get from an external link:
[
{
"Id":441,
"Name":"Gary"
},
{
"Id":1864,
"Name":"Bob"
}
]
When I try and display the Id and Name, I receive the error:
Notice: Trying to get property of non-object
$file = file_get_contents('http://linktojson.com');
$decode = json_decode($file, false);
$name = $decode->Name;
$id = $decode->Id;
echo $name;
echo $id;
Your json data has nested objects. So you need to access it like this:
$decode[0]->Name;
See here: https://3v4l.org/2aY22
Seeing as you have multiple objects with the same structure, you probably want to loop over them, like this:
foreach($decode AS $person) {
echo $person->Id . ": " . $person->Name;
}
Side note: it really helps to examine your data structure if you're having trouble navigating it. Just doing a var_dump($decode); shows you quite clearly how it is structured and how you need to access it!
I am trying to interpret the response from a web service call using json_decode. The JSON appears to be valid (have checked in online validators and such) but json_decode still returns NULL.
The data returned by the web service is as follows:
[{"id":"cc9dfabc36abc54c5a7","extension":"001","links":{"self":"https:\/\/someurl\/api\/response\/#me\/001\/cc9dfabc36abc54c5a7"}}]
The web service response data is stored in $result. My code to decode the JSON is as follows:
if (is_null(json_decode($result))){
$callObj = json_decode($result, true);
$callID = $callObj->extension;
}
else
{
echo "<BR>Could not decode response <BR>";
$callID = 'error';
}
return $callID;
Shouldn't the first test be negative (!is_null)? Only if it isn't null (and therefore a json object) should you then proceed.
Also, the second json_encode function you're passing a 'true' as the second argument which makes the returned object an associative array rather than an object.
The below alterations appeared to work for me.
if (!is_null(json_decode($result))){
$callObj = json_decode($result, true);
$callID = $callObj[0]['extension'];
}
else
{
echo "<BR>Could not decode response <BR>";
$callID = 'error';
}
return $callID;