PHP cURL API response handling - php

I have a question regarding the handling of a response from an API using the cURL function from PHP.
If I access the API via cURL I get the following response:
{"status":"ok","meta":{"count":1},"data":{"502268596":{"clan_id":500022074}}}
I transformed this code (named $json) using:
$jsondecoded = json_decode($json,true);
If I var_dump the associative array I now got I receive the following:
array(3) { ["status"]=> string(2) "ok" ["meta"]=> array(1) { ["count"]=> int(1) } ["data"]=> array(1) { [502268596]=> array(1) { ["clan_id"]=> int(500022074) } } }
My Question is: How do I access the field "clan_id"? I'm absolutely lost, because I don't have a real understanding of how arrays work and how I cycle through them.

Your response data is in array format. In this specific case, you can access clan id as follows:
echo $jsondecoded['data'][502268596]['clan_id'];
If you would rather work with objects, you can do so this way:
$jsondecoded = json_decode($json);
echo $jsondecoded->data->{502268596}->clan_id;

Related

Accessing array with nested objects

I have an api wrapper i am using that returns something like this
object(stdClass)#7 (1) {
[0]=>
object(stdClass)#6 (2) {
["contactId"]=>
string(2) "nV"
["email"]=>
string(31) "email#domain.com"
}
}
how do i access the email part with PHP
Cast your API returned data to an array.
For example you are saving API returned data in $result variable. Cast it to an array.
$arrayResult = (array) $result;
echo $arrayResult[0]->email;
Try this.

php, from json decode to individual variables

I am trying to go from a cur request to a page with some info.
I have the curl working but I have trouble going from the decode json to individual php variables. the conten after json_decode is:
object(stdClass)#1 (2) { ["response"]=> object(stdClass)#2 (2) { ["request"]=> string(20) "mailboxes/status/get" ["result"]=> string(1) "0" } ["status"]=> string(7) "success" }
I need the value of result which is 0 here.
Thanks in advance.
$result = $data->response->result;
Assuming the variable $data is where you stored your json_decode. It returns an instance of stdClass, and viewing the vardump, you can see the structure and get the data you want.

Parsing json_decode output

Im a little stuck on parsing json output. I am working blind with json and have tried some tutorials but cant seem to find what im looking for.
I am trying to use a service providers API which is doing the functions correctly. I am then getting the correct feedback from the provider in json format.
My code so far looks like;
$response = curl_exec($apicall);
$json_output = json_decode($response);
var_dump($json_output);
This then returns;
object(stdClass)#1 (2) {
["status"]=>
string(2) "OK"
["droplet"]=>
object(stdClass)#2 (5) {
["id"]=>
int(490021)
["name"]=>
string(20) "test.mydomain.com"
["image_id"]=>
int(374535)
["size_id"]=>
int(66)
["event_id"]=>
int(6403716)
}
}
What I'm looking for is a way to store the "OK" from "status" and the "490021" from "id" as variables.
Hope someone can help.
There's no need to store them as variables. When you use json_decode, it'll create a stdClass which you can access the variables, like:
$json_output = json_decode($response);
echo $json_output->id;

Get elements of json_encoded array in javascript

i passed a json_encoded array to javascript. Now i would like to acces that array to get the different elemtnts.
i print it out in the console.log() and i get this array:
array(1) {
[16]=>
array(2) {
[3488]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "999184"
["article_name_internal"]=>
string(29) "Geschenkbox Kerzenschein 2011"
}
}
[2615]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "700469"
["article_name_internal"]=>
string(29) "Hotelscheck RomantischeTagef2"
}
}
}
}
This is about right. How can i access the article_name of the second array, with the ID 2615?
found a related question here reading a jsone object, hope for some better explebation or answer. Thanks.
EDIT:
As it seems i made a mistake, i showed a php var_dump in the console. When i try to show the javascript array in the console i get undefined.
Since JSON means "JavaScript Object Notation" you don't need to do anything to access the object's items.
For example you can access:
jsonObject[2615][0]["article_name_internal"]
if this object is String, use eval to convert the string to a JavaScript object and access the items in the same way with the previous example.
var jsonObject = eval(jsonstring);
jsonObject[2615][0]["article_name_internal"]

JSON data and PHP

I am retrieving data via JSON and PHP from a URL.
I am having difficulty breaking the object apart and displaying the data. The PHP code seems to be working until I get to the for loops.
$jsonurl = 'http://myweb/api.php';
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach ($json_output->Monitoring AS $monitoring) {
foreach ($monitoring->Status AS $Status){
echo $Status->Emailed;
echo $Status->Status;
}
Here is my data structure:
object(stdClass)#12 (1)
{ ["Monitoring"]=> array(10) { [0]=> object(stdClass)#13 (14)
{
["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
["Emailed"]=> string(1) "0" }
[1]=> object(stdClass)#14 (14) {
["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
["Emailed"]=> string(1) "0" }
[2]=> object(stdClass)#15 (14) {
["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
["Emailed"]=> string(1) "0" }
[3]=> object(stdClass)#16 (14) {
["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
["Emailed"]=> string(1) "0" }
} }
According to the data structure you put, you only need one foreach loop like:
$jsonurl = 'http://myweb/api.php';
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach ($json_output->Monitoring AS $Status) {
echo $Status->Emailed;
echo $Status->Status;
}
in the first foreach loop, the value ($monitoring in your code, $Status in mine as I just renamed it) is not another array that you need to loop over. It would contain a std object with Emailed and Status as keys.
First of all, are you sure your script returns actual JSON string? Did you json_encode it?
Did you compare the 'data structure' of the object before JSON and after decoding? Were there any differences? Maybe it is not at all about the foreach loops and JSON and the problem is instead in your data structure of sorts, which seems to consist of multiple sub-objects.
Alternative is to try and use associative arrays instead of objects in that context by returning json_decode($json,true) and treating it as an array.
At the outset, make sure you turn error reporting on so you can see what exactly is failing.
An improper format passed to json_decode will just return false. It is one of the limitations of PHP's json handling that they are trying to address in the next release by creating a JSONSerializable interface. Make sure the return for decode is an instance of stdClass (or an array if you have passed the option for an associative array), otherwise the loops will never execute at all.

Categories