按城市和酒店搜索以查找数据库中存在的城市和酒店。我的代码无法正常工作。

我的城市名称:
“软木”

我的酒店清单:


“奥利尔之家酒店”
“茱莉斯科克旅馆”


API网址:https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=DBscoSJ9anWDChd05JIRGIscEBtpCmGl&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000

elseif(!empty($_GET['countrycode'])){
        $countyname =   $_GET['countrycode'];
        $url='https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=xxxxxxxxxxxxxxxxxxxxx&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
        $response = file_get_contents($url);
        $array = json_decode($response, true);


        foreach($array as $api_array){
            $final_array = array();
            $Results = $wpdb->get_results( "select * FROM hotels where county = '$countyname'",ARRAY_A );
            $RowCount  =  $wpdb->num_rows;
            if($RowCount>0){
                foreach($Results as $Result){
                    foreach($api_array as &$value){
                        if($Result['county'] == $value['address']['city']){
                            $final_array[] = $value;

                        }
                    }
                }
            }

            foreach($Results as $Result){
                foreach($final_array as &$display){
                    if($Result['hotel'] == $display['property_name']){
                        $final_array2[] = $display;
                    }
                }
            }
        }
    }




-- Table structure for table `hotels`
--

CREATE TABLE IF NOT EXISTS `hotels` (
  `id`              int(11) NOT NULL AUTO_INCREMENT,
  `county`          varchar(50)     COLLATE         utf8_unicode_ci NOT NULL,
  `hotel`           varchar(500)    COLLATE         utf8_unicode_ci NOT NULL,
  `image_1`         longtext        COLLATE         utf8_unicode_ci NOT NULL,
  `image_2`         longtext        COLLATE         utf8_unicode_ci NOT NULL,
  `image_3`         longtext        COLLATE         utf8_unicode_ci NOT NULL,
  `kids`            text            COLLATE         utf8_unicode_ci NOT NULL,
  `adult`           text            COLLATE         utf8_unicode_ci NOT NULL,
  `description`     text            COLLATE         utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=43 ;

最佳答案

您的API响应如下所示:

{
    "results" : [
        {
            "property_code" : "MSDUBALF",
            "property_name" : "The Central Hotel Tullamore",
            "location" : {
                "latitude" : 53.3433,
                "longitude" : -7.61408
            },
            "address" : {
                "line1" : "Main Street",
                "city" : "Tullamore",
                "postal_code" : "R35 Y7P1",
                "country" : "IE"
            },
            "total_price" : {
                "amount" : "729.47",
                "currency" : "USD"
            },
            "min_daily_rate" : {
                "amount" : "16.55",
                "currency" : "USD"
            },
            "contacts" : [
                {
                    "type" : "PHONE",
                    "detail" : "353-57-9360034"
                },
                {
                    "type" : "FAX",
                    "detail" : "353-57-9320354"
                }
            ],
            "amenities" : [
                {
                    "amenity" : "RESTAURANT",
                    "ota_code" : 76,
                    "description" : "Restaurant"
                },
                {
                    "amenity" : "FREE_HIGH_SPEED_INTERNET",
                    "ota_code" : 222,
                    "description" : "Free high speed internet connection"
                },
                {
                    "amenity" : "INTERNET_PUBLIC_AREAS",
                    "ota_code" : 178,
                    "description" : "Internet access in public areas"
                },
                {
                    "amenity" : "LAUNDRY_SERVICE",
                    "ota_code" : 58,
                    "description" : "Laundry/Valet service"
                },
                {
                    "amenity" : "PARKING",
                    "ota_code" : 68,
                    "description" : "Parking"
                },
                {
                    "amenity" : "LOUNGE_BARS",
                    "ota_code" : 165,
                    "description" : "Lounges/bars"
                }
            ],
            "awards" : [
                {
                    "provider" : "Local Star Rating",
                    "rating" : "3"
                }
            ],
            "images" : [ ],
            "rooms" : [
                {
                    "booking_code" : "N1DADV",
                    "room_type_code" : "N1D",
                    "rate_plan_code" : "ADV",
                    "total_amount" : {
                        "amount" : "729.47",
                        "currency" : "USD"
                    },
                    "rates" : [
                        {
                            "start_date" : "2017-06-19",
                            "end_date" : "2017-06-23",
                            "currency_code" : "USD",
                            "price" : 66.21
                        },
                        {
                            "start_date" : "2017-06-25",
                            "end_date" : "2017-06-26",
                            "currency_code" : "USD",
                            "price" : 60.6
                        },
                        {
                            "start_date" : "2017-06-27",
                            "end_date" : "2017-06-28",
                            "currency_code" : "USD",
                            "price" : 60.6
                        },
                        {
                            "start_date" : "2017-06-26",
                            "end_date" : "2017-06-27",
                            "currency_code" : "USD",
                            "price" : 66.21
                        },
                        {
                            "start_date" : "2017-06-28",
                            "end_date" : "2017-06-29",
                            "currency_code" : "USD",
                            "price" : 66.21
                        },
                        {
                            "start_date" : "2017-06-23",
                            "end_date" : "2017-06-24",
                            "currency_code" : "USD",
                            "price" : 94.27
                        },
                        {
                            "start_date" : "2017-06-24",
                            "end_date" : "2017-06-25",
                            "currency_code" : "USD",
                            "price" : 116.72
                        }
                    ],
                    "descriptions" : [
                        "Prepay and Save - Nonrefundable Rate.",
                        "1 double bed, free wifi, en-suite bath, coffee",
                        "and tea maker, cable tv."
                    ],
                    "room_type_info" : {
                        "room_type" : "Non-smoking",
                        "bed_type" : "Double",
                        "number_of_beds" : "1"
                    },
                    "rate_type_code" : "ADV"
                }
            ],
            "_links" : {
                "more_rooms_at_this_hotel" : {
                    "href" : "https://api.sandbox.amadeus.com/v1.2/hotels/MSDUBALF?apikey=DBscoSJ9anWDChd05JIRGIscEBtpCmGl&check_in=2017-06-19&check_out=2017-06-29&referrer=more_rooms_at_this_hotel"
                }
            }
        },
        {....}
    ]
}


代码:

<?php
$countryName =  $_GET['countrycode'];
$url        =  'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=xxxxxxxxxxxxxx&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
$response  = file_get_contents($url);
$apiResults = json_decode($response, true);
$apiResults = $apiResults["results"];

$finalArray = [];
$dbResults   = $wpdb->get_results(
    $wpdb->prepare("
        SELECT *
          FROM hotels
         WHERE county = %s",
        $countryName
    ),
    ARRAY_A
);


# If atleast 1 row is fecthed from
# database
if (count($dbResults)) {
    foreach ($dbResults as $dbResult) {
        $storedCountryName = $dbResult["county"];
        $storedHotelName   = $dbResult["hotel"];
        foreach ($apiResults as $apiResult) {
            if ($storedCountryName  === $apiResult['address']['city']
                && $storedHotelName === $apiResult['property_name'])
            {
                $finalArray[] = $apiResult;
            }
        }
    }
}

?>


在这里,finalArray将包含数据库中的旅馆。

由于我没有适当的系统,所以无法测试上述代码。您可以在系统中对其进行测试。让我知道,如果发生任何问题。

测试代码:

<?php
$countryName =  'Cork';
$url        =  'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=DBscoSJ9anWDChd05JIRGIscEBtpCmGl&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
$response  = file_get_contents($url);
$apiResults = json_decode($response, true);

$apiResults = $apiResults["results"];

$finalArray = [];
$dbResults   = [
    [
        "county" => "Cork",
        "hotel"  => "Oriel House Hotel"
    ]
];


# If atleast 1 row is fecthed from
# database
if (count($dbResults)) {
    foreach ($dbResults as $dbResult) {
        $storedCountryName = $dbResult["county"];
        $storedHotelName   = $dbResult["hotel"];
        foreach ($apiResults as $apiResult) {
            if ($storedCountryName  === $apiResult['address']['city']
                && $storedHotelName === $apiResult['property_name'])
            {
                $finalArray[] = $apiResult;
            }
        }
    }
}

print_r($finalArray);
?>

关于php - 按城市和酒店搜索API,查找数据库中存在的城市和酒店,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44235875/

10-10 13:14