Trying to get property of non object error

Hi there!

I am trying to utilize an API to get some user data. It’s for a gaming community and the game developer has provided a public API. I have done exactly what all of the guides say to do and used their PHP examples but for some reason it just keeps telling me I’m trying to get the property of a non object. I have sought solutions from the developers/other community members but it takes ages to get a response so I thought I’d try here to see if someone could help figure it out for me.

Here is the code in question:

$apiKey = 'REMOVED FOR SECURITY';
$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, 'https://www.bungie.net/platform/Destiny/2/Stats/GetMembershipIdByDisplayName/crush_uk');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Key: ' . $apiKey));

$json = json_decode(curl_exec($ch)); 
    echo $json->Response;

I thought perhaps the restult was an array so I tried using:

foreach($json as $item); { 
echo $item->Response; } 

But that gave me a “using invalid argument for foreach” error message.

I tried using $print_r($json); after the $json is defined because it seems that is the object it thinks isn’t one and all it does is return a blank page, which led me to believe maybe the URL I was querying wasn’t right - so I checked all through the documentation again and it is definitely right…

Am I missing something else? The queried URL is definitely correct. Fro clarity - This is the guide I am using (and the same place I got the code from): http://web.engr.oregonstate.edu/~walkermi/HowTo3.html

Hi chru5h, welcome to the forum

Going to that URL returns

{"Response":"0",
"ErrorCode":2102,
"ThrottleSeconds":0,
"ErrorStatus":"ApiKeyMissingFromRequest",
"Message":"Please add valid X-API-Key header to request.  Visit https://www.bungie.net/developer for details.",
"MessageData":{}}

It is a JSON object not an array

Anyway, are you certain you have cURL installed and configured correctly?

Hi there,

I have managed to get past this first part. I know you will see the API Key Missing message because you don’t have my API Key and it’s probably advised I leave that out of the public domain - so it will be difficulty for you to replicate the error your end without your own API Key.

Anyway - I got past this by replacing https with http… so my code now looks like:

$apiKey = 'REMOVED FOR SECUIRTY';

// First we will grab the user's membership ID

$platform = '2';
$name = 'crush_uk';

$url1 = 'http://www.bungie.net/platform/destiny/SearchDestinyPlayer/'.$platform.'/'.$name.'/';

$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Key: ' . $apiKey));

$json = json_decode(curl_exec($ch), true); 
  
  $data = $json['Response'];
  $array = $data[0];
  $memberID = $array['membershipId'];
  
// Then we will redirect to the character info script, passing that member ID along in the URL. 

 header("Location:http://christopherbush.co.uk/destiny/characterinfo.php?ID=".urlencode($memberID)."&platform=".urlencode($platform)."&name=".urlencode($name)."");

This works perfectly - it gets my membership ID from the API and redirects me to the next stage of the process… Now I am faced with another problem - The second page is displaying the JSON query raw data even though I’ve not put any print_r or var_dump requests in the code… Here is the code for characterinfo.php

// Now we will get the parameters passed over in the URL... 

$memberID = $_GET['ID'];
$platform = $_GET['platform'];
$name = $_GET['name'];
    
// Then we build the URL to get the character list using the above variables...

$url = 'http://www.bungie.net/platform/destiny/'.$platform.'/Account/'.$memberID.'/Summary/';
 
 // Then we run our query
 
$apiKey = 'REMOVED FOR SECURITY';
$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Key: ' . $apiKey));
 
$json = json_decode(curl_exec($ch), true);

Any idea why it is automatically displaying the raw data from the new JSON query?

And, whilst you’re here - the next step in the process is for me to display the Character IDs for each of the characters associated with this membership - If you look at the data, you’ll see them there under Response->data->characters->characterBase->characterId. In this example, there are two in the array. How do I display both of theswe IDs?

Live Example: http://christopherbush.co.uk/destiny/playerinfo.php

Update!

I have stopped it from always showing the JSON raw data by changing:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);

to:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

and I have changed:

$json = json_decode(curl_exec($ch), true);

to:

$json = json_decode(curl_exec($ch));

It is quite long but for clarity I have posted the beatufied JSON data below - In this example, there are 2 character IDs (There can be anything up to 3) - I am trying to figure out what code is needed to display a list of both of these character IDs. I’ve removed as much of the unnecessary code that makes up the majority of the JSOn request and highligthed the two objects/parameters I am trying to fetch.

{
    "Response": {
        "data": {
            "membershipId": "4611686018448666364",
            "membershipType": 2,
            "characters": [
                {
                    "characterBase": {
                        "membershipId": "4611686018448666364",
                        "membershipType": 2,
                        **"characterId": "2305843009292449175",**
                        "dateLastPlayed": "2015-10-30T06:28:09Z",
                        "minutesPlayedThisSession": "233",
                        "minutesPlayedTotal": "25725",
                        "powerLevel": 304,
                        "raceHash": 2803282938,
                        "genderHash": 3111576190,
                        "classHash": 2271682572,
                        "currentActivityHash": 0,
                        "lastCompletedStoryHash": 0,
                        "customization": {
                            "personality": 2166136261,
                            "face": 820889531,
                            "skinColor": 2542514572,
                            "lipColor": 1328348389,
                            "eyeColor": 1511637744,
                            "hairColor": 204829631,
                            "featureColor": 2166136261,
                            "decalColor": 2304614797,
                            "wearHelmet": false,
                            "hairIndex": 8,
                            "featureIndex": 0,
                            "decalIndex": 3
                        },
                        "grimoireScore": 4030,
                        "peerView": {
                            "equipment": [
                                {
                                    "itemHash": 3828867689,
                                    "dyes": []
                                }
                        "genderType": 0,
                        "classType": 2,
                        "buildStatGroupHash": 2172613790
                    },
                    "levelProgression": {
                        "dailyProgress": 0,
                        "weeklyProgress": 0,
                        "currentProgress": 346000,
                        "level": 40,
                        "step": 0,
                        "progressToNextLevel": 0,
                        "nextLevelAt": 0,
                        "progressionHash": 1716568313
                    },
                    "emblemPath": "/common/destiny_content/icons/4de8116ee7acef7c2419924b12f9636f.jpg",
                    "backgroundPath": "/common/destiny_content/icons/06587476c45a2de2b7b09d5e6690575e.jpg",
                    "emblemHash": 2372257458,
                    "characterLevel": 40,
                    "baseCharacterLevel": 40,
                    "isPrestigeLevel": false,
                    "percentToNextLevel": 0
                }
                {
                    "characterBase": {
                        "membershipId": "4611686018448666364",
                        "membershipType": 2,
                        **"characterId": "2305843009316656187",**
                        "dateLastPlayed": "2015-10-04T11:37:34Z",
                        "minutesPlayedThisSession": "2",
                        "minutesPlayedTotal": "2005",
                        "powerLevel": 135,
                        "raceHash": 3887404748,
                        "genderHash": 3111576190,
                        "classHash": 3655393761,
                        "currentActivityHash": 0,
                        "lastCompletedStoryHash": 0,
                        "customization": {
                            "personality": 2166136261,
                            "face": 2680554480,
                            "skinColor": 683440567,
                            "lipColor": 3571537821,
                            "eyeColor": 1194006499,
                            "hairColor": 280639505,
                            "featureColor": 2166136261,
                            "decalColor": 1213241010,
                            "wearHelmet": false,
                            "hairIndex": 9,
                            "featureIndex": 0,
                            "decalIndex": 0
                        },
                        "grimoireScore": 4030,
                        "peerView": {
                            "equipment": [
                                {
                                    "itemHash": 2455559914,
                                    "dyes": []
                                },
                        },
                        "genderType": 0,
                        "classType": 0,
                        "buildStatGroupHash": 1235322459
                    },
                    "levelProgression": {
                        "dailyProgress": 0,
                        "weeklyProgress": 0,
                        "currentProgress": 274000,
                        "level": 34,
                        "step": 0,
                        "progressToNextLevel": 0,
                        "nextLevelAt": 9000,
                        "progressionHash": 1716568313
                    },
                    "emblemPath": "/common/destiny_content/icons/54f048e157d97193ccf618b8b5fa8102.jpg",
                    "backgroundPath": "/common/destiny_content/icons/f63cfeb14c128400a793b63c857156bc.jpg",
                    "emblemHash": 1600609907,
                    "characterLevel": 34,
                    "baseCharacterLevel": 34,
                    "isPrestigeLevel": false,
                    "percentToNextLevel": 0
                }
            ],
            "clanName": "Little Big Light",
            "clanTag": "LBL",
            "inventory": {
                "items": []
            },
            "grimoireScore": 4030,
            "versions": 15
        }
    },
    "ErrorCode": 1,
    "ThrottleSeconds": 0,
    "ErrorStatus": "Success",
    "Message": "Ok",
    "MessageData": {}
}

Hopefully.

Try
Response->data->characters[0]->characterBase->characterId

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.