json_decode file_get_contents help - php

<?php
$json = file_get_contents('http://tiny.cc/ttrhelp');
$obj = json_decode($json);
$example = $obj->rooms->displayName;
?>
Name: <?php echo $example; ?>
Trying to show the value for 'displayName' but its not showing

Untested code:
<?php
$json = file_get_contents('http://pub.tapulous.com/tapplications/coresocial/v1/chat/api/index.php?method=room_list');
$obj = json_decode($json);
foreach($obj->rooms as $room){
$example = $room->displayName;
echo $example;
}
?>

You probably want $obj->rooms[0]->displayName.
CodePad.

echo $obj->rooms[2]->displayName;

Try
echo $obj->rooms[0]->displayName;

Related

Transform output value to array in php

I need to transform an ouput value in array
I have this output
{ "ID":"5454126" ,"class":"ObjectClassRAJ" }
I just need to have the ID "5454126" how can i do it please ?
Thanks
Use json_decode()
$ch =json_decode($json,true);
echo $ch["ID"];
Output:-https://3v4l.org/Hsncf
Try json_decode:
<?php
$string = '{ "ID":"5454126" ,"class":"ObjectClassRAJ" }';
$obj = json_decode($string);
print_r($obj);
echo "<br>ID: ".$obj->ID;
echo "<br>class: ".$obj->class;
?>

Loading an Array Elements with a PHP variable

Hello There I'm having a problem creating an array
<?php
//I'm actually grabbing the list from MySQl
//$list = '"02","03"';
$friends_list_array = array($list);
echo $friends_list_array[0];
?>
This is the Code !
But It Doesn't Work
Expected Result : 02
Output what i got from above code : "02","03"
Someone help please ?
Use php explode() function:-
<?php
$list = '"02","03"';
$friends_list_array = explode(",",$list);
echo $friends_list_array[0];
?>
Output:-https://eval.in/839531
If you want output strictly 02:-
<?php
$list = '"02","03"';
$friends_list_array = explode(",",$list);
echo trim($friends_list_array[0], '"');
?>
Output:-https://eval.in/839537
You can try also this way:-
<?php
$friends_list_array = array(
"02",
"03"
);
echo $friends_list_array[0];

How to use PHP to fetch and decode JSON data?

How can I get variables "price_usd" and "price_btc" from JSON url https://api.coinmarketcap.com/v1/ticker/ethereum/? I wrote a script but nothing happens.
<?php
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$url = $tick;
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$usd = $data[0]["price_usd"];
echo $usd;
?>
Your code use a file_get_contents twice, look at would be:
<?php
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$url = $tick;
echo $url;
//$json = file_get_contents($url);
$data = json_decode($tick, TRUE);
$usd = $data[0]["price_usd"];
echo $usd;
?>
Try this
<?php
$json = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$data = json_decode($json);
var_dump($data[0]->price_usd);
?>

How to get number of video views with YouTube API V3?

i use this code to achieve this but this don't work :
<?php
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=hqepb5hzuB0&key={YOUR-API-KEY}");
$JSON_Data = json_decode($JSON);
$views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>
Thanks
Try something like this:
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=hqepb5hzuB0&key={YOUR-API-KEY}");
$json_data = json_decode($JSON, true);
echo $json_data['items'][0]['statistics']['viewCount'];

Get JSON object from URL with php

I have from url this
[{"user_id":"3932131","username":"DanielDimitrov","count300":"1677134","count100":"239025","count50":"41207","playcount":"17730","ranked_score":"1413977663","total_score":"7355146958","pp_rank":"35848","level":"95.5852","pp_raw":"1582.26","accuracy":"97.88556671142578","count_rank_ss":"42","count_rank_s":"337","count_rank_a":"120","country":"BG","events":[]}]
and my php is this
$json = file_get_contents('url');
$obj = json_decode($json);
echo $obj->user_id;
echo 'Username: '.$obj['username'].'<br>';
echo 'PP: '.(int)$obj['pp_raw'].'<br>';
echo 'Level: '.(int)$obj['level'].'<br>';
echo 'Play count: '.$obj['playcount'].'<br>';
I try to remove the brackets from the url and the code run but i get it with brackets... How can i remove them?
<?php
$json = '[{"user_id":"3932131","username":"DanielDimitrov","count300":"1677134","count100":"239025","count50":"41207","playcount":"17730","ranked_score":"1413977663","total_score":"7355146958","pp_rank":"35848","level":"95.5852","pp_raw":"1582.26","accuracy":"97.88556671142578","count_rank_ss":"42","count_rank_s":"337","count_rank_a":"120","country":"BG","events":[]}]';
$in = array("[{", "}]");
$out = array("{", "}");
$obj = json_decode(str_replace($in, $out, $json));
echo $obj->user_id;
echo 'Username: '.$obj->username.'<br>';
echo 'PP: '.(int)$obj->pp_raw.'<br>';
echo 'Level: '.(int)$obj->level.'<br>';
echo 'Play count: '.$obj->playcount.'<br>';
?>
$obj is an array with 1 element.
The short fix is, do $obj = $obj[0] first.
Then you can do $obj["user_id"] etc.

Categories