在JSON响应中的参数之前和之后获取reddit API

时间:2015-08-14 16:21:27

标签: php json api reddit

我正在用PHP进行调用,语法如下:

<?php
$subreddit = 'cats';
$limit = 15;
$source = "https://www.reddit.com/r/$subreddit.json?limit=$limit";
$json = file_get_contents($source);
$js = json_decode($json);


foreach($js->data as $n) {          
    var_dump($js);      
} // endforeach
?>

按预期工作,并返回如下内容:

object(stdClass)[1]
  public 'kind' => string 'Listing' (length=7)
  public 'data' => 
    object(stdClass)[2]
      public 'modhash' => string '' (length=0)
      public 'children' => 
        array (size=15)
          0 => 
            object(stdClass)[3]
              ...
          1 => 
            object(stdClass)[23]
              ...
          2 => 
            object(stdClass)[39]
              ...
          3 ...
              ...
      public 'after' => string 't3_3guzfa' (length=9)
      public 'before' => null

然后我尝试在循环内定义after变量,循环变为

foreach($js->data as $n) {

    var_dump($js); 

    $after = $n->data->after;
    echo ("This:" . $after);

} // endforeach

正如您所怀疑的那样,结果是“注意:尝试在第13行的C:\ var_launchers \ A1 \ build \ index-return.php中获取非对象的属性”

我如何达到“之后”的价值?任何帮助将非常感激。谢谢。

0 个答案:

没有答案