PHP code example of starkeen / yandex-search-api

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




andexSearch\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();
}

$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