Retrieve videoId From JSON Using PHP - php

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'].',';
}

Related

Get last item URL

I have a data in following JSON sort of format in which I am trying to only get the very last URL which in my following example is https://lastexample.com/images/I/4162nAIaRCL.jpg . Is there an easy way I can retrieve this via PHP?
{"https://example.com/images/I/4162nAIaRCL._SX425_.jpg":[425,425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg":[355,355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg":[466,466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg":[450,450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg":[500,500]}
Try, decode it into an array (json_decode) then get the keys (array_keys), then the last entry (end/max).
<?php
$json = '{
"https://example.com/images/I/4162nAIaRCL._SX425_.jpg": [425, 425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg": [355, 355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg": [466, 466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg": [450, 450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg": [500, 500]
}';
$array = json_decode($json, true);
$keys = array_keys($array);
echo end($keys); // https://lastexample.com/images/I/4162nAIaRCL.jpg
https://3v4l.org/MLbdt
Try this :
<?php
$json = '{
"https://example.com/images/I/4162nAIaRCL._SX425_.jpg":[425,425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg":[355,355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg":[466,466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg":[450,450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg":[500,500]
}';
$arr = json_decode($json,true);
$arrkeys = array_keys($arr);
$lastkey = end($arrkeys);
echo $lastkey;
?>

Loop over Array in array

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
}

PHP JSON multiple echo value

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

JSON decode to PHP not working

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;
?>

Getting content excerpt Wikipedia with JSON & PHP

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]['*']);
}

Categories