I am using php curl to get account and transaction data from the plaid api. I'd like to see it in a nice format so I can go about creating loops to save the information into a database. The decoded json output, when printed with print_r, is returned like this:
array ( [accounts] => Array ( [0] => Array ( [_id] => QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK [_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA [_user] => eJXpMzpR65FP4RYno6rzuA7OZjd9n3Hna0RYa [balance] => Array ( [available] => 1203.42 [current] => 1274.93 ) [institution_type] => fake_institution [meta] => Array ( [name] => Plaid Savings [number] => 9606 ) [subtype] => savings [type] => depository ) [1] => Array ( [_id] => nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy [_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA...
I'd like to get it looking like this:
Array (
[accounts] => Array (
[0] => Array (
[_id] => QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK
[_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA
[_user] => eJXpMzpR65FP4RYno6rzuA7OZjd9n3Hna0RYa
[balance] => Array (
[available] => 1203.42
[current] => 1274.93 )
[institution_type] => fake_institution
[meta] => Array (
[name] => Plaid Savings
[number] => 9606 )
[subtype] => savings
[type] => depository )
[1] => Array (
[_id] => nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy
[_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA
`
For reference, here is the code to get the response:
$data = array(
"client_id"=>"test_id",
"secret"=>"test_secret",
"access_token"=>"test_chase"
);
$string = http_build_query($data);
//initialize session
$ch=curl_init("https://tartan.plaid.com/connect/get");
//set options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute session
$accountData = json_decode(curl_exec($ch), true);
print_r($accountData);
//close session
curl_close($ch);
Any thoughts other than loops?
Related
So i was trying to get "title" and "Artist->name" from all the indexes
and i tried many times even used foreach but i am still learning curl and can only get it for the first index or a specific index here is the table
(
[data] => Array
(
[0] => Array
(
[id] => 2298089
[readable] => 1
[title] => Broken Strings
[title_short] => Broken Strings
[title_version] =>
[link] => https://www.deezer.com/track/2298089
[duration] => 250
[rank] => 749501
[explicit_lyrics] =>
[explicit_content_lyrics] => 0
[explicit_content_cover] => 0
[preview] => https://cdns-preview-1.dzcdn.net/stream/c-140e953bb38254bc8db4fe4dc97dd689-8.mp3
[md5_image] => 71698262cce4bb1c0e3d25e0707ed092
[artist] => Array
(
[id] => 4523
[name] => James Morrison
[link] => https://www.deezer.com/artist/4523
[picture] => https://api.deezer.com/artist/4523/image
[picture_small] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/56x56-000000-80-0-0.jpg
[picture_medium] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/250x250-000000-80-0-0.jpg
[picture_big] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/500x500-000000-80-0-0.jpg
[picture_xl] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/1000x1000-000000-80-0-0.jpg
[tracklist] => https://api.deezer.com/artist/4523/top?limit=50
[type] => artist
)
[album] => Array
(
[id] => 229099
[title] => Songs For You, Truths For Me (International Exclusive Bundle)
[cover] => https://api.deezer.com/album/229099/image
[cover_small] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/56x56-000000-80-0-0.jpg
[cover_medium] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/250x250-000000-80-0-0.jpg
[cover_big] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/500x500-000000-80-0-0.jpg
[cover_xl] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/1000x1000-000000-80-0-0.jpg
[md5_image] => 71698262cce4bb1c0e3d25e0707ed092
[tracklist] => https://api.deezer.com/album/229099/tracks
[type] => album
)
[type] => track
)
[1] => Array
(
[id] => 1196110412
etc.., and here is the code
$ch = curl_init();
$url = "https://api.deezer.com/search?q=broken%20strings";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
if($e = curl_error($ch)){
echo $e;
}else{
$decoded = json_decode($resp,true);
print_r($decoded);
}
curl_close($ch);
this posts all of array
$decoded['data'] will contain an array with all your records, so you want to foreach over that:
foreach ($decoded['data'] as $record) {
On each iteration of that loop, $record will be the subarray for each individual record. So on the first iteration, $record will be:
Array(
[id] => 2298089
[readable] => 1
[title] => Broken Strings
[title_short] => Broken Strings
[title_version] =>
[link] => https://www.deezer.com/track/2298089
[duration] => 250
[rank] => 749501
...
Then you can reference each field in that array like $record['id'] or $record['link']. To "drill down" into multi-dimensional arrays, just stack consecutive [] references, like $record['album']['id'] or $record['artist']['link'].
All together:
foreach ($decoded['data'] as $record) {
printf("Title: %s\n", $record['title']);
printf("Artist: %s\n\n", $record['artist']['name']);
}
I have the following problem.
I am trying to read a Json file but I am not getting the desired result. I have the following data.
I would like to read the features and there is a mistake in finding out somewhere.
I get the error message when reading out:
Warning: Illegal string offset 'features'
Output
Array
(
[objectIdFieldName] => OBJECTID
[uniqueIdField] => Array
(
[name] => OBJECTID
[isSystemMaintained] => 1
)
[globalIdFieldName] =>
[geometryProperties] => Array
(
[shapeAreaFieldName] => Shape__Area
[shapeLengthFieldName] => Shape__Length
[units] => esriMeters
)
[geometryType] => esriGeometryPolygon
[spatialReference] => Array
(
[wkid] => 4326
[latestWkid] => 4326
)
[fields] => Array
(
[0] => Array
(
[name] => GEN
[type] => esriFieldTypeString
[alias] => GEN
[sqlType] => sqlTypeOther
[length] => 33
[domain] =>
[defaultValue] =>
)
[1] => Array
(
[name] => cases
[type] => esriFieldTypeInteger
[alias] => Anzahl Fälle
[sqlType] => sqlTypeOther
[domain] =>
[defaultValue] =>
)
[2] => Array
(
[name] => deaths
[type] => esriFieldTypeInteger
[alias] => Anzahl Todesfälle
[sqlType] => sqlTypeOther
[domain] =>
[defaultValue] =>
)
)
[features] => Array
(
[0] => Array
(
[attributes] => Array
(
[GEN] => Celle
[cases] => 220
[deaths] => 15
)
...........
My Code
$url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=GEN%20%3D%20%27CELLE%27&outFields=GEN,cases,deaths&outSR=4326&f=json";
$json = file_get_contents($url);
$data = json_decode($json,true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
foreach($data as $row){
echo $row['features']->$row['attributes']->$row['GEN'];
}
Where am I wrong in reading?
It only has to be read what is in parentheses right?
So actually features-> attributes-> Gen to get the GEN query
Let's try change to this.
foreach($data as $row){
echo $row['features'][0]['attributes']['GEN'];
}
Sorry this is my fault when seem does'nt read clearly your question.
With GEN attribute you don't use loop to get this value.
If you wanna get GEN only your code should be that.
$url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=GEN%20%3D%20%27CELLE%27&outFields=GEN,cases,deaths&outSR=4326&f=json";
$json = file_get_contents($url);
$data = (array) json_decode($json, true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
echo $data['features']['attributes']['GEN'];
// foreach($data as $row){
// echo $row['features']->$row['attributes']->$row['GEN'];
// }
I am trying to print the following json data in foreach loop of php. Can anyone please tell me how I can do it? I have searched a lot but nothing works.
I am getting this data from coinmarket api and printing this code by using:
but failed to get every value separately.
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
print_r(json_decode($response));
Below is code that I want to use in foreach loop:
stdClass Object ( [status] => stdClass Object ( [timestamp] => 2019-10-09T03:13:30.051Z [error_code] => 0 [error_message] => [elapsed] => 9 [credit_count] => 1 [notice] => ) [data] => stdClass Object ( [BTC] => stdClass Object ( [id] => 1 [name] => Bitcoin [symbol] => BTC [slug] => bitcoin [num_market_pairs] => 7935 [date_added] => 2013-04-28T00:00:00.000Z [tags] => Array ( [0] => mineable ) [max_supply] => 21000000 [circulating_supply] => 17981825 [total_supply] => 17981825 [platform] => [cmc_rank] => 1 [last_updated] => 2019-10-09T03:12:33.000Z [quote] => stdClass Object ( [USD] => stdClass Object ( [price] => 8195.0290967 [volume_24h] => 14981665979.667 [percent_change_1h] => -0.224992 [percent_change_24h] => -1.35388 [percent_change_7d] => -1.38135 [market_cap] => 147361579086.77 [last_updated] => 2019-10-09T03:12:33.000Z ) ) ) ) )
Please format your question, code and output so it's becomming readable. What have you tried yet? What data do you need?
To get a list of the names of all coins retrieved in data you can use this code:
$response = json_decode($response);
foreach ($response->data as $coin){ // assuming the data could contain multiple coins
echo $coin->name;
echo '<br>';
}
Tip: if you want to see a nice formatted output of print_r() in an HTML-page you can place it between PRE-tags.
echo '<pre>';
print_r($my_object);
echo '</pre>';
I am trying to fetch videos from youtube using below code:
$youtube_api_url = "https://www.googleapis.com/youtube/v3/search?key=".$new_key->api_key;
$youtube_api_url = $youtube_api_url."&order=relevance&type=video&maxResults=20&part=snippet&videoCategoryId=10&q=". urlencode(preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '',$trackRecord->name));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$youtube_api_url);
$result=curl_exec($ch);
$response = json_decode($result, true);
echo "<pre>"; print_r($response); die;
It is returning wrong number of results as below:
Array
(
[items] => Array
(
[0] => Array
(
[kind] => youtube#searchResult
[etag] => "m2yskBQFythfE4irbTIeOgYYfBU/ne5AZXpdoLtAn-mlwu4ipioOQ_I"
[id] => Array
(
[kind] => youtube#video
[videoId] => dB0ITzfbwfw
)
[snippet] => Array
(
[publishedAt] => 2017-01-23T17:31:23.000Z
[channelId] => UCdW5nPo1oyD2vLkvoYijmEg
[title] => Lihtz Kamraz "The Switch Up" Interview Part 2
[description] => In 2016, a lot of break out artists emerged and made a mark in the game. As 2017 begins, one name that you should definitely be on the lookout for is Lihtz ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/default.jpg
[width] => 120
[height] => 90
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/mqdefault.jpg
[width] => 320
[height] => 180
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/hqdefault.jpg
[width] => 480
[height] => 360
)
)
[channelTitle] => HIPHOPSINCE1987TV
[liveBroadcastContent] => none
)
)
)
)
As you can see it is showing total and per page result as:
[pageInfo] => Array
(
[totalResults] => 14
[resultsPerPage] => 20
)
How can I get all results?
What I am trying to do is to get the value of the first element, [0]['uri'] from my result curl api. I have an associative array from the curl_exec but can't figure out how to extract the data to find the value of the first element. I have tried echo $alerts[0]['uri'] but it doesn't work.
<?php
// Initiate curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json',"Authorization: Bearer $access_token"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$alerts = json_decode($result,true);
print_r($alerts);
//echo $alerts[0]['uri'] doesn't work.
?>
Output Result:
Array (
[result] => success
[message] => Array (
[alerts] => Array (
[0] => Array (
[id] => 78591963
[uri] => htps://access.rrr.com/interface/open_api/api/alerts/78591963
)
[1] => Array (
[id] => 78576283
[uri] => htps://access.rrr.com/interface/open_api/api/alerts/78576283
)
[2] => Array (
[id] => 78576209
[uri] => htps://access.rrr.com/interface/open_api/api/alerts/78576209
)
[3] => Array (
[id] => 78572644
[uri] => htps://access.rrr.com/interface/open_api/api/alerts/78572644
)
[4] => Array
)
)
)
)
Answer was given by other gentlemen in the comment.
I failed to look at the array structure. It is simply:
$alerts['message']['alerts'][0]['uri']