alsoasked/also-asked-php

用于AlsoAsked API的PHP库

1.1.0 2023-09-06 18:56 UTC

This package is auto-updated.

Last update: 2024-09-06 21:24:08 UTC


README

AlsoAsked Logo

更多信息,请访问 https://developers.alsoasked.com

安装与使用

要求

PHP 7.4 及以上。

Composer

要使用 Composer 安装绑定,请将以下内容添加到 composer.json

{
  "require": {
    "AlsoAsked/also-asked-php": "1.0.0"
  }
}

然后运行 composer install

示例

创建客户端

您需要首先为要使用的 API 创建一个客户端,并指定要使用的 API 密钥。

我们首先创建一个遵守 PSR-18 标准的 HTTP 客户端。

\Http\Discovery\Psr18ClientDiscovery 类将自动从已安装的 Composer 包中发现一个 PSR-18 HTTP 客户端,这意味着如果您尚未安装实现 PSR-18 的 HTTP 客户端,则需要安装一个。

常见的实现 PSR-18 的 HTTP 客户端是 Guzzle,我们可以通过运行 composer require guzzlehttp/guzzle:^7.0 来安装它。

以下示例通过使用 \Http\Client\Common\Plugin\BaseUriPlugin 插件设置基本 URI 来创建一个针对 Live API 的客户端。如果您想使用 Sandbox API,可以将基本 URI 从 https://alsoaskedapi.com/v1 更改为 https://sandbox.alsoaskedapi.com/v1

您需要将 \AlsoAsked\Api\Authentication\ApiKeyAuthentication 插件中指定的 API 从 your-api-key 更改为您创建的 API 密钥。如果您没有 API 密钥,请遵循 认证指南

<?php

$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);

获取您的账户详情

使用 getAccount 获取您的账户详情,这会调用 GET /v1/account API 端点。

/**
 * @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

执行搜索

使用 performSearch 执行搜索请求,这会调用 POST /v1/search API 端点。

$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?
// ...

帮助

如果您需要更多信息,请参阅 开发者文档,或通过 help@alsoasked.com 联系我们。