urieldavid/desafio_xlr8_php

v2.2.0 2023-10-31 17:37 UTC

This package is auto-updated.

Last update: 2024-09-30 02:02:53 UTC


README

开发 PHP 包用于 XLR8 测试的项目。该项目包括创建一个 PHP 库以消费 XLR8 API,并通过经纬度坐标,生成离您最近或更便宜的酒店列表。

使用 Composer 安装库

composer require urieldavid/desafio_xlr8_php

之后,需要在项目的根目录下创建 json 和 config.json 文件,包含用于使用库的有效端点,以下是一个文件示例

{
    "source_1": "https://xlr8-interview-files.s3.eu-west-2.amazonaws.com/source_1.json",
    "source_2": "https://xlr8-interview-files.s3.eu-west-2.amazonaws.com/source_2.json"
}

使用库

use XLR8\Exception\XLR8Exception;
use XLR8\Search;

require implode(DIRECTORY_SEPARATOR, [__DIR__, "vendor", "autoload.php"]);

try {
    $search = new Search();
    $search->getNearbyHotels(41.157944, -8.629105, "pricepernight");
} catch (XLR8Exception $e) {
    echo $e->getMessage();
}

use XLR8\Exception\XLR8Exception;
use XLR8\Search;

require implode(DIRECTORY_SEPARATOR, [__DIR__, "vendor", "autoload.php"]);

try {
    $options = [
        'page' => 1,
        'limit' => 15,
        'responseJson' => false | true,
        'selectSource' => null | 'source_1' | 'any other endpoint in config.json'
    ];
    $search = new Search();
    $search->getNearbyHotels(41.157944, -8.629105, "pricepernight", $options);
} catch (XLR8Exception $e) {
    echo $e->getMessage();
}

注意。

  • 默认参数要求 getNearbyHotels($latitude, $longitude)

  • 要按 "pricepernight" 排序 => getNearbyHotels($latitude, $longitude, "pricepernight")

  • 默认排序为 "proximity"

  • 参数选项

        $options = [
            'page' => 1,
            'limit' => 15,
            'responseJson' => false | true,
            'selectSource' => null | 'source_1' | 'any other endpoint in config.json'
        ];

    注意-1:默认情况下将返回所有结果,如果您想要分页,则需要将值传递到 options 参数中,$page => 选中页码$limit => 每页数据限制

    注意-2:这是可选的,但可以设置信息缓存时间,new Search(3600);,在这种情况下,3600 秒相当于 1 小时,默认缓存为 1 天 42300 秒。