简体   繁体   中英

Access Array In Json response In PHP from REST API response from Invision Community

I'm trying to access the "results" array from inside of a Json response from an Invision Community REST API call. I'm not sure how to filter to that results array from in the PHP code as shown below. Once I filter to it I want to echo it onto the screen like the way the full response is currently shown. Any help would be greatly appreciated!

PHP Code

<?php
    $communityUrl = 'https://website.net/invisioncommunity';
    $apiKey = 'apikey';
    $endpoint = '/core/members';

    $curl = curl_init( $communityUrl . 'api' . $endpoint );
    curl_setopt_array( $curl, array(
        CURLOPT_RETURNTRANSFER  => TRUE,
        CURLOPT_HTTPAUTH    => CURLAUTH_BASIC,
        CURLOPT_USERPWD     => "{$apiKey}:"
    ) );
    $response = curl_exec( $curl );
    $resArr = array();
    $resArr = json_decode($response);
    
    $resultsArr = cleanResponse($response);
    
    echo "<pre>"; print_r($resultsArr); echo "</pre>";    
    function cleanResponse($arr)
    {
        $element = $arr['results'];
        return $element;
    }

Json (partial) Response from API Call

{
    "page": 1,
    "perPage": 25,
    "totalResults": 111,
    "totalPages": 5,
    "results": [
        {
            "id": 1,
            "name": "Demo",
            "title": null,
            "timeZone": "America\/New_York",
            "formattedName": "Demo<\/span>",
            "primaryGroup": {
                "id": 4,
                "name": "Demo",
                "formattedName": "Demo<\/span>"
            },
            "secondaryGroups": [
                {
                    "id": 15,
                    "name": "Root Admin (Do not edit!)",
                    "formattedName": "Root Admin (Do not edit!)"
                }
            ],
            "email": "demo@website.net",
            "joined": "2020-07-07T19:48:33Z",
            "registrationIpAddress": "",
            "warningPoints": 0,
            "reputationPoints": 6,
            "photoUrl": "",
            "photoUrlIsDefault": false,
            "coverPhotoUrl": "",
            "profileUrl": "https:\/\/www.website.net\/profile\/1-demo\/",
            "validating": false,
            "posts": 3,
            "lastActivity": "2020-07-14T20:01:21Z",
            "lastVisit": "2020-07-14T18:43:48Z",
            "lastPost": "2020-07-13T13:26:02Z",
            "profileViews": 1234,
            "birthday": "1\/2",
            "customFields": {
                "1": {
                    "name": "Personal Information",
                    "fields": {
                        "1": {
                            "name": "About Me",
                            "value": ""
                        },
                        "2": {
                            "name": "Discord",
                            "value": "Demo#0000"
                        },
                        "3": {
                            "name": "Steam Hex",
                            "value": null
                        }
                    }
                }
            }
        },
    ]
}

A few small changes to your code should solve your issue:

  1. To get an associative array returned, use true as second argument to json_decode()

$resArr = json_decode($response, true);

  1. Use associative array $resArr as the argument for function cleanResponse :

$resultsArr = cleanResponse($resArr);

Now $resultsArr holds the results sub-array, which you can output using eg print_r()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM