starkeen / yandex-search-api
使用 Yandex 搜索 API 的库
1.2.0
2024-01-01 09:56 UTC
Requires
- php: ^8.0
- ext-simplexml: *
- guzzlehttp/guzzle: ^7.8
- psr/log: ^3.0
Requires (Dev)
- phpmd/phpmd: ^2.15
- phpstan/phpstan: ^1.10
- phpunit/php-code-coverage: ^9.2
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.8
README
Yandex 搜索 API 库
描述
这个库是方便与 Yandex 搜索 API 交互的方式。它允许轻松将 Yandex 搜索功能集成到您的 PHP 项目中。该库封装了与 API 交互的复杂逻辑,并为执行搜索查询提供了简单易懂的接口。
安装
要安装库,请使用 Composer 将其添加到项目中
composer require starkeen/yandex-search-api
用法
要使用库,您需要获取一个 API 密钥。您可以在 Yandex 搜索 API 页面上获取。
<?php require_once 'vendor/autoload.php'; use YandexSearch\SearchException; use YandexSearch\SearchRequest; use YandexSearch\YandexSearchService; // Create a guzzlehttp client instance $httpClient = new \GuzzleHttp\Client(); // Create a logger instance (any of LoggerInterface implementations) $logger = new \Psr\Log\NullLogger(); // Initialize the client with your API key $client = new YandexSearchService($httpClient, $logger); $client->setApiId('abcdefg'); // Folder ID from your Yandex Cloud account $client->setApiKey('A1B2C3D4'); // API key from your Yandex Cloud account // Your search query $query = 'Кому на Руси жить хорошо?'; $searchRequest = new SearchRequest($query); try { $response = $client->search($searchRequest); // Process the results foreach ($response->getResults() as $result) { echo 'Title: ' . $result->getTitle() . PHP_EOL; echo 'URL: ' . $result->getUrl() . PHP_EOL; echo 'Snippet: ' . $result->getSnippet() . PHP_EOL; } } catch (SearchException $e) { echo $e->getMessage(); }
配置
要使用库,您需要提供您的 Yandex 搜索 API 密钥和您的账户 ID。您可以通过在 Yandex Cloud Console 上注册应用程序来获取它。
您可以在代码中将 API 密钥设置如下
$client = new YandexSearchService($httpClient, $logger); $client->setApiId('abcdefg'); // Folder ID from your Yandex Cloud account $client->setApiKey('A1B2C3D4'); // API key from your Yandex Cloud account
还建议将密钥存储在配置文件中,并在创建客户端实例时使用它。
问题和反馈
如果您有任何问题、问题或改进建议,请创建一个 GitHub 上的 问题。
许可
此库根据 MIT 许可证分发。有关详细信息,请参阅 LICENSE 文件。