开发者

json_decode file_get_contents problem

开发者 https://www.devze.com 2023-04-01 10:50 出处:网络
Ok the following php code is working fine <?php $json = file_get_contents(\'http://tiny.cc/example22\');

Ok the following php code is working fine

<?php
$json = file_get_contents('http://tiny.cc/example22');

$obj = json_decode($json);
$example = $obj->{'screen_name'};
?>

User: <?php echo $example; ?>

It shows the screen name 'muffinlosers' just like i wanted

But if i change 'screen_name' to 'total_coins', why it doesn't show the total coins?

I need help with this, i want jus开发者_开发技巧t to show the total coins

Thanks


Use

$obj->experience->total_coins;

Also, this

$example = $obj->{'screen_name'};

Should simply be

$example = $obj->screen_name;


It's so because your request returns json object that has no field called total_coins, but it has field called experience, which type is object also. And that (experience) object has field called total_coins.

So you should:
1. Get object, stored as experience field's value.
2. Get total_coins field's value of received object.

To achieve this, use code, suggested by Phil & user900898 ($example = $obj->experience->total_coins).


This is what you want $example = $obj->experience->total_coins;

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号