PHP code example of alsoasked / also-asked-php

1. Go to this page and download the library: Download alsoasked/also-asked-php 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/ */

    

alsoasked / also-asked-php example snippets




$httpClient = (new \Http\Client\Common\PluginClientBuilder())
    // add a base URI plugin to point to the live API URL
    ->addPlugin(new \Http\Client\Common\Plugin\BaseUriPlugin(
        \Http\Discovery\UriFactoryDiscovery::find()
            ->createUri('https://alsoaskedapi.com/v1'),
    ))
    // add an authentication plugin to add the API key header
    ->addPlugin(new \Jane\Component\OpenApiRuntime\Client\Plugin\AuthenticationRegistry([
        new \AlsoAsked\Api\Authentication\ApiKeyAuthentication('your-api-key'),
    ]))
    // create the PSR-18 HTTP client
    ->createClient(\Http\Discovery\Psr18ClientDiscovery::find());

// create the API client with the PSR-18 HTTP client
$apiClient = \AlsoAsked\Api\Client::create($httpClient);

/**
 * @var \AlsoAsked\Api\Model\Account
 */
$account = $apiClient->getAccount();

echo 'Account ID: ' . $account->getId() . \PHP_EOL;
echo 'Name: ' . $account->getName() . \PHP_EOL;
echo 'Email: ' . $account->getEmail() . \PHP_EOL;
echo 'Plan Type: ' . $account->getPlanType() . \PHP_EOL;
echo 'Credits: ' . $account->getCredits() . \PHP_EOL;
echo 'Credits Reset At: ' . $account->getCreditsResetAt()->format(DateTimeInterface::ISO8601_EXPANDED) . \PHP_EOL;
echo 'Registered At: ' . $account->getRegisteredAt()->format(DateTimeInterface::ISO8601_EXPANDED) . \PHP_EOL;

// outputs:
//
// Account ID: 6G8QgoK9ar0E1pB7Rl0LN5mxljdAvBWb
// Name: Mantis Toboggan
// Email: [email protected]
// Plan Type: pro
// Credits: 100
// Credits Reset At: +2023-09-14T01:19:27+00:00
// Registered At: +2022-03-23T17:54:19+00:00

$request = (new \AlsoAsked\Api\Model\SearchRequestOptions())
    ->setTerms(['cars'])
    ->setLanguage('en')
    ->setRegion('us')
    ->setDepth(2)
    ->setFresh(false)
    ->setAsync(false)
    ->setNotifyWebhooks(true);

/**
 * @var \AlsoAsked\Api\Model\SearchRequestResults
 */
$results = $apiClient->performSearch($request);

// ensure the search request was successful
if ($results->getStatus() !== 'success') {
    echo 'We expected the status to be "success", but encountered ' . $results->getStatus();

    exit;
}

/**
 * Recursively print a search result and it's children.
 *
 * @param \AlsoAsked\Api\Model\SearchResult $result
 *
 * @return void
 */
function printResult(\AlsoAsked\Api\Model\SearchResult $result): void
{
    echo '- Question: ' . $result->getQuestion() . \PHP_EOL;

    foreach ($result->getResults() as $childResult) {
        \printResult($childResult);
    }
}

// print the queries and their results

foreach ($results->getQueries() as $query) {
    echo 'Term: ' . $query->getTerm() . \PHP_EOL;
    echo 'Results:' . \PHP_EOL;

    foreach ($query->getResults() as $result) {
        \printResult($result);
    }
}

// outputs:
//
// Term: cars
// Results:
// - Question: What are 10 best cars to buy?
// - Question: What are top 5 most reliable cars?
// - Question: What is the #1 most reliable car?
// - Question: Who is car 20 in Cars?
// - Question: Who owns Towbin Dodge now?
// - Question: What kind of car is Mater?
// ...