1. Go to this page and download the library: Download starkeen/yandex-search-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
starkeen / yandex-search-api example snippets
andexSearchAPI\SearchException;
use YandexSearchAPI\SearchRequest;
use YandexSearchAPI\YandexSearchService;
// Any PSR-17 factory that implements both RequestFactoryInterface and StreamFactoryInterface.
// Popular choices: Nyholm\Psr7\Factory\Psr17Factory, Laminas\Diactoros\RequestFactory, etc.
$factory = new \Nyholm\Psr7\Factory\Psr17Factory();
// Any PSR-18 HTTP client.
// Popular choices: Symfony\Component\HttpClient\Psr18Client, Guzzle 7+ (implements PSR-18), etc.
$httpClient = new \Symfony\Component\HttpClient\Psr18Client();
// Any PSR-3 logger.
$logger = new \Psr\Log\NullLogger();
// Initialize the service with your credentials:
// - Folder ID from your Yandex Cloud account
// - API key from your Yandex Cloud account
$service = new YandexSearchService($httpClient, $factory, $logger, 'abcdefg', 'A1B2C3D4');
// Your search query
$searchRequest = new SearchRequest('Кому на Руси жить хорошо?');
try {
$response = $service->search($searchRequest);
// Process the results
foreach ($response->getResults() as $result) {
echo 'Title: ' . $result->getTitle() . PHP_EOL;
echo 'URL: ' . $result->getUrl() . PHP_EOL;
echo 'Domain: ' . $result->getDomain() . PHP_EOL;
echo 'Snippet: ' . $result->getSnippet() . PHP_EOL;
}
} catch (SearchException $e) {
echo $e->getMessage();
}
$service = new YandexSearchService($httpClient, $factory, $logger, 'abcdefg', 'A1B2C3D4');
use YandexSearchAPI\SearchRequest;
use YandexSearchAPI\constant\Filter;
use YandexSearchAPI\constant\Language;
use YandexSearchAPI\constant\Sort;
$request = new SearchRequest('php');
$request->setNumResults(20); // results per page (default 10)
$request->setPage(1); // page number, starting from 0
$request->setMaxPassages(5); // passages per document (default 4)
$request->setLanguage(Language::ENGLISH); // RUSSIAN (default) | ENGLISH | TURKISH
$request->setSort(Sort::DATE); // RELEVANCE (default) | DATE
$request->setFilter(Filter::STRICT); // NONE (default) | MODERATE | STRICT
$response = $service->search($request);
$response->getRequestID(); // string — Yandex request id
foreach ($response->getResults() as $result) {
$result->getTitle(); // string
$result->getUrl(); // string
$result->getDomain(); // string — host extracted from the URL
$result->getSnippet(); // string|null
}
// Pagination (null when the response contains no results)
$pagination = $response->getPagination();
if ($pagination !== null) {
$pagination->getTotal(); // int|null — total documents found
$pagination->getTotalHuman(); // string|null — human-readable count
$pagination->getCurrentPage(); // int|null
$pagination->getPageSize(); // int|null
$pagination->getPagesCount(); // int — total pages (0 if unknown)
}
// Spelling correction (null when Yandex did not suggest one)
$correction = $response->getCorrection();
if ($correction !== null) {
$correction->getSourceText(); // string — the original query
$correction->getResultText(); // string — the corrected query
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.