PHP输出json arrray

时间:2015-11-17 11:43:59

标签: php json laravel-5

我一直试图发现为什么我能够提取部分JSON但其他人失败......

基本上我是从URL(https://public-crest.eveonline.com/sovereignty/campaigns/)获取json。在我的PHP(使用Laravel5)上我得到了这个内容:

class Content {
    const mTable = "makes";

    public function getMakes($paginate = true) {
        if ($paginate) {
        $pager = Paginator::instance();
        $pager->items_total = countEntries(self::mTable);
        $pager->default_ipp = Registry::get("Core")->ipp;
        $pager->paginate();
        $limit = $pager->limit;
    } else {
        $limit = null;
    }

    $sql = "SELECT * FROM " . self::mTable . " ORDER BY name" . $limit;
    $row = self::$db->fetch_all($sql);

    return ($row) ? $row : 0;
    }  
}

使用$ get_timers运行'foreach':

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

这让我得到了正确的XML结果,但是,当我尝试从两个JSON行(防御者)和(攻击者)中获取时,我的PHP返回错误:

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

我不知道为什么我能够获得结构的一部分,但是那个特定的结构不起作用......:/

这是JSON的结构(只有一行才能保持清洁):

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

基本上我可以获得任何'字段',除了:

    {
        "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"
        }
    },

帮助? :/

2 个答案:

答案 0 :(得分:0)

一旦你通过调用与你的响应相关的数组实例来json_decode它,你是否能够获得数据?比如,$ get_timers [' campaignID]?

答案 1 :(得分:0)

使用给定的示例,您的代码应该可以正常工作:

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

但我检查了完整的来源(https://public-crest.eveonline.com/sovereignty/campaigns/),并且有些项目不包含像这样的攻击者或防御者:

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
        )

)
相关问题