通过php代码获取JSON数据

时间:2016-10-22 07:08:31

标签: javascript php html json api

方法名称: GetAvaiillabiilliity

 $mySOAP = <<<EOD
{

        "Authentication": {
            "LoginId": "username",
            "Password": "password"

        },

    "AvailabilityInput":{
        "BookingType":"O",
        "JourneyDetails":[{
            "Origin":"BOM",
            "Destination":"PNQ",
            "TravelDate":"10/30/2016"
        }],
        "ClassType":"Economy",
        "AirlineCode":"",
        "AdultCount":1,
        "ChildCount":0,
        "InfantCount":0,
        "ResidentofIndia":1,
        "Optional1":"0",
        "Optional2":"0",
        "Optional3":"0"
    }

}
EOD;
// The HTTP headers for the request (based on image above)
$headers = array(
    'Content-Type: application/json',
);
// Build the cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $mySOAP);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Send the request and check the response
if (($result = curl_exec($ch)) === FALSE) {
    die('cURL error: ' . curl_error($ch) . "<br />\n");
} else {
    echo $result;
}
curl_close($ch);
$data = json_decode($result,true);
$devices = $data['AvailabilityOutput']['AvailableFlights'];

?>

输出: GetAvailabilityResponse 这是我们从服务器端获得的响应,我想通过我的php和html代码显示它。

{
"ResponseStatus":1,"UserTrackId":"RMYLN97099869978897983977484056886242191","AvailabilityOutput":{"AvailableFlights":{"OngoingFlights":[{"AvailSegments":[{"FlightId":"41","AirlineCode":"9W","FlightNumber":"618","AirCraftType":"738","Origin":"BOM","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 10:00:00","ArrivalDateTime":"30\/10\/2016 10:50:00","Duration":"00Hrs 50Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":6940,"YQ":200,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":8254,"Commission":"208.2"},"Child":null,"Infant":null}],"SupplierId":"1"}]},{"AvailSegments":[{"FlightId":"42","AirlineCode":"9W","FlightNumber":"453","AirCraftType":"738","Origin":"BOM","OriginAirportTerminal":"","Destination":"HYD","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 03:00:00","ArrivalDateTime":"30\/10\/2016 04:15:00","Duration":"01Hrs 15Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"L","ClassCodeDesc":"L","BaggageAllowed":null,"Adult":{"FareBasis":"L2IPO","FareType":"\u000aRefundable","BasicAmount":0,"YQ":0,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":0,"Commission":"0"},"Child":null,"Infant":null}],"SupplierId":"1"},{"FlightId":"43","AirlineCode":"9W","FlightNumber":"2822","AirCraftType":"73W","Origin":"HYD","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 05:50:00","ArrivalDateTime":"30\/10\/2016 07:05:00","Duration":"01Hrs 15Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"L","ClassCodeDesc":"L","BaggageAllowed":null,"Adult":{"FareBasis":"L2IPO","FareType":"\u000aRefundable","BasicAmount":16540,"YQ":400,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":18965,"Commission":"496.2"},"Child":null,"Infant":null}],"SupplierId":"1"}]},{"AvailSegments":[{"FlightId":"44","AirlineCode":"9W","FlightNumber":"465","AirCraftType":"738","Origin":"BOM","OriginAirportTerminal":"","Destination":"MAA","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 07:05:00","ArrivalDateTime":"30\/10\/2016 09:05:00","Duration":"02Hrs 0Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":0,"YQ":0,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":0,"Commission":"0"},"Child":null,"Infant":null}],"SupplierId":"1"},{"FlightId":"45","AirlineCode":"9W","FlightNumber":"2491","AirCraftType":"738","Origin":"MAA","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 09:45:00","ArrivalDateTime":"30\/10\/2016 11:25:00","Duration":"01Hrs 40Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":14050,"YQ":600,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":16244,"Commission":"421.5"},"Child":null,"Infant":null}],"SupplierId":"1"}]},{"AvailSegments":[{"FlightId":"46","AirlineCode":"9W","FlightNumber":"339","AirCraftType":"73W","Origin":"BOM","OriginAirportTerminal":"","Destination":"DEL","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 03:00:00","ArrivalDateTime":"30\/10\/2016 05:05:00","Duration":"02Hrs 5Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":0,"YQ":0,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":0,"Commission":"0"},"Child":null,"Infant":null}],"SupplierId":"1"},{"FlightId":"47","AirlineCode":"9W","FlightNumber":"365","AirCraftType":"73H","Origin":"DEL","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 08:30:00","ArrivalDateTime":"30\/10\/2016 10:40:00","Duration":"02Hrs 10Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":13600,"YQ":800,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":16031,"Commission":"408"},"Child":null,"Infant":null}],"SupplierId":"1"}]},{"AvailSegments":[{"FlightId":"48","AirlineCode":"9W","FlightNumber":"463","AirCraftType":"73H","Origin":"BOM","OriginAirportTerminal":"","Destination":"MAA","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 02:55:00","ArrivalDateTime":"30\/10\/2016 04:50:00","Duration":"01Hrs 55Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPOA","FareType":"\u000aRefundable","BasicAmount":0,"YQ":0,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":0,"Commission":"0"},"Child":null,"Infant":null}],"SupplierId":"1"},{"FlightId":"49","AirlineCode":"9W","FlightNumber":"2491","AirCraftType":"738","Origin":"MAA","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 09:45:00","ArrivalDateTime":"30\/10\/2016 11:25:00","Duration":"01Hrs 40Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":13800,"YQ":600,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":15979,"Commission":"414"},"Child":null,"Infant":null}],"SupplierId":"1"}]}],"ReturnFlights":null}}}

我的PHP代码

<?php
echo 'Flifht checking';
echo '<br><hr>';
    foreach ($devices as $device){
        echo '<h4>';
        echo $device['FlightNumber'];
        echo '</h4>';
    }
?>

如何以json代码获取所有航班信息? 那么如何获取这个json数据AvailabilityOutput":{"AvailableFlights":{"OngoingFlights":[{"AvailSegments"。我无法理解深度树结构。

1 个答案:

答案 0 :(得分:1)

在这里,您可以通过所有航班进行实施。

我将$devices名称更改为$availableFlights,以使示例更易于理解。

$data = json_decode($result, true);
$availableFlights = $data['AvailabilityOutput']['AvailableFlights'];

echo 'Flifht checking';
echo '<br><hr>';

foreach ($availableFlights['OngoingFlights'] as $availableFlight) {
  foreach ($availableFlight['AvailSegments'] as $availSegment) {

    echo '<h4>';
    echo 'Flight Number: ' . $availSegment['FlightNumber'];
    echo '</h4>';
    echo '<p>Origin: ' . $availSegment['Origin'] . ' - Destination: ' . $availSegment['Destination'] . '</p>';

  }
}

这里的输出是:

Flifht checking

Flight Number: 618

Origin: BOM - Destination: PNQ

Flight Number: 453

Origin: BOM - Destination: HYD

Flight Number: 2822

Origin: HYD - Destination: PNQ

Flight Number: 465

Origin: BOM - Destination: MAA

Flight Number: 2491

Origin: MAA - Destination: PNQ

Flight Number: 339

Origin: BOM - Destination: DEL

Flight Number: 365

Origin: DEL - Destination: PNQ

Flight Number: 463

Origin: BOM - Destination: MAA

Flight Number: 2491

Origin: MAA - Destination: PNQ

另外,一个小技巧:如果你想轻松查看整个json树,你可以使用它:

echo "<pre>";
print_r($availableFlights);
echo "</pre>";

(我认为理解数据结构可能会有所帮助)