I am trying to get propriety of Json Decoded string of twitch tv
$hue = file_get_contents('https://api.twitch.tv/kraken/streams/?channel=starladder1');
$hue = json_decode($hue);
print_r($hue->display_name);
but it doesnt work tryed almost everything please help
Try following code:
<?php
$hue = file_get_contents('https://api.twitch.tv/kraken/streams/?channel=starladder1');
$hue1 = json_decode($hue, TRUE);
foreach ($hue1 as $data)
{
foreach ($data as $datas) {
echo ($datas['channel']['display_name']."<br/>");
}
}
?>
the reason why it is not working is because you try to access "display_name" directly without analyzing the structure of the object.
Try this:
print_r($hue->streams[0]->channel->display_name);
You see that streams begins with an "[" which means that its elements are accessed like an array
Your object really looks like this, and this helps you to understand the structure better:
{
"streams":[{
"_id":10954982848,
"game":"Dota 2",
"viewers":11918,
"_links":{
"self":"https://api.twitch.tv/kraken/streams/starladder1"
},
"preview":{
"small":"http://static-cdn.jtvnw.net/previews-ttv/live_user_starladder1-80x50.jpg",
"medium":"http://static-cdn.jtvnw.net/previews-ttv/live_user_starladder1-320x200.jpg",
"large":"http://static-cdn.jtvnw.net/previews-ttv/live_user_starladder1-640x400.jpg",
"template":"http://static-cdn.jtvnw.net/previews-ttv/live_user_starladder1-{width}x{height}.jpg"
},
"channel":{
"_links":{
"self":"http://api.twitch.tv/kraken/channels/starladder1",
"follows":"http://api.twitch.tv/kraken/channels/starladder1/follows",
"commercial":"http://api.twitch.tv/kraken/channels/starladder1/commercial",
"stream_key":"http://api.twitch.tv/kraken/channels/starladder1/stream_key",
"chat":"http://api.twitch.tv/kraken/chat/starladder1",
"features":"http://api.twitch.tv/kraken/channels/starladder1/features",
"subscriptions":"http://api.twitch.tv/kraken/channels/starladder1/subscriptions",
"editors":"http://api.twitch.tv/kraken/channels/starladder1/editors",
"videos":"http://api.twitch.tv/kraken/channels/starladder1/videos",
"teams":"http://api.twitch.tv/kraken/channels/starladder1/teams"
},
"background":null,
"banner":null,
"display_name":"starladder1",
"game":"Dota 2",
"logo":"http://static-cdn.jtvnw.net/jtv_user_pictures/starladder1-profile_image-557367f831a49ebb-300x300.png",
"mature":false,
"status":"NewBee vs LGD-Gaming 1:0 # WEC Lan-Finals Day 2 by v1lat",
"url":"http://www.twitch.tv/starladder1",
"video_banner":"http://static-cdn.jtvnw.net/jtv_user_pictures/starladder1-channel_offline_image-c29311bb34830472-640x360.png",
"_id":28633177,
"name":"starladder1",
"created_at":"2012-03-01T18:05:14Z",
"updated_at":"2014-09-06T06:59:23Z",
"abuse_reported":null,
"delay":0,
"followers":118574,
"profile_banner":null,
"profile_banner_background_color":null,
"views":186419614,"language":"en"
}
}],
"_total":1,
"_links":{
"self":"https://api.twitch.tv/kraken/streams?channel=starladder1&limit=25&offset=0",
"next":"https://api.twitch.tv/kraken/streams?channel=starladder1&limit=25&offset=25",
"featured":"https://api.twitch.tv/kraken/streams/featured",
"summary":"https://api.twitch.tv/kraken/streams/summary",
"followed":"https://api.twitch.tv/kraken/streams/followed"
}
}
Here is code for you:
$hue = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/starladder1'));
echo "Name :" .$hue->display_name;
You just do wrong url to fetch
Related
I got some data in my data/clients.json file and I'm trying to reach for some of the keys and values and display them in my php file. It does not work and i can not figure out why.
My clients.json file looks like:
{
"data": {
"nikajakubec": {
"name": "nika",
"loans": {
"amount": "1000",
"reason": "for garden"
}
}
}
}
And my PHP file:
<?php
$sData = file_get_contents('data/clients.json');
$jData = json_decode($sData);
$jClient = $jData->data->$sUsername;
foreach ($jClient->loans as $jLoans) {
echo "
<div>
<div>Amount:$jLoans->loans-></div>
<div>Reason:$sUserName->reason</div>
</div>";
}
?>
Do like this:-
<?php
$json = '{
"data": {
"nikajakubec": {
"name": "nika",
"loans": {
"amount": "1000",
"reason": "for garden"
}
}
}
}';
$jData = json_decode($json);
foreach ($jData->data as $user) {
echo "
<div>
<div>Amount:". $user->loans->amount ."</div>
<div>Reason: ". $user->loans->reason ."</div>
</div>";
}
Output:- https://3v4l.org/8HPV5
The field reason it's actually nested inside the loans prop, not directly in the user.
so instead of
$sUserName->reason
just go
$jLoans->reason
And also go $jLoans->amount instead of $jLoans->loans->
Consider that when you cycle $jClient->loans you are going to work on each prop of loans, which are 'amount' and 'reason'
I have a simple JSON and want to read in PHP. I am certainly missing something in array, can anybody point out my mistake. Its been considerable time I am playing with this simple thing.
Here is the JSON & php :
$string='[
{
"phone":"+91009999000",
"name":"abcd",
"typeid":1
}
{
"phone":"+91009999222",
"name":"efg",
"typeid":2
}
{
"phone":"+91009999444",
"name":"hijhl",
"typeid":1
}
]';
$json_a = json_decode($string,true);
$phone = $json_a[0]['phone'];
$full_name=$json_a[0]['courseid'];
echo "phone = " . $phone;
echo "<br>fullname = " . $full_name;
You are missing commas near curly braces.
It should be like this:
$string='[
{
"phone":"+91009999000",
"name":"abcd",
"typeid":1
},
{
"phone":"+91009999222",
"name":"efg",
"typeid":2
},
{
"phone":"+91009999444",
"name":"hijhl",
"typeid":1
}
]';
I have a file that contains the following HL7 Information :
{
MESSAGE_HEADER: {
SENDING_APPLICATION: 'IQCARE',
SENDING_FACILITY: '10829',
RECEIVING_APPLICATION: 'IL',
RECEIVING_FACILITY: '10829',
MESSAGE_DATETIME: '20170713110000',
SECURITY: '',
MESSAGE_TYPE: 'ADT^A04',
PROCESSING_ID: 'P'
},
PATIENT_IDENTIFICATION: {
EXTERNAL_PATIENT_ID: {
ID: '110ec58a-a0f2-4ac4-8393-c866d813b8d1',
IDENTIFIER_TYPE: 'GODS_NUMBER',
ASSIGNING_AUTHORITY: 'MPI'
}}}
I want to convert this message to a json object and I did the following :
// copy file content into a string var
$json_file = file_get_contents("" . getcwd() . "\integration_layer\ADT^A04 - Patient Registration.json");
echo gettype($json_file);
// convert the string to a json object
$jfo = json_decode($json_file);
// read the title value
$title = $jfo->MESSAGE_HEADER->SENDING_APPLICATION;
// copy the posts array to a php var
$posts = $jfo->PATIENT_IDENTIFICATION->EXTERNAL_PATIENT_ID;
// listing posts
foreach ($posts as $post) {
echo $post->ID;
}
But I get the following error :
Severity: Notice
Message: Trying to get property of non-object
When I user the getype function of PHP on the $json_file , it is a string file.
How can I convert the message to an object for my own system consumption ?
Please validate your JSON code.
JSON rules
Data is in name/value pairs
Data is separated by commas
Curly braces hold objects - Your file contains no parent object
Square brackets hold arrays
A name/value pair consists of a field name (in double quotes). -
Your name fields are not in double quotes
Valid JSON code:
{
"MESSAGE_HEADER": {
"SENDING_APPLICATION": "IQCARE",
"SENDING_FACILITY": 10829,
"RECEIVING_APPLICATION": "IL",
"RECEIVING_FACILITY": 10829,
"MESSAGE_DATETIME": "20170713110000",
"SECURITY": "",
"MESSAGE_TYPE": "ADT^A04",
"PROCESSING_ID": "P"
},
"PATIENT_IDENTIFICATION": {
"EXTERNAL_PATIENT_ID": {
"ID": "110ec58a-a0f2-4ac4-8393-c866d813b8d1",
"IDENTIFIER_TYPE": "GODS_NUMBER",
"ASSIGNING_AUTHORITY": "MPI"
}
}
}
Working PHP example with valid JSON code:
<?php
$json = '
{
"MESSAGE_HEADER": {
"SENDING_APPLICATION": "IQCARE",
"SENDING_FACILITY": 10829,
"RECEIVING_APPLICATION": "IL",
"RECEIVING_FACILITY": 10829,
"MESSAGE_DATETIME": "20170713110000",
"SECURITY": "",
"MESSAGE_TYPE": "ADT^A04",
"PROCESSING_ID": "P"
},
"PATIENT_IDENTIFICATION": {
"EXTERNAL_PATIENT_ID": {
"ID": "110ec58a-a0f2-4ac4-8393-c866d813b8d1",
"IDENTIFIER_TYPE": "GODS_NUMBER",
"ASSIGNING_AUTHORITY": "MPI"
}
}
}
';
$object = json_decode($json);
echo $object->MESSAGE_HEADER->SENDING_APPLICATION;
?>
This is the JSON data I get from our ticket-system. I would like to parse it with PHP and save the data in a database to create statistics and a dashboard so everyone can see how many tickets are open and the latest closed tickets.
I can read some of the data but not everything.
{
"address":"belgium",
"workers":{
"peter":{
"worker":"peter",
"open_close_time":"45.6 T/h",
"closed_tickets":841,
"open_tickets":7,
"last_checkin":1498768133,
"days_too_late":0
},
"mark":{
"worker":"mark",
"open_close_time":"45.9 T/h",
"closed_tickets":764,
"open_tickets":2,
"last_checkin":1498768189,
"days_too_late":0
},
"walter":{
"worker":"walter",
"open_close_time":"20.0 T/h",
"closed_tickets":595,
"open_tickets":4,
"last_checkin":1498767862,
"days_too_late":0
}
},
"total_tickets":2213,
"tickets":[
{
"id":2906444760,
"client":"297",
"processed":0
},
{
"id":2260,
"client":"121",
"processed":0
},
{
"id":2424,
"client":"45",
"processed":0
}
],
"last_closed_tickets":[
{
"id":2259,
"client":"341",
"closed_on":"2017-06-25T10:11:00.000Z"
},
{
"id":2258,
"client":"48",
"closed_on":"2017-06-20T18:37:03.000Z"
}
],
"settings":{
"address":"belgium",
"email":"",
"daily_stats":0
},
"open_close_time":"161.1 T/h",
"avgopen_close_time":123298,
"ticket_time":"27.1 T/h",
"stats":{
"time":1498768200087,
"newest_ticket":1498768189000,
"closed_tickets":2200,
"open_tickets":13,
"active_workers":3
},
"avg_paid_tickets":64.55,
"avg_afterservice_tickets":35.45
}
This is the PHP code I tried to get the names of the worker but this doesn't work.
<?php
$string = file_get_contents("example.json");
$json = json_decode($string, true);
echo $json['address'];
foreach($json->workers->new as $entry) {
echo $entry->worker;
}
?>
If I try it like here below it works but then I've got to change the code everytime another employee starts.
echo $json['workers']['mark']['closed_tickets'];
<?php
$string = file_get_contents("example.json");
$json = json_decode($string, true);
foreach($json['workers'] as $entry){
echo $entry['worker'] . " has " . $entry['open_tickets'] . " tickets" . "\n";
}
?>
I tried to print the json out put in my php page from facebook graph url .
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<?php
$jsonurl = "https://graph.facebook.com/search?q=%23IIFA&access_token=TOKEN_GOES_HERE";
$json = file_get_contents($jsonurl);
$json_output = json_encode($json);
echo $json_output;
?>
Out put :
"data":[
{
"id":"1003224",
"from":{
"id":"1000042242",
"name":"abc"
},
"message":"#abc",
"privacy":{
"value":""
},
"type":"status",
"created_time":"2014-06-09T09:49:58+0000",
"updated_time":"2014-06-09T09:49:58+0000"
}
],
This page give out put as json data and i want to add the data into html layout /table
Please advice me how to proceed
UPDATE
$json=array();
foreach ($json_output as $json_result) {
$json[] = array(
'value' => $json_result["name"]
'value' => $json_result["message"],
);
echo $json_result['name'];
echo $json_result['message'];
}
You're not currently navigating the JSON properly.
if your output looks like this:
"data":[
{
"id":"1003224",
"from":{
"id":"1000042242",
"name":"abc"
},
"message":"#abc",
"privacy":{
"value":""
},
"type":"status",
"created_time":"2014-06-09T09:49:58+0000",
"updated_time":"2014-06-09T09:49:58+0000"
}
],
JSON does not search all of its levels to find the thing you're asking for, you need to tell it exactly where to look. So, for instance, in order to get "abc" you need to echo
$json_result["data"][0]["from"]["name"]
and if you want to get "#abc" you need to echo
$json_result["data"][0]["message"]
EDIT:
try this:
$json=array();
foreach ($json_output["data"] as $json_result) {
$json[] = array(
'value' => $json_result["from"]["name"],
'value' => $json_result["message"]
);
echo $json_result["from"]['name'];
}