I have tried many different variations here - how can I get the content summary from this: http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c
Here's what I have so far:
$wiki_url = "http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c.json";
$json = file_get_contents($wiki_url);
$data = json_decode($json, TRUE);
I know I need to do something like echo $data[0]->query but am unable to get it to do anything.
$wiki_url = "http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c.json";
$json = file_get_contents($wiki_url);
$data = json_decode($json, TRUE);
foreach ($data['query']['pages'] as $page) {
print_r($page['revisions'][0]['*']);
}
Related
I want to get the imdb_id from inside json using foreach
i am having an issue with getting it decoded and displayed.
My php code so far:
$themoviedburl = "http://api.themoviedb.org/3/tv/60625/external_ids?api_key=";
$get = file_get_contents($themoviedburl);
$decode1 = json_decode($get, TRUE);
foreach($decode1['external_ids'] as $value){
$imdb = $value['imdb_id'];
}
Json:
{"id":60625,"imdb_id":"tt2861424","freebase_mid":"/m/0z6p24j","freebase_id":null,"tvdb_id":275274,"tvrage_id":33381,"facebook_id":"RickandMorty","instagram_id":"rickandmorty","twitter_id":"RickandMorty"}
After $decode1 = json_decode($get, TRUE);
You can simply do $imdb_ids = array_column($decode1['external_ids'],'imdb_id'); to collect all imdb_id in one array.
More info on array_column : https://www.php.net/manual/en/function.array-column.php
As discussed in the comments, to get only one imdb_id ID, you can do like below:
$json = '{"id":60625,"imdb_id":"tt2861424","freebase_mid":"/m/0z6p24j","freebase_id":null,"tvdb_id":275274,"tvrage_id":33381,"facebook_id":"RickandMorty","instagram_id":"rickandmorty","twitter_id":"RickandMorty"}';
$decoded_data = json_decode($json,true);
$imdb_id = $decoded_data['imdb_id'];
Link To JSON File: https://pastebin.com/gXcnNnUK
Code I've Tried So Far:
$json = json_decode($json, true);
foreach($json as $data){
echo $data['videoId'];
}
But it's not returning values. Is there any better approach to do it?
On line 3, did you mean to iterate over $arr instead of $json?
$json = file_get_contents('json.json');
$arr = json_decode($json, true);
foreach($arr['contents']['sectionListRenderer']['contents']['2']['itemSectionRenderer']['contents'] as $pero){
$video_id_list .= $pero['compactVideoRenderer']['videoId'].',';
}
How can I get the source, title, issn, author, ... from a json file: JSON file
We tried with:
$new_pmid = $_POST['new_pmid'];
$api_json_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=".$new_pmid."&retmode=json";
$json = file_get_contents($api_json_url);
$data = json_decode($json, TRUE);
echo $header[0]->result->$new_pmid->title;
....
But nothing happen...
Can you give me the solution for the json file (generated from pubmed database).
Thank you.
You didn't use the $data variable, which stored the decoded data
You decode JSON into $data as array
$title = $data['result'][$new_pmid]['title'];
$issn = $data['result'][$new_pmid]['issn'];
$authors = $data['result'][$new_pmid]['authors'];
--Update--
To get $authors name ,authtype ,... use foreach loop:
foreach($authors as $author){
$name = $author['name'];
$authtype = $author['authtype'];
$clusterid = $author['clusterid'];
}
In addition to the question yesterday: Question
I got multiple (3) Items in the json file:
results.json
["{\"Produkt\":{\"Produktkategorie\":\"Artikel1\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante1\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel2\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante2\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel3\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante3\"}}}"]
I want echo all three value from "Produktkategorie" and the value "MaxBreite"
It should look like this:
Artikel1 - 250
Artikel2 - 250
Artikel3 - 250
My code looks like this:
$json = file_get_contents('results.json');
$json = json_decode($json, true);
$anzahl = count($json) -1;
$anzahlstart = 0;
while ($anzahlstart < $anzahl) {
$json = json_decode($json[$anzahlstart], true);
$ProduktkategorieFile = $json['Produkt']['Produktkategorie'];
$MaxBreiteFile = $json['Produkt']['Optionen']['MaxBreite'];
echo $ProduktkategorieFile. "-" .$MaxBreiteFile;
$anzahlstart ++;
}
Unfortunately my Code throws a error after passing first line:
Notice: Undefined offset: 1 in
After that I don't get any result.
Kings of coding, could you help me again please :)
Do you need like this?:-
<?php
$json_string = file_get_contents('results.json');
$json = json_decode($json_string, true);
// I hope the above line gives you exact json what you shown to us
foreach ($json as $jso){
$array = json_decode($jso, true);
echo $array['Produkt']['Produktkategorie'].' - '.$array['Produkt']['Optionen']['MaxBreite'];
echo PHP_EOL;
}
Output:-https://eval.in/728430
Note:- If yes then I hope you are able to get other values easily.Thanks
The problem is with the $json variable name: You're reassigning it on this line: $json = json_decode($json[$anzahlstart], true);
Rename this variable and you're good to go!
I would also replace the while loop with a foreach loop as shown in my example:
<?php
//With foreach
$original = '["{\"Produkt\":{\"Produktkategorie\":\"Artikel1\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante1\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel2\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante2\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel3\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante3\"}}}"]';
$decoded = json_decode($original);
foreach($decoded as $encodedProduct){
$product = json_decode($encodedProduct,true)['Produkt'];
echo $product['Produktkategorie'] . " - " . $product['Optionen']['MaxBreite'] . "\n";
}
//Original fixed code
$json = json_decode($original, true);
$anzahl = count($json);
$anzahlstart = 0;
while ($anzahlstart < $anzahl) {
$decodedJson = json_decode($json[$anzahlstart], true);
$ProduktkategorieFile = $decodedJson['Produkt']['Produktkategorie'];
$MaxBreiteFile = $decodedJson['Produkt']['Optionen']['MaxBreite'];
echo $ProduktkategorieFile. " - " .$MaxBreiteFile . "\n";
$anzahlstart ++;
}
Your problem is, that you are trying to decode an array of json string, instead of the string itself.
so that would be like this now.
$json = file_get_contents('results.json');
$json = json_decode($json[0], true); // notice [0];on this line.
...
After reading the other question, I have had this problem before, but you essentially need to do two things. in your ajax.
$.ajax({
...
data : JSON.stringify(data)
})
This changes an object into a json string,
Then on your server you do the decode.
something like this
$json = json_decode($jsonstringGoesHERE , true);
For more information in understanding the issue, have a look at this other post.
jQuery ajax, how to send JSON instead of QueryString
I try to put a JSON Object into a PHP Variable, it is not working. When I var_dump it, it says NULL.
PHP:
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$money = $data->tplc->stats; // -> What do i have to do to reach the id:GTA Online Cash and use the val: $33.9K ?
?>
JSON file example: (it changes over time)
{"cid":0,"ttl":"Grand Theft Auto V",
"ishs":false,"issa":false,"istc":false,"htl":true,"tlc":"text",
"tlt":"View Stats","tlh":"/member/drantifat/games/gtav/career/overview",
"iso":false,"cmng":false,"order":0,"uid":0,"ttc":0,
"tplc":"{\"game\":\"GTAV\",\"stats\":[{\"id\":\"Game Progress\",\"val\":\"58.77%\"},
{\"id\":\"Missions Passed\",\"val\":\"55\"},{\"id\":\"Playing Time\",\"val\":\"29:00:33\"},
{\"id\":\"GTA Online RP\",\"val\":\"2.4M\"},{\"id\":\"GTA Online Rank\",\"val\":\"128\"},
{\"id\":\"GTA Online Cash\",\"val\":\"$33.9K\"},
{\"id\":\"GTA Online Playing Time\",\"val\":\"529:44:12\"}],
\"url\":\"/member/drantifat/games/gtav/career/overview\"}","isa":false,"cnt":""}
Could anyone also help me with what I've mentioned in the PHP file?
tplc isn't a JSON object, it's a string that is itself JSON.
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$money = $tplc->stats;
?>
To get the money value specifically:
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$stats = $tplc->stats;
foreach ($tplc->stats as $stat) {
if ($stat->id == 'GTA Online Cash') {
$money = $stat->val;
break;
}
}
echo $money;
?>