docaxess/php-apify-sdk

Apify服务的非官方SDK

0.4.0 2024-06-05 12:17 UTC

This package is auto-updated.

Last update: 2024-09-05 12:48:22 UTC


README

Latest Version CC-BY-1.0 License

CI Testing

安装

composer require docaxess/php-apify-sdk

令牌生成

您可以通过以下说明生成令牌:

  • 访问Apify控制台
  • 进入设置 > 集成
  • 点击“+ 添加令牌”按钮

您可以在Apify文档中找到更多详细信息

使用方法

首先,您需要创建ApifyConnector类的新实例,并将令牌作为参数传递。

use DocAxess\Apify\ApifyConnector;

$apify = new ApifyConnector('YOUR_APIFY_TOKEN');

从这一点开始,您可以使用类中可用的不同方法。

获取用户信息

$user = $apify->user()->me(); 

启动新的actor运行

use DocAxess\Apify\ApifyConnector;
use DocAxess\Apify\Task\Data\Option\TaskOption;

// The ID of the actor you want to run, it can be found in the actor's URL
// or you can use the slug like 'yanis~actor-name'
$actorId = 'YOUR_ACTOR_ID'; 
$apify = new ApifyConnector('YOUR_APIFY_TOKEN');
$run = $apify->taskRunner()->run($actorId);

// it also possible to pass configuration options and input data
$config = new TaskOption(
    build: '1.2.3', 
    timeout: 300, 
    memory: 2048, 
);
$run = $apify->taskRunner()->run($actorId, $config, [
    'key' => 'value' // input data to pass to the actor run
]);

向actor添加webhook

use DocAxess\Apify\ApifyConnector;
use DocAxess\Apify\Task\Data\Option\TaskOption;
use DocAxess\Apify\Webhook\Config\WebhookConfig;
use DocAxess\Apify\Webhook\Event\EventType;

$actorId = 'YOUR_ACTOR_ID'; 
$apify = new ApifyConnector('YOUR_APIFY_TOKEN');

$config = new TaskOption();
$config->addWebhook(WebhookConfig::forEvent(EventType::RUN_SUCCEEDED, 'https://your-webhook-url.com'));

$run = $apify->taskRunner()->run($actorId, $config);

可以使用提供的对象解析webhook事件详情。

use DocAxess\Apify\Webhook\Data\EventResult;

$eventResult = EventResult::fromArray(request()->all());

获取数据集

use DocAxess\Apify\ApifyConnector;
use DocAxess\Apify\Webhook\Data\EventResult;
use DocAxess\Apify\Core\Type\Identifier;

$eventResult = EventResult::fromArray(request()->all());

$apify = new ApifyConnector('YOUR_APIFY_TOKEN');
$results = $apify->dataset()->getJson($eventResult->datasetId);

// or if you know the dataset ID
$results = $apify->dataset()->getJson(Identifier::make('YOUR_DATASET_ID'));

默认情况下,数据集将返回关联数组,但您也可以为每个项目提供DTO类来返回。它应该实现DocAxess\Apify\Dataset\Item\Item接口。

use DocAxess\Apify\ApifyConnector;
use DocAxess\Apify\Webhook\Data\EventResult;
use DocAxess\Apify\Core\Type\Identifier;

$apify = new ApifyConnector('YOUR_APIFY_TOKEN');
$results = $apify->dataset()->getJson(Identifier::make('YOUR_DATASET_ID'), YourDtoForItem::class);

免责声明

关联

此包不是来自Apify的官方包。这是一个使用Apify API的社区包。它以任何方式与Apify无关。

API覆盖范围

此包未覆盖所有Apify API,主要是为了覆盖我们自己的用例而构建。它是一个正在进行的工作,并将随着时间的推移而更新。如果您需要特定功能,请随时提出问题或提交拉取请求。