Cultuurnet版本3的搜索服务
2.0.0
2024-08-08 07:21 UTC
Requires
- php: ^8
- guzzlehttp/guzzle: ^6.5.8|^7.4.5
- jms/serializer: 3.24.0
- simple-bus/jms-serializer-bridge: ^6.3.1
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^10.4.1
- publiq/php-cs-fixer-config: ^2.0
This package is auto-updated.
Last update: 2024-08-27 12:24:20 UTC
README
此PHP库允许您与publiq的Search API3集成。
为了兼容性,我们还有一个独立的分支 main-php7
,如果您需要与PHP 7.4兼容。
完整API文档:https://docs.publiq.be/docs/uitdatabank/search-api/introduction
获取API密钥:https://platform.publiq.be
安装
composer require cultuurnet/search-v3
使用
设置一个Guzzle客户端,带有用于Search API3测试环境的URL和API密钥,如下所示
$httpClient = new GuzzleHttp\Client([ 'base_uri' => 'https://search-test.uitdatabank.be/', 'headers' => [ 'X-Api-Key' => 'YOUR_API_KEY_FOR_TEST_ENV', ], ]);
然后,设置search-v3的SearchClient
,如下所示
$searchClient = new CultuurNet\SearchV3\SearchClient( $httpClient, // HTTP client set up in the previous step new CultuurNet\SearchV3\Serializer\Serializer() // Built-in serializer to deserialize the JSON responses );
然后您可以执行如下搜索
$searchQuery = new \CultuurNet\SearchV3\SearchQuery(); $events = $searchClient->searchEvents($searchQuery); // Search events $places = $searchClient->searchPlaces($searchQuery); // Search places $offers = $searchClient->searchOffers($searchQuery); // Search both events + places
结果是\CultuurNet\SearchV3\ValueObjects\PagedCollection
的一个实例,它支持以下方法
// Get pagination info $events->getItemsPerPage(); $events->getTotalItems(); // Get the search results, as instances of CultuurNet\SearchV3\ValueObjects\Event // and CultuurNet\SearchV3\ValueObjects\Place $events->getMember()->getItems(); // Get the facet results as an instance of CultuurNet\SearchV3\ValueObjects\FacetResults $events->getFacets();
要自定义搜索,您可以像这样配置\CultuurNet\SearchV3\SearchQuery
$searchQuery = new \CultuurNet\SearchV3\SearchQuery(); // Embed the JSON-LD of the search results, instead of only the ID and type. $searchQuery->setEmbed(true); // Set the number of which result to fetch first. Defaults to 0. // If the first page had a limit of 30 for example, and you want to get the results of the second page, set the start to // 30. (So the start is always the limit multiplied by the page number you want to get, starting with 0.) $searchQuery->setStart(0); // Set the max amount of results to return per page. $searchQuery->setLimit(30); // Add a sort (see SAPI3 docs for possible fields to sort on) $searchQuery->addSort('created', 'ASC'); // Remove a sort $searchQuery->removeSort('created'); // Add a search parameter (see src/Parameter for all options) $searchQuery->addParameter( new CultuurNet\SearchV3\Parameter\AudienceType( CultuurNet\SearchV3\Parameter\AudienceType::AUDIENCE_EDUCATION ) ); // Remove a search parameter (see src/Parameter for all options) $searchQuery->removeParameter( new CultuurNet\SearchV3\Parameter\AudienceType( CultuurNet\SearchV3\Parameter\AudienceType::AUDIENCE_EDUCATION ) );
某些参数允许有多个选项,并且可以添加多次
$searchQuery->addParameter(new CultuurNet\SearchV3\Parameter\Label('foo')); $searchQuery->addParameter(new CultuurNet\SearchV3\Parameter\Label('bar')); // Will return only results that have both labels.