Pulling data from weird JSON array response - php

I am working with an API and the response comes back weird. I need to grab the ID field from the response below.
{
"message_campaigns": [
{
"embedded_errors": [],
"id": "2729",
"is_legacy_message": false,
"is_setup_complete": false,
"message_campaign_type_id": 1,
"name": "Message Campaign 1",
"organization_id": 123
}
]
}
Below is my attempt:
$result = curl_exec($ch);
curl_close($ch); // Seems like good practice
$responseData = json_decode($result, TRUE);
print_r($responseData);
I have tried several methods and cannot get it to work
$responseData[0]['id'];
$responseData->id;
I can't figure out how to get the id to display.

Assuming your curl_exec actually returns a response because you've set curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
If I look at your json response you need:
$responseData['message_campaigns'][0]['id'];

It's because the result is an array, as you pass the second parameter as true to json_decode:
When TRUE, returned objects will be converted into associative arrays.
In JSON, {} stands for objects, and [] stands for array. You've converted all objects to associative arrays, so this'll work:
$responseData['message_campaigns'][0]['id']

You have the second parameter of json_decode set to true, so an array (not an object) will be returned.
You'll also need to access message_campaigns as the first item in the array.
$responseData['message_campaigns'][0]['id'];
This can be tested:
$result = <<< EOT
{
"message_campaigns": [
{
"embedded_errors": [],
"id": "2729",
"is_legacy_message": false,
"is_setup_complete": false,
"message_campaign_type_id": 1,
"name": "Message Campaign 1",
"organization_id": 123
}
]
}
EOT;
$responseData = json_decode($result, TRUE);
echo $responseData['message_campaigns'][0]['id'];
// 2729

Related

Extract data from JSON with PHP?

i want to take data of username(david) from this Json , I saw the similar question i try more than 100 times but i can not , I need exact answer if it's possible.
thanks in advance
{
"related_assets": [],
"orders": [],
"auctions": [],
"supports_wyvern": true,
"top_ownerships": [
{
"owner": {
"user": {
"username": "david"
},
"",
"address": "",
"config": ""
},
"quantity": "1"
}
],
"ownership": null,
"highest_buyer_commitment": null
}
and i coded as below :
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
//the second parameter turns the objects into an array
$decodedData = json_encode($response, true);
$owner = $decodedData['top_ownerships'][0]['owner'];
$username = $owner['user']['username'];
echo $username;
}
the output is "
please help thanks
your code is completely current! and it works like charm!
but you made a very ambiguous mistake in your JSON code
you're using this ” instead of " (double quotes) there "david” <----
they exactly look the same!
and also you missed {} in the beginning and end of your JSON code
checkout your json code here it's the best tool for this
<?php
$code = '{
"top_ownerships": [{
"owner": {
"user": {
"username": "david"
},
"profile_img_url": "",
"address": "",
"config": ""
},
"quantity": "1"
}]
}';
$decodedData = json_decode($code, true);
$owner = $decodedData['top_ownerships'][0]['owner'];
$username = $owner['user']['username'];
echo $username;
?>
To start with, it's important you understand how to access json fields and key php;
Assuming the above values are stored in a file known as data.json, we can extract the values like this:
$data = file_get_contents('data.json');
//the second parameter turns the objects into an array
$decodedData = json_decode($data, true);
$owner = $decodedData['top_ownerships'][0]['owner'];
$username = $owner['user']['username'];
//echo $username
The above would work if your json file or data is properly encoded.
first you have to store your JSON into a variable, lets call it $json.
$jsonArray = json_decode($json,true);
And set your key name, the key you want from your JSON.
$key = "owner";
$owner= $jsonArray[$key];
You also can read like an object:
$owner= $jsonObj->$key;
Try
$object = json_decode('{"owner": {"user": {"username": "david”},"profile_img_url": "","address": "","config": ""},"quantity": "1"}')
echo $object->owner->user->username
This will make the JSON an object that you can access by using the -> accessor

Trouble With API Curl Response(php)

I am having trouble with the response from Companies House API. I am making the following curl request from a php page:
$request_url_1 ='https://api.companieshouse.gov.uk/company/' ;
$company_number = 11495745;
$request_url_2 = '/officers';
$request_url = $request_url_1 . $company_number . $request_url_2;
$ch = curl_init($request_url);
$headers = array(
'Content-Type: application/json',
'Authorization: Basic '. base64_encode("$chkey:")
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$content = curl_exec($ch);
curl_close( $ch );
$contents = json_decode($content,true);
The following is the response that is 'pasted' on the php page after sending the curl request, and I cannot seem to get at the contents/value of the $contents variable that should contain the curl response.
{
"active_count":1,
"items_per_page":35,
"kind":"officer-list",
"etag":"6cc48213158205c9fa85492a4c2ef0a98b56b2f1",
"resigned_count":2,
"items":[
{
"date_of_birth":{
"year":1963,
"month":2
},
"appointed_on":"2019-08-01",
"nationality":"British",
"address":{
"address_line_1":"Moorside Crescent",
"country":"United Kingdom",
"address_line_2":"Sinfin",
"postal_code":"DE24 9PH",
"locality":"Derby",
"premises":"75"
},
"occupation":"Company Director",
"links":{
"officer":{
"appointments":"/officers/IryiaaBZodlGNaOP0Rf3Pb2GnO8/appointments"
}
},
"name":"MILLER, Eira",
"country_of_residence":"England",
"officer_role":"director"
},
{
"date_of_birth":{
"month":3,
"year":1987
},
"nationality":"English",
"occupation":"Company Director",
"address":{
"locality":"Derby",
"address_line_1":"Moorside Crescent",
"premises":"75",
"country":"United Kingdom",
"postal_code":"DE24 9PH",
"address_line_2":"Sinfin"
},
"appointed_on":"2018-12-10",
"resigned_on":"2019-07-31",
"name":"KING, Kimberley Rachel",
"links":{
"officer":{
"appointments":"/officers/av7G3-iF_9-FXhRH2xm-IxejeGQ/appointments"
}
},
"country_of_residence":"United Kingdom",
"officer_role":"director"
},
{
"address":{
"postal_code":"DE24 9PH",
"locality":"Derby",
"country":"United Kingdom",
"premises":"75",
"address_line_2":"Sinfin",
"address_line_1":"Moorside Crescent"
},
"occupation":"Managing Director",
"nationality":"British",
"appointed_on":"2018-08-01",
"resigned_on":"2018-12-10",
"officer_role":"director",
"country_of_residence":"United Kingdom",
"links":{
"officer":{
"appointments":"/officers/X9nlVD6qIIENMjaH946__4CB3QE/appointments"
}
},
"name":"MARTIN, Katey",
"date_of_birth":{
"month":6,
"year":1968
}
}
],
"links":{
"self":"/company/11495745/officers"
},
"inactive_count":0,
"start_index":0,
"total_results":3
}
I have tried returning result as non-associative array and access it through normal array but no joy there either.
FluffyKitten kindly supplied the following code to perform the extraction of the name field:
$item = $contents["items"];
foreach($item as $items){
echo $items["name"];
}
However, this couldn't access the required data. A print_r of $contents results in '1' and a var_dump (1)
There are a couple of problem with your code:
You are mixing up code for accessing arrays and objects
You are looping when there is no need for loops
You are using json_decode($content,true), and by passing in that true parameter, you are converting the result into associative arrays, but then you try to access items as the property of an object.
Because you have converted the contents to an associative array, you can easily access the information you want using the correct keys, e.g.:
$item = $contents["items"];
foreach($item as $items){
echo "<p>".$items["name"];
}
Reference PHP json_decode Documentation
UPDATE:
Your question has changed totally - it was about how to loop through the variable with the results, but now it seems that the problem is saving the results as a variable in the first place.
You need to set the CURLOPT_RETURNTRANSFER option to true so that the result is not output directly (I assume this is what you mean by the result being "pasted" into the PHP page).
Add this to your CURL code, and if the rest of your code is correct, the result should be saved in the $content variable so you can use it to loop through using the code above:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Handling Complex Arrays in PHP

I've worked with arrays in the past with PHP, but generally simple associative arrays that are easy to manage and crawl.
I am making an HTTP POST request to an API endpoint that returns a lot of data in JSON format. I'm using json_decode($response, true) to convert the json into an array, and am trying to access components of the array without luck - just serving me a blank page. Alternatively, if I do print_r on the array I just get a jumble of data, so I know at least the API is returning data.
Here's a snippet of the response from the API
{
"data": {
"accounts": [
{
"locations": [
{
"name": "Test Location",
"soundZones": [
{
"name": "Main",
"nowPlaying": {
"startedAt": "2017-09-06T00:38:51.000Z",
"track": {
"name": "Some Song Name 123",
"imageUrl": "https://some-cdn-url.com",
"Uri": "5hXEcqQhEjfZdbIZLO8mf2",
"durationMs": 327000,
"artists": [
{
"name": "SomeName",
"Uri": "5lpH0xAS4fVfLkACg9DAuM"
}
]
}
}
}
]
},
How would I use PHP to access, let's say, the NAME value under the track object? (In this example I'd be trying to return the value "Some Song Name 123")
Here's the code I'm using.. I know I'm way off
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$response = json_decode($response, true);
print_r($response[0]);
That's because you're being returned not just an array, but both an array and an object.
<?php
echo $response[0]->data->accounts[0]['locations'][0]->soundZones[0]->nowPlaying->track->name;
I like sf_admin's answer better, because this kind of messy object really lends itself more to being an object, but using json_decode($response, true) then I think you should be able to access it like this:
echo $response[0]['data']['accounts'][0]['locations'][0]['soundZones'][0][0]['nowPlaying']['track']['name'];
Try this.
//$response = json_decode($response, true);
$response = json_decode($response);
// loop
if (isset($response->data->accounts)) {
foreach ($response->data->accounts as $account) {
foreach ($account->locations as $location) {
foreach ($location->soundZones as $soundZone) {
print_r($soundZone->nowPlaying->track->name);
}
}
}
}
// first
if (isset($response->data->accounts[0]->locations[0]->soundZones[0]->nowPlaying->track->name)) {
print_r($response->data->accounts[0]->locations[0]->soundZones[0]->nowPlaying->track->name);
}
Some Song Name 123

file_get_contents returns an empty string

I am sending this json to my php site (http://example.com/webhook/) as a post from a third-party site, but all I get is an empty array.
{
"listing_id": 0,
"guest_id": 0,
"confirmation_code": "confirmation code",
"start_date": "2015-05-05",
"nights": 1,
"number_of_guests": 3,
"listing_base_price": 399
}
And for this, I am using the following server-side code in the index.php file:
$json = file_get_contents('php://input');
echo $json;
$data = json_decode($json, True);
The response is expected to be json so I am unable to use var_dump() hence it returns other than json.

JSON to PHP Output

I usually manage to figure stuff out by myself, but on this occasion I've had to register an account and ask for help before I jump out the window.
I'm trying to output some basic JSON data to php, all I need to do is echo it out, the rest I'll figure out.
The API gives this guide:
{
"success" : true,
"message" : "",
"result" : {
"Bid" : 2.05670368,
"Ask" : 3.35579531,
"Last" : 3.35579531
}
}
An example of the URL I'll be using: https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC
All I want to output is the 'Last' data, I don't care about the rest, keeping the decimal in the right place is also important.
I've tried all sorts, I can't get it to output it properly :(. I've ran a var_dump which spits out:
array(3) { ["success"]=> bool(true) ["message"]=> string(0) "" ["result"]=> array(3) { ["Bid"]=> float(0.00011505) ["Ask"]=> float(0.000116) ["Last"]=> float(0.00011505) } }
If someone could just tell me the few lines of code to put the 'Last' number into a variable called $lastBid I will love you long time!
Thanks guys!
use json_decode - php method to decode json
http://php.net/manual/en/function.json-decode.php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
Here you have an example,
$contents = file_get_contents("https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC");
$json = json_decode($contents);
$lastBid = $json->result->Last;
$lastBid would then be set to 0.01189802
You need to json_decode() the result.
that kind of data is called json. php permits you to convert that json (which is a string) to a php array.
to get it, you need to convert it and then access to the value you'd like using array rules.
// save in a variable the data you're going to process
$json = '{
"success" : true,
"message" : "",
"result" : {
"Bid" : 2.05670368,
"Ask" : 3.35579531,
"Last" : 3.35579531
}
}';
// json_decode is a function that allows you to obtain an array
// (the second parameter set to true indicates that the array'll be an associative one)
$data = json_decode($json, TRUE);
/*
every php array has an internal pointer which points to a position in the array.
the end pointer, if not moved, points to the last position. to access to the value
you want, first get the last value (an array called "result"),
then access to the last value of that array (called "last").
the property you'll get is the float value you requested!
*/
var_dump(
end(
end($data)
)
);
and here, there is the output:
float(3.35579531)
Access it by:
$url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
$data = file_get_contents($url);
$data = json_decode($data);
$last = $data->result->Last;
If you like using arrays instead of object orrientation style, json_decode has an extra boolean param, that converts it too an array if you feel more comfortable using that.
$url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
$data = file_get_contents($url);
$data = json_decode($data,true);
$last = $data['result']['Last'];
Side note; For accessing API's I would rather advice you to use curl instead of file_get_contents. It gives you better control, for instance with timeouts. But you have many more options. You can use this function;
function curl($URL,&$errmsg){
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_TIMEOUT, 10);
$contents = curl_exec($c);
if (curl_errno($c)){
$errmsg = 'Failed loading content.';
curl_close($c);
return;
}
else{
curl_close($c);
return($contents);
}
}
And your code then would be:
$url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
$data = curl($url, $errmsg);
$data = json_decode($data,true);
$last = $data['result']['Last'];

Categories