Shiv Singh
Shiv Singh

Reputation: 7221

Error on getting data from JSON API

I am new to PHP.

Currently I'd like to get data from a JSON API URL: https://kickass.so/json.php?q=test+category:tv&field=seeders&order=desc&page=2

I have tried the following:

<?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'];
  }
?>

Here I'd want to get the value as:

Title, category, link, hash ect.

But I'm getting this error:

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

Upvotes: 0

Views: 612

Answers (2)

manuelbcd
manuelbcd

Reputation: 4607

Firstly you need to know the structure of JSON object. If you make a print_r of $parsed_json this will be the result like:

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
                )
                ...

As you can see, this object contains only one register, this register contains another register called 'list' which contains the array you are looking for.

This code will loop without problems:

$parsed_json = json_decode($query, true);


if( !isset( $parsed_json['list'])) die("Invalid format");

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

Upvotes: 1

Kevin
Kevin

Reputation: 41893

It's still inside an array 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
                    ....
                )

You can point it directly inside the loop:

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

Upvotes: 3

Related Questions