I am trying to read a reddit json using my account as an example.
Tried the solution above as:
$string_reddit = file_get_contents("https://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);
$children = $json['data'];
foreach ($children as $child){
$linkkarma = $child['data']['link_karma'];
}
Also tried:
foreach ($json->data as $mydata){
$values["Latest_Karma"] = $mydata['link_karma'];
}
Also tried:
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: reddiant api script\r\n"
));
$context = stream_context_create($opts);
$url = "http://www.reddit.com/user/joshfolgado/about.json";
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
foreach ($result as $child){
$values['Latest_Karma'] = $child['data']['link_karma'];
}
Spent a couple of hours trying do get the values for any of the items inside the "data" array, havent been able to get any.
What am I doing wrong? What am I missing?
Any help is appreciated.
Thanks
$string_reddit = file_get_contents("http://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);
$children = $json['data'];
foreach ($children as $child){
$link_karma= $child['link_karma'];
}
A slight modification to pee2pee's post (His returned an undefined index error for me)
$string_reddit = file_get_contents("http://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);
$children = $json['data'];
$user = [];
foreach ($children as $key => $value)
{
$user[$key] = $value;
}
echo $user['name']; //Now you can use the $user array to access all the properties!
This works for me ->
Related
I am trying to create a small function to load data from different API endpoints into one array to work with / load data from. Right now my code is looking as follow;
$url1 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/uhmotNBAQ6-0vi9PFysRBg/members';
$url2 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/DWkD4B0uSsGQoryrz8Nuwg/members';
$url3 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/wSbCgimqTPy470n-wDQXPQ/members';
$url4 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/nmc0HQW-TZirTlnGzwbF-w/members';
$url5 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/WpV4yaVxSLW8QXH2Be40cA/members';
$api_endpoint1 = json_decode(file_get_contents($url1), true);
$api_endpoint2 = json_decode(file_get_contents($url2), true);
$api_endpoint3 = json_decode(file_get_contents($url3), true);
$api_endpoint4 = json_decode(file_get_contents($url4), true);
$api_endpoint5 = json_decode(file_get_contents($url5), true);
$merged_data = array();
foreach ($api_endpoint1 as $data) {
foreach ($api_endpoint2 as $data2) {
$merged_data[] = array_merge($data, $data2[$data['Name']]);
}
}
var_dump($merged_data);
It doesn't work as I tried so many things the past two days, I came to search for help.
Huge thanks to #barrycarter for his help in pushing me into the right direction.
The actual code needed to be like this:
$url1 = 'gameinfo.albiononline.com/api/…';
$url2 = 'gameinfo.albiononline.com/api/…';
$url3 = 'gameinfo.albiononline.com/api/…';
$url4 = 'gameinfo.albiononline.com/api/…';
$url5 = 'gameinfo.albiononline.com/api/…';
$merged_urls = [$url1, $url2, $url3, $url4, $url5];
$api_endpoint = [];
foreach ($merged_urls as $urls) {
$api_endpoint[] = file_get_contents($urls);
}
// Gives all DATA from all Guilds members
// var_dump($api_endpoint);
$merged_arrays = array_merge(json_decode($api_endpoint[0], true), json_decode($api_endpoint[1], true), json_decode($api_endpoint[2], true), json_decode($api_endpoint[3], true), json_decode($api_endpoint[4], true));
foreach ($merged_arrays as $query) {
echo $query['Name'];
echo '<br>';
}
I'm a little confused, and I need help.
I have a foreach loop in my code below. I need to put the foreach loop into the $data = array('image' => $final_image);
How can i put foreach loop in array ? Anyone can help me please.
$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage);
foreach($ExploreImage as $a) {
$newdata=$Post->Post_GetUploadChatImageID($a);
if($newdata){
$final_image=$base_url."uploads/images/".$newdata['uploaded_image'];
}
echo $final_image;
}
if($GetNewMessageU){
$json = array();
$data = array(
'image' => $final_image,
);
$result = json_encode( $data );
echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result);
}
$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage);
// Create an array to store final images
$final_images = [];
foreach($ExploreImage as $a) {
$newdata=$Post->Post_GetUploadChatImageID($a);
if($newdata){
$final_image=$base_url."uploads/images/".$newdata['uploaded_image'];
// Save the image if its new data.
$final_images[]= $final_image;
}
}
if($GetNewMessageU){
$json = array();
$data = array(
'image' => $final_images, // We pass the final images array
);
$result = json_encode( $data );
echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result);
}
Made things a bit more verbose.
First define the array.
Then in the if statement add element to the array.
$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage);
$final_images = array(); // Define object as array
foreach($ExploreImage as $a) {
$newdata=$Post->Post_GetUploadChatImageID($a);
if($newdata){
$final_image=$base_url."uploads/images/".$newdata['uploaded_image'];
$final_images[] = array('image' => $final_image); // Will create new array item
}
echo $final_image;
}
if($GetNewMessageU){
$json = array();
$data = array(
'image' => $final_image,
);
$result = json_encode( $data );
echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result);
}
I've got this data: http://api.tvmaze.com/shows?page=0
What i am trying to achieve is looping over the data and echo each id. This is the code i used:
<?php
$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);
$response = json_decode($json, TRUE);
foreach( $response as $serie){
foreach( $serie as $info => $value){
echo $info->$value["id"];
}
}
I don't really know what i am doing wrong.. Do you guys have any idea?
Greetz,
Try below code for getting id and url. You have to use array because you are passing TRUE in json_decode().
<?php
$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);
$response = json_decode($json, TRUE);
foreach( $response as $serie)
{
echo $serie['id']."->".$serie['url']."<br>";
}
?>
Keep it simple to fetch ids you wanted,
$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);
$response = json_decode($json, TRUE);
echo "<pre>";
foreach ($response as $value) {
echo $value['id']."<br/>"; // you will get ids in here only
}
how can I get the same results/values in one loop rather than two?
$json = file_get_contents($url)
$data = json_decode($json, true);
$desc = $data["descriptions"];
$assets = $data["assets"];
foreach ($assets as $assItem) {
echo $assItem["assetid"];
}
foreach($desc as $descItem) {
echo descItem["name"];
}
I've tried something like
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach ($data as $item) {
echo $item["assets"]["assetid"];
echo $item["descriptions"]["name"];
}
pastebin to the json: https://pastebin.com/raw/uA9mvE2e
You could do something like:
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach ($data['assets'] as $k => $item) {
echo $item["assetid"];
echo $data["descriptions"][$k]["name"];
}
This assumes that $data['assets'] and $data['descriptions'] share the same indices.
I am attempting to fetch JSON from Instagram according to a number of URL parameters, the JSON is then decoded and then the objects required are then encoded into my own JSON format. Whilst I know this sound a little ridiculous, it is what is required. My only issue here is that for some reason it does not encode each section of JSON, it will only work for one item. The code is as below.
<?php
function instagram($count=16){
$igtoken = $_GET['igtoken'];
$hashtag = $_GET['hashtag'];
$url = 'https://api.instagram.com/v1/tags/'.$hashtag.'/media/recent/?access_token='.$igtoken.'&count='.$count;
$jsonData = json_decode((file_get_contents($url)));
$jsonData = json_decode((file_get_contents($url)));
foreach ($jsonData->data as $key=>$value) {
$response = array();
$response["data"] = array();
$data = array();
$data["createdtime"] = $value->caption->created_time;
$data["username"] = $value->caption->from->username;
$data["profileimage"] = $value->caption->from->profile_picture;
$data["caption"] = $value->caption->text;
$data["postimage"] = $value->images->standard_resolution->url;
array_push($response["data"], $data);
$result = json_encode($response);
}
return $result;
}
echo instagram();
?>
It will work for each section of JSON if I do something like this instead:
$result .= '<li>
'.$value->caption->from->username.'<br/>
'.$value->caption->from->profile_picture.'<br/>
'.$value->caption->text.'<br/>
'.$value->images->standard_resolution->url.'<br/>
'.$value->caption->created_time.'<br/>
</li>';
I feel I have bodged up somewhere with the array, however i'm not entirely sure.
What if we move $response["data"] and $result varia out of foreach?
Have you tried this?
$response = array();
$response["data"] = array();
foreach ($jsonData->data as $key=>$value) {
$data = array();
$data["createdtime"] = $value->caption->created_time;
$data["username"] = $value->caption->from->username;
$data["profileimage"] = $value->caption->from->profile_picture;
$data["caption"] = $value->caption->text;
$data["postimage"] = $value->images->standard_resolution->url;
array_push($response["data"], $data);
}
$result = json_encode($response);
return $result;