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);
$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?
// ...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.