2016-04-08 46 views
0

我想從API獲取以下JSON響應中的某些信息。這是actual API cal l。使用PHP處理來自TFL api的JSON響應?

{ 
 
    "$type": "Tfl.Api.Presentation.Entities.PlacesResponse, Tfl.Api.Presentation.Entities", 
 
    "centrePoint": [ 
 
    51.555, 
 
    0.059 
 
    ], 
 
    "places": [{ 
 
    "$type": "Tfl.Api.Presentation.Entities.StopPoint, Tfl.Api.Presentation.Entities", 
 
    "naptanId": "490009219W", 
 
    "indicator": "Stop B", 
 
    "stopLetter": "B", 
 
    "modes": [ 
 
     "bus" 
 
    ], 
 
    "icsCode": "1009219", 
 
    "stopType": "NaptanPublicBusCoachTram", 
 
    "stationNaptan": "490G00009219", 
 
    "lines": [{ 
 
     "$type": "Tfl.Api.Presentation.Entities.Identifier, Tfl.Api.Presentation.Entities", 
 
     "id": "25", 
 
     "name": "25", 
 
     "uri": "/Line/25", 
 
     "type": "Line" 
 
    }, { 
 
     "$type": "Tfl.Api.Presentation.Entities.Identifier, Tfl.Api.Presentation.Entities", 
 
     "id": "86", 
 
     "name": "86", 
 
     "uri": "/Line/86", 
 
     "type": "Line" 
 
    }, { 
 
     "$type": "Tfl.Api.Presentation.Entities.Identifier, Tfl.Api.Presentation.Entities", 
 
     "id": "w19", 
 
     "name": "W19", 
 
     "uri": "/Line/w19", 
 
     "type": "Line" 
 
    }], 
 
    "lineGroup": [{ 
 
     "$type": "Tfl.Api.Presentation.Entities.LineGroup, Tfl.Api.Presentation.Entities", 
 
     "naptanIdReference": "490009219W", 
 
     "stationAtcoCode": "490G00009219", 
 
     "lineIdentifier": [ 
 
     "25", 
 
     "86", 
 
     "w19" 
 
     ] 
 
    }], 
 
    "lineModeGroups": [{ 
 
     "$type": "Tfl.Api.Presentation.Entities.LineModeGroup, Tfl.Api.Presentation.Entities", 
 
     "modeName": "bus", 
 
     "lineIdentifier": [ 
 
     "25", 
 
     "86", 
 
     "w19" 
 
     ] 
 
    }], 
 
    "status": true, 
 
    "id": "490009219W", 
 
    "commonName": "Little Ilford Lane", 
 
    "distance": 64.10041498232529, 
 
    "placeType": "StopPoint", 
 
    "additionalProperties": [{ 
 
     "$type": "Tfl.Api.Presentation.Entities.AdditionalProperties, Tfl.Api.Presentation.Entities", 
 
     "category": "Direction", 
 
     "key": "CompassPoint", 
 
     "sourceSystemKey": "Naptan490", 
 
     "value": "W" 
 
    }, { 
 
     "$type": "Tfl.Api.Presentation.Entities.AdditionalProperties, Tfl.Api.Presentation.Entities", 
 
     "category": "Direction", 
 
     "key": "Towards", 
 
     "sourceSystemKey": "CountDown", 
 
     "value": "East Ham or Manor Park" 
 
    }], 
 
    "lat": 51.554475, 
 
    "lon": 0.059381 
 
    }] 
 
}

我想要得到的naptanId,行標識符和對作爲名稱值對,並打印出來。例如,

  • naptan:490009219W;
  • lineidentifer無論值是
  • 朝:「伊斯特姆或莊園公園」

請幫助我,我是一個初學者, 預先感謝您

回答

0

根據您的問題,看來你只對返回的第一個place的值感興趣,所以我會假設在答案中。

要了解從API的數據爲對象結構(而不​​是JSON字符串您最初獲得):

$data = json_decode(file_get_contents('https://api.tfl.gov.uk/Place?lat=51.555504&lon=0.0592359&radius=200&includeChildren=False&type=NaptanPublicBusCoachTram&app_id=&app_key='), true); 

要獲得naptanId

$naptanId = $data['places'][0]['naptanId']; 

爲了得到lineIdentifier ,我將假設它是第一個lineGroup元素(不是lineModeGroups,但可以根據需要更改):

$lineIdentifier = $data['places'][0]['lineGroup'][0]['lineIdentifier']; 

該值將是一個數組。

獲取towards的值有點複雜,因爲您需要根據子鍵過濾additionalProperties數組,然後拔出value;再次我假設你只在第一個值感興趣:

$towards = array_values(array_filter(
    $data['places'][0]['additionalProperties'], 
    function ($property) { 
     return $property['key'] === 'Towards'; 
    } 
))[0]['value']; 

array_filter調用翻出additionalProperties元素,其中key === 'Towards'(注意,這個假設區分大小寫,您可以添加一個strtolower並更改字符串常量到'towards'如果你想不區分大小寫)。對array_values的調用對標準化數組索引是必需的。然後我們拉出第一個元素[0]並獲得其value屬性。

請注意,上面的代碼不會做任何錯誤檢查,例如,如果API調用沒有返回任何places,那麼您第一次做$data['places'][0]它將會失敗。如果您需要處理這些情況(您幾乎可以肯定這樣做),那麼您需要預先檢查這些事情,例如:

if (!isset($data['places']) || sizeof($data['places']) === 0) {...}