The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> PHP Get json data from url not displaying all entrys
sanoj96
post Mar 9 2021, 11:11 AM
Post #1


Advanced Member
****

Group: Members
Posts: 118
Joined: 18-September 12
Member No.: 17,803



Hello, so i am trying to get some data from a json url.

The script gives me one out of 600 entrys with the correct information and all that. But when i try to get another one it returns "array". for the next 599 entrys.

Tried doing a for loop, and a foreach just crashed the site "website should not load ~".

So after a few hours on google to find a solution for it, i am asking here if anyone here has a solution or can point me in the direction.

This is the working code:
CODE

<?php
    $data = json_decode(file_get_contents('http://~~~/posts'), true);
    $main = $data['posts'][0];
    if (is_null($data)) {
    echo json_last_error_msg();
    die;
    }
    
    foreach($main as $key => $value) {
      if (is_array($value)){
         // here
         $value = implode(', ', $main['roles']);
      }
      echo "$key: $value <br>";
    }  

?>


and this is part of the json file

CODE

{
    "posts": [
        {
            "roles": [
                "307576552110161920",
                "574256699985100800",
                "605330280777252864",
                "771034177994883152",
                "771035043895836704",
                "771035810421669888",
                "771036634433978398",
                "771037238346907658",
                "777631277342916668",
                "801832703879413831",
                "814246213108629564"
            ],
            "_id": "60476b3743c2d92b28668001",
            "username": "username1",
            "nickname": "nickname1",
            "avatar": "UrlToProfilePicture",
            "__v": 0
        },
        {
            "roles": [],
            "_id": "60476b3743c2d92b2866800b",
            "username": "username",
            "avatar": "UrlToProfilePicture",
            "__v": 0
        },
        {
            "roles": [
                "498147873003798539"
            ],
            "_id": "60476b3743c2d92b2866800c",
            "username": "username",
            "nickname": "nickname",
            "avatar": "UrlToProfilePicture",
            "__v": 0
        },
        {
            "roles": [
                "574256699985100800",
                "658959318070329346",
                "771034421646065684",
                "771035043895836704",
                "771035587506995210",
                "771036634433978398",
                "777631277342916668",
                "777631781922013194",
                "777632056997183529"
            ],
            "_id": "60476b3743c2d92b2866800d",
            "username": "username",
            "avatar": "UrlToProfilePicture",
            "__v": 0
        },
    ]
}



This returns

CODE
roles: 307576552110161920, 574256699985100800, 605330280777252864, 771034177994883152, 771035043895836704, 771035810421669888, 771036634433978398, 771037238346907658, 777631277342916668, 801832703879413831, 814246213108629564
_id: 60476b3743c2d92b28668001
username: username1
nickname: nickanme1
avatar: UrlToProfilePicture
__v: 0

as it should. But it only gives one of the entrys and not all of them. Any pointers will help!

Thanks
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
sanoj96
post Mar 9 2021, 11:33 AM
Post #2


Advanced Member
****

Group: Members
Posts: 118
Joined: 18-September 12
Member No.: 17,803



Seems like i managed to fix it!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 29th March 2024 - 06:57 AM