使用PHP从TFL api处理JSON响应?

I am trying to get certain information from the following JSON response from an API. This is the actual API call.

{
  "$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
  }]
}

I want to get the naptanId, line identifier and towards as name value pairs and print them. For example,

  • naptan :490009219W;
  • lineidentifer Whatever the values are
  • towards : "Eastham or manor park"

Please help me I am a beginner, Thank you in advance

</div>

Based on your question, it seems you are only interested in the values from the first place returned, so I'll assume that in the answer.

To get the data from the API into an object structure (as opposed to the JSON string you initially get):

$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);

To get the naptanId:

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

To get the lineIdentifier, which I'll assume is the one from the first lineGroup element (not lineModeGroups, but you can change as appropriate):

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

This value will be an array.

To get the towards value is a bit more complex, because you need to filter the additionalProperties array based on a child key, then pull out the value; again I'm assuming you're only interested in the first value:

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

The call to array_filter pulls out the additionalProperties element where key === 'Towards' (note this assumes case sensitivity, you could add a strtolower and change the string literal to 'towards' if you want to match case insensitively). The call to array_values is necessary to normalise the array indexes. Then we pull out the first element [0] and get its value property.

Note that the above code does not do any error checking, for example if the API call does not return any places then the first time you do $data['places'][0] it will fail. If you need to handle these cases (which you almost certainly do), then you will need to pre-check these things, e.g.:

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