API按城市和酒店搜索,查找我的数据库中存在的城市和酒店

时间:2017-05-29 06:27:42

标签: php mysql sql wordpress api

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

我的城市名称: "软木"

我的酒店列表:

  1. " Oriel House Hotel"

  2. " Jurys Inn Cork"

  3. 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 ;
    

1 个答案:

答案 0 :(得分:0)

您的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);
?>