简体   繁体   中英

PHP Output json arrray

I've been trying to discover why am i able to extract parts of the JSON but others fail...

Basically i'm gettting json from an URL ( https://public-crest.eveonline.com/sovereignty/campaigns/ ). On my PHP (using Laravel5) i'm getting this content:

    $url = 'https://public-crest.eveonline.com/sovereignty/campaigns/';
    $get_timers = json_decode(file_get_contents($url));

With that $get_timers run a 'foreach':

foreach($timers->items as $mydata)
     {
      echo $mydata->eventType_str; //output OK
      echo $mydata->constellation->id_str ; //output OK
     }

This get's me the results from the XML properly, BUT, when i try to get from two of those JSON lines (defender) and (attacker) my PHP returns me an error:

Undefined property: stdClass::$attackers (View: /srv/cclp_tb/resources/views/timerboard/index.blade.php)

I'm not sure why am i able to get part of the structure but that particular one is not working... :/

This is the structure of the JSON (one line only to keep it clean):

    {
        "eventType_str": "1",
        "campaignID": 8812,
        "eventType": 1,
        "sourceSolarsystem": {
            "id_str": "30004014",
            "href": "https://public-crest.eveonline.com/solarsystems/30004014/",
            "id": 30004014,
            "name": "4-2UXV"
        },
        "attackers": {
            "score": 0.4
        },
        "campaignID_str": "8812",
        "sourceItemID": 1018997148361,
        "startTime": "2015-11-16T09:50:38",
        "sourceItemID_str": "1018997148361",
        "defender": {
            "defender": {
                "id_str": "99002938",
                "href": "https://public-crest.eveonline.com/alliances/99002938/",
                "id": 99002938,
                "name": "DARKNESS."
            },
            "score": 0.6
        },
        "constellation": {
            "id_str": "20000586",
            "href": "https://public-crest.eveonline.com/constellations/20000586/",
            "id": 20000586,
            "name": "3B-IWE"
        }
    },

Basically i'm able to get ANY 'field' except:

attackers->score;
defender->defender->id_str;
defender->defender->href;
defender->defender->id;
defender->defender->id;
defender->score;

Help? :/

Shouldn't you just be able to get the data once you json_decode it by calling an instance of an array in relation to your response? Like, $get_timers['campaignID] ?

With the given example your code should work fine:

http://sandbox.onlinephpfunctions.com/code/f49a5ff596406161ac6cb75216524da2354a7d38

But I checked the complete source ( https://public-crest.eveonline.com/sovereignty/campaigns/ ) and there are items wich not contain an attacker or defender like this one:

stdClass Object
(
    [eventType_str] => 4
    [campaignID] => 8950
    [eventType] => 4
    [sourceSolarsystem] => stdClass Object
        (
            [id_str] => 30004037
            [href] => https://public-crest.eveonline.com/solarsystems/30004037/
            [id] => 30004037
            [name] => 1-3HWZ
        )

    [campaignID_str] => 8950
    [sourceItemID] => 61000996
    [startTime] => 2015-11-18T01:24:45
    [sourceItemID_str] => 61000996
    [constellation] => stdClass Object
        (
            [id_str] => 20000590
            [href] => https://public-crest.eveonline.com/constellations/20000590/
            [id] => 20000590
            [name] => Yrton
        )

)

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