从 JSON API 获取数据时出错


Error on getting data from JSON API

我是PHP的新手。

目前我想从 JSON API URL 获取数据:

https://kickass.so/json.php?q=test+category:tv&field=seeders&order=desc&page=2

我尝试了以下方法:

<?php
  $query = file_get_contents('https://kickass.so/json.php?q=test+category:tv&field=seeders&order=desc&page=2');
  $parsed_json = json_decode($query, true);
  foreach ($parsed_json as $key => $value)
  {
    echo $value['title'];
  }
?>

在这里,我想将值获取为:

标题,类别,链接,哈希等。

但是我收到此错误:

Warning: Illegal string offset 'title' in C:'xampp'htdocs'xxxxx'test.php on line 6
K
Warning: Illegal string offset 'title' in C:'xampp'htdocs'xxxxx'test.php on line 6
h
Warning: Illegal string offset 'title' in C:'xampp'htdocs'xxxxx'test.php on line 6
B
Warning: Illegal string offset 'title' in C:'xampp'htdocs'xxxxx'test.php on line 6
e
Warning: Illegal string offset 'title' in C:'xampp'htdocs'xxxxx'test.php on line 6
1
Notice: Undefined index: title in C:'xampp'htdocs'xxxxx'test.php on line 6

它仍然在数组list内:

[title] => Kickasstorrents test category:tv
    [link] => http://kickass.so
    [description] => BitTorrent Search: test category:tv
    [language] => en-us
    [ttl] => 60
    [total_results] => 1002
    [list] => Array // <------- Another nesting
        (
            [0] => Array
                (
                    [title] => The Simpsons S24E10 A Test Before Trying 480p WEB-DL x264-mSD
                    [category] => TV
                    [link] => http://kickass.so/the-simpsons-s24e10-a-test-before-trying-480p-web-dl-x264-msd-t7006138.html
                    [guid] => http://kickass.so/the-simpsons-s24e10-a-test-before-trying-480p-web-dl-x264-msd-t7006138.html
                    [pubDate] => Sunday 20 Jan 2013 10:57:02 +0000
                    [torrentLink] => http://torcache.net/torrent/0D616E1F4578942883F9A0BF676DCDDA02B1A894.torrent?title=[kickass.so]the.simpsons.s24e10.a.test.before.trying.480p.web.dl.x264.msd
                    [files] => 4
                    [comments] => 0
                    ....
                )

您可以将其直接指向循环内:

foreach ($parsed_json['list'] as $key => $value)
{
    echo $value['title'];
}

首先,您需要了解JSON对象的结构。 如果对 $parsed_json 进行print_r,结果如下:

Array
(
    [title] => Kic ....
    [link] => http://kick...
    [description] => BitT...
    [language] => en-us
    [ttl] => 60
    [total_results] => 1002
    [list] => Array
        (
            [0] => Array
                (
                    [title] => The ...
                    [category] => TV
                    [link] => http://kicka...
                    [guid] => http://kicka...
                    [pubDate] => Sunday 20 Jan 2013 10:57:02 +0000
                    [torrentLink] => http://torcac...
                    [files] => 4
                    [comments] => 0
                    [hash] => 0D616...
                    [peers] => 18
                    [seeds] => 17
                    [leechs] => 1
                    [size] => 13652...
                    [votes] => 3
                    [verified] => 1
                )
            [1] => Array
                (
                    [title] => Lio..
                    [category] => TV
                    [link] => http://kicka...
                    [guid] => http://kick...
                    [pubDate] => Sunday 7 Jul 2013 01:45:16 +0000
                    [torrentLink] => http://tor...
                    [files] => 2
                    [comments] => 7
                    [hash] => BBF5D...
                    [peers] => 17
                    [seeds] => 17
                    [leechs] => 0
                    [size] => 1614025889
                    [votes] => 8
                    [verified] => 0
                )
                ...
如您所见,此对象仅包含一个寄存器,此寄存器

包含另一个称为"list"的寄存器,其中包含您要查找的数组。

此代码将毫无问题地循环:

$parsed_json = json_decode($query, true);

if( !isset( $parsed_json['list'])) die("Invalid format");
foreach ($parsed_json['list'] as $key => $value)
{
    echo $value['title'];
}