### Create an AlsoAsked API Client Source: https://github.com/alsoasked/also-asked-php/blob/main/README.md Initializes the API client using a PSR-18 HTTP client and configures authentication and base URI plugins. Ensure a PSR-18 compatible client like Guzzle is installed. ```php 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); ``` -------------------------------- ### Fetch Account Details Source: https://github.com/alsoasked/also-asked-php/blob/main/README.md Retrieves account information using the getAccount method, which maps to the GET /v1/account endpoint. ```php /** * @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: mantis.toboggan@gmail.com // Plan Type: pro // Credits: 100 // Credits Reset At: +2023-09-14T01:19:27+00:00 // Registered At: +2022-03-23T17:54:19+00:00 ``` -------------------------------- ### Install AlsoAsked PHP Client via Composer Source: https://github.com/alsoasked/also-asked-php/blob/main/README.md Add this dependency to your composer.json file to include the AlsoAsked PHP client in your project. ```json { "require": { "AlsoAsked/also-asked-php": "1.0.0" } } ``` -------------------------------- ### Perform a Search Request Source: https://github.com/alsoasked/also-asked-php/blob/main/README.md Executes a search using SearchRequestOptions and processes the results. The performSearch method maps to the POST /v1/search endpoint. ```php $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? // ... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.