alsoasked/also-asked-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alsoasked/also-asked-php

Composer 安装命令:

composer require alsoasked/also-asked-php

包简介

A PHP library for the AlsoAsked API

README 文档

README

AlsoAsked Logo

For more information, please visit https://developers.alsoasked.com.

Installation & Usage

Requirements

PHP 7.4 and later.

Composer

To install the bindings via Composer, add the following to composer.json:

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

Then run composer install

Examples

Create a client

You'll need to begin by creating a client for the API you wish to use, as well as specify the API key you wish to use.

We do this by first creating an HTTP client respecting the PSR-18 standard.

The \Http\Discovery\Psr18ClientDiscovery class will automatically discover an PSR-18 HTTP client from the installed composer packages, meaning you'll need an HTTP client installed which implements PSR-18 if you don't already.

A common HTTP client which does is Guzzle, we can install this by running composer require guzzlehttp/guzzle:^7.0.

The example below creates a client for the Live API by setting a base URI using the \Http\Client\Common\Plugin\BaseUriPlugin plugin. If you wish to use the Sandbox API, you can change the base URI from https://alsoaskedapi.com/v1 to https://sandbox.alsoaskedapi.com/v1.

You'll need to change the API specified in the \AlsoAsked\Api\Authentication\ApiKeyAuthentication plugin from your-api-key to the API key you've created. If you don't have an API key, follow the authentication guide.

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

Fetch your account details

Use getAccount to fetch your account details, this calls the GET /v1/account API endpoint.

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

Perform a search

Use performSearch to perform a search request, this calls the POST /v1/search API endpoint.

$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

If you need more information, see the developer documentation, or get in touch with us at help@alsoasked.com.

alsoasked/also-asked-php 适用场景与选型建议

alsoasked/also-asked-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 852 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 alsoasked/also-asked-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alsoasked/also-asked-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 alsoasked/also-asked-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 852
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-24