Reddit json和PHP - 打开foreach()并获取值

时间:2017-01-19 16:09:27

标签: php json reddit

我正在尝试使用我的帐户作为示例阅读reddit json。

尝试上面的解决方案:

$string_reddit = file_get_contents("https://www.reddit.com/user/joshfolgado/about.json");

$json = json_decode($string_reddit, true);  

$children = $json['data'];
foreach ($children as $child){

$linkkarma = $child['data']['link_karma'];

}

也尝试过:

foreach ($json->data as $mydata){

$values["Latest_Karma"] = $mydata['link_karma'];

}

也尝试过:

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: reddiant api script\r\n"
));

$context = stream_context_create($opts);
$url = "http://www.reddit.com/user/joshfolgado/about.json";
$json = file_get_contents($url, false, $context);

$result = json_decode($json, true);

foreach ($result as $child){
    $values['Latest_Karma'] = $child['data']['link_karma'];
}

花了几个小时尝试获取"数据"中的任何项目的值。数组,还没有得到任何。

我做错了什么?我错过了什么?

感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:1)

Visitor

答案 1 :(得分:0)

对pee2pee的帖子略有修改(他为我返回了一个未定义的索引错误)

$string_reddit = file_get_contents("http://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);

$children = $json['data'];
$user = [];

foreach ($children as $key => $value)
{
    $user[$key] = $value;
}

echo $user['name']; //Now you can use the $user array to access all the properties!

这对我有用 - >

enter image description here