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

Php API按城市和酒店搜索,查找我的数据库中存在的城市和酒店,php,mysql,sql,wordpress,api,Php,Mysql,Sql,Wordpress,Api,按城市和酒店搜索以查找我的数据库中存在的城市和酒店。我的代码不能正常工作 我的城市名称: “软木塞” 我的酒店列表: “奥里尔之家酒店” “科克朱利斯酒店” API url: 表格 -- Table structure for table `hotels` -- CREATE TABLE IF NOT EXISTS `hotels` ( `id` int(11) NOT NULL AUTO_INCREMENT, `county` varcha

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

我的城市名称: “软木塞”

我的酒店列表:

  • “奥里尔之家酒店”

  • “科克朱利斯酒店”

  • API url:

    表格

    -- 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);
    ?> 
    
    
    
    能否显示hotels表的表结构?我假设代码中存在以下问题:1)定义$final_array=array();内环。这将每次重写最终的_数组。2) 将数据库查询国家名称与api城市进行比较。3) 最终数组2未定义。其他一些缺点:在循环中执行数据库查询。这不是必需的。您只能执行此查询一次。@Dinesh Patra请检查到底是什么不起作用?sql结果、代码本身、其他内容?@alex.pulver foreach不工作。也许是我的问题。我正在尝试搜索城市酒店哪个城市和酒店与我的数据库匹配。它可能是“$storedCountryName=$dbResult[“county”];”不知道如何回显$finalArray数据?打印($finalArray)注意:我已将apikey编辑为apikey=XXXXXXXXXXXXX。对于测试,请使用适当的api密钥。在您的问题中还隐藏了api密钥。因为在公共发布中不提供敏感数据。它显示
    Array()
    。我正在使用这个api进行演示。我的主要api尚未发布。
    <?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);
    ?>