JSON data capture with PHP

Asked

Viewed 780 times

1

I have the following JSON file:

{
    "version": "1.0",
    "encoding": "UTF-8",
    "entry": {
        "xmlns": "http://www.w3.org/2005/Atom",
        "xmlns$gd": "http://schemas.google.com/g/2005",
        "xmlns$yt": "http://gdata.youtube.com/schemas/2007",
        "xmlns$media": "http://search.yahoo.com/mrss/",
        "gd$etag": "W/\"CkYBQn47eCp7I2A9XRdbE0U.\"",
        "id": {
            "$t": "tag:youtube.com,2008:video:X9sNcWUuIoY"
        },
        "published": {
            "$t": "2014-12-12T10:29:13.000Z"
        },
        "updated": {
            "$t": "2014-12-12T10:29:13.000Z"
        },
        "category": [{
                "scheme": "http://schemas.google.com/g/2005#kind",
                "term": "http://gdata.youtube.com/schemas/2007#video"
            }, {
                "scheme": "http://gdata.youtube.com/schemas/2007/categories.cat",
                "term": "Games",
                "label": "Jogos"
            }],
        "title": {
            "$t": "Minecraft Premium Account Generator December 2014 No Survey"
        },
        "content": {
            "type": "application/x-shockwave-flash",
            "src": "https://www.youtube.com/v/X9sNcWUuIoY?version=3&f=videos&app=youtube_gdata"
        },
        "link": [{
                "rel": "alternate",
                "type": "text/html",
                "href": "https://www.youtube.com/watch?v=X9sNcWUuIoY&feature=youtube_gdata"
            }, {
                "rel": "http://gdata.youtube.com/schemas/2007#video.related",
                "type": "application/atom+xml",
                "href": "https://gdata.youtube.com/feeds/api/videos/X9sNcWUuIoY/related?v=2"
            }, {
                "rel": "http://gdata.youtube.com/schemas/2007#mobile",
                "type": "text/html",
                "href": "https://m.youtube.com/details?v=X9sNcWUuIoY"
            }, {
                "rel": "http://gdata.youtube.com/schemas/2007#uploader",
                "type": "application/atom+xml",
                "href": "https://gdata.youtube.com/feeds/api/users/U9JGqO-lDU1zxB_afGr99g?v=2"
            }, {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "https://gdata.youtube.com/feeds/api/videos/X9sNcWUuIoY?v=2"
            }],
        "author": [{
                "name": {
                    "$t": "Pijdusa Goruk"
                },
                "uri": {
                    "$t": "https://gdata.youtube.com/feeds/api/users/U9JGqO-lDU1zxB_afGr99g"
                },
                "yt$userId": {
                    "$t": "U9JGqO-lDU1zxB_afGr99g"
                }
            }],
        "yt$accessControl": [{
                "action": "comment",
                "permission": "allowed"
            }, {
                "action": "commentVote",
                "permission": "allowed"
            }, {
                "action": "videoRespond",
                "permission": "moderated"
            }, {
                "action": "rate",
                "permission": "allowed"
            }, {
                "action": "embed",
                "permission": "allowed"
            }, {
                "action": "list",
                "permission": "allowed"
            }, {
                "action": "autoPlay",
                "permission": "allowed"
            }, {
                "action": "syndicate",
                "permission": "allowed"
            }],
        "gd$comments": {
            "gd$feedLink": {
                "rel": "http://gdata.youtube.com/schemas/2007#comments",
                "href": "https://gdata.youtube.com/feeds/api/videos/X9sNcWUuIoY/comments?v=2",
                "countHint": 0
            }
        },
        "media$group": {
            "media$category": [{
                    "$t": "Games",
                    "label": "Jogos",
                    "scheme": "http://gdata.youtube.com/schemas/2007/categories.cat"
                }],
            "media$content": [{
                    "url": "https://www.youtube.com/v/X9sNcWUuIoY?version=3&f=videos&app=youtube_gdata",
                    "type": "application/x-shockwave-flash",
                    "medium": "video",
                    "isDefault": "true",
                    "expression": "full",
                    "duration": 80,
                    "yt$format": 5
                }, {
                    "url": "rtsp://r2---sn-jc47eu7e.c.youtube.com/CiILENy73wIaGQmGIi5lcQ3bXxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
                    "type": "video/3gpp",
                    "medium": "video",
                    "expression": "full",
                    "duration": 80,
                    "yt$format": 1
                }, {
                    "url": "rtsp://r2---sn-jc47eu7e.c.youtube.com/CiILENy73wIaGQmGIi5lcQ3bXxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
                    "type": "video/3gpp",
                    "medium": "video",
                    "expression": "full",
                    "duration": 80,
                    "yt$format": 6
                }],
            "media$credit": [{
                    "$t": "U9JGqO-lDU1zxB_afGr99g",
                    "role": "uploader",
                    "scheme": "urn:youtube",
                    "yt$display": "Pijdusa Goruk"
                }],
            "media$description": {
                "$t": "Legit Download Link : http://bit.ly/legit-link\nFree Download\nhttp://bit.ly/legit-link\n\nor\n\nhttp://bit.ly/legit-link\n\nExtra tags :\n\nfree minecraft account 2014,\nfree minecraft premium account,\nfree minecraft premium account generator,\nget minecraft premium,\nhoe o make minecraft account,\nminecraft account,\nminecraft account password,\nminecraft account card,\nminecraft account checker,\nminecraft account code,\nminecraft account code pc,\nminecraft account cracker,\nminecraft account emails and passwords,\nminecraft account free,\nminecraft account generator,\nminecraft account generator no survey or password,\nminecraft account generator no survey,\nminecraft account gift card,\nminecraft account giveaway,\nminecraft account hacker,\nminecraft account info,\nminecraft account list 2014,\nminecraft account login,\nminecraft account names,\nminecraft account pc,\nminecraft account premium,\nminecraft account setup,\nminecraft account\nminecraft account and passwords\nminecraft account and passwords list 2014\nminecraft account and passwords list free\nminecraft account free 2014\nminecraft premium 2014\nminecraft premium account\nminecraft premium account card\nminecraft premium account code\nminecraft premium account free\nminecraft premium account generator\nminecraft premium account generator .exe\nminecraft premium account 1.6.2\nminecraft premium account generator 2014\nminecraft premium account generator no survey\nminecraft premium account generator no survey no password\nminecraft premium account generator download\nminecraft premium account generator free\nminecraft premium account generator mac\nminecraft premium account generator no download\nminecraft premium account generator no survey\nminecraft premium account generator no survey no password\nminecraft premium account generator no surveys 2014\nminecraft premium account generator 2014\nminecraft premium account generator online\nminecraft premium account generator torrent\nminecraft premium account generator update 2014",
                "type": "plain"
            },
            "media$keywords": {},
            "media$license": {
                "$t": "youtube",
                "type": "text/html",
                "href": "http://www.youtube.com/t/terms"
            },
            "media$player": {
                "url": "https://www.youtube.com/watch?v=X9sNcWUuIoY&feature=youtube_gdata_player"
            },
            "media$thumbnail": [{
                    "url": "https://i.ytimg.com/vi/X9sNcWUuIoY/default.jpg",
                    "height": 90,
                    "width": 120,
                    "time": "00:00:40",
                    "yt$name": "default"
                }, {
                    "url": "https://i.ytimg.com/vi/X9sNcWUuIoY/mqdefault.jpg",
                    "height": 180,
                    "width": 320,
                    "yt$name": "mqdefault"
                }, {
                    "url": "https://i.ytimg.com/vi/X9sNcWUuIoY/hqdefault.jpg",
                    "height": 360,
                    "width": 480,
                    "yt$name": "hqdefault"
                }, {
                    "url": "https://i.ytimg.com/vi/X9sNcWUuIoY/sddefault.jpg",
                    "height": 480,
                    "width": 640,
                    "yt$name": "sddefault"
                }, {
                    "url": "https://i.ytimg.com/vi/X9sNcWUuIoY/1.jpg",
                    "height": 90,
                    "width": 120,
                    "time": "00:00:20",
                    "yt$name": "start"
                }, {
                    "url": "https://i.ytimg.com/vi/X9sNcWUuIoY/2.jpg",
                    "height": 90,
                    "width": 120,
                    "time": "00:00:40",
                    "yt$name": "middle"
                }, {
                    "url": "https://i.ytimg.com/vi/X9sNcWUuIoY/3.jpg",
                    "height": 90,
                    "width": 120,
                    "time": "00:01:00",
                    "yt$name": "end"
                }],
            "media$title": {
                "$t": "Minecraft Premium Account Generator December 2014 No Survey",
                "type": "plain"
            },
            "yt$aspectRatio": {
                "$t": "widescreen"
            },
            "yt$duration": {
                "seconds": "80"
            },
            "yt$uploaded": {
                "$t": "2014-12-12T10:29:13.000Z"
            },
            "yt$uploaderId": {
                "$t": "UCU9JGqO-lDU1zxB_afGr99g"
            },
            "yt$videoid": {
                "$t": "X9sNcWUuIoY"
            }
        },
        "yt$statistics": {
            "favoriteCount": "0",
            "viewCount": "1"
        }
    }
}

I want to capture, with PHP, the name of the author, which in the case is in the node author 0 name $t.

The code I have so far:

<?php

// código do vídeo 
$video_ID = 'your-video-ID';

// substitua pelo código do vídeo 
// trazendo o json com as informações do vídeo
$JSON = file_get_contents("gdata.youtube.com/feeds/api/videos{$video_ID}?v=2&alt=json");

// decodificando e transformado o json em um objeto / array 
$JSON_Data = json_decode($JSON);

// guardando em uma variável a quantidade de views 
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};

// exibindo na tela 
echo $views;

?> 
  • Friend, what have you tried in PHP? Already have some code?

  • If you give a Json.Parse("your file") you can turn your json into a list of objects?

3 answers

2


Use the function json_decode and pass true in the second argument to turn the return into an associative array. Then call the key you want.

$arr = json_decode($json, true);
echo $arr['entry']['author'][0]['name']['$t'];

2

If you have the string already stored in a variable just use the function json_decode of PHP.

This function will convert the string to JSON for a array.

Then staying that way

$parseObj = json_decode($json); //Tipo objeto
$parseArray = json_decode($json, true); //Tipo Array

echo $parseObj->entry->author[0]->name->{'$t'}; //Pijdusa Goruk
echo $parseArray['entry']['author'][0]['name']['$t']; //Pijdusa Goruk

2

PHP has functions to work with JSON. What you’re looking for is json_decode.

To get the desired key, just navigate between the array indexes

<?php

$content = file_get_contents('myJson.json');

// Segundo argumento 'true' retorna um array associativo
// ao invés de um objeto em stdClass
$array = json_decode($content, true);
echo $array['entry']['author'][0]['name']['$t'];

// Se preferir, pode usar a notação em objeto
$obj = json_decode($content);
echo $obj->entry->author[0]->name->{'$t'};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.