SDKs

PHP

Thunderbit Open API 的地道 PHP 写法

用 Guzzle。一行 Composer 装好;支持 PHP 8.1+。

安装

composer require guzzlehttp/guzzle

配置

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;

$api = new Client([
    'base_uri' => 'https://openapi.thunderbit.com/openapi/v1/',
    'headers'  => [
        'Authorization' => 'Bearer ' . getenv('THUNDERBIT_API_KEY'),
        'Content-Type'  => 'application/json',
    ],
    'timeout'  => 60,
]);

Distill 一个页面

$res = $api->post('distill', [
    'json' => ['url' => 'https://thunderbit.com/playground'],
]);
$data = json_decode($res->getBody(), true);
echo $data['data']['markdown'];

Extract 结构化数据

$res = $api->post('extract', [
    'json' => [
        'url'    => 'https://example.com/product/iphone-15-pro',
        'schema' => [
            'type' => 'object',
            'properties' => [
                'name'  => ['type' => 'string'],
                'price' => ['type' => 'number'],
            ],
            'required' => ['name', 'price'],
        ],
    ],
]);
$data = json_decode($res->getBody(), true)['data'];

Batch + Webhook

$job = json_decode($api->post('batch/distill', [
    'json' => [
        'urls'    => ['https://example.com/page1', 'https://example.com/page2'],
        'webhook' => [
            'url'    => 'https://your-server.com/webhook',
            'secret' => getenv('WEBHOOK_SECRET'),
        ],
    ],
])->getBody(), true);

error_log('Batch submitted: ' . $job['data']['id']);

在 handler 里校验 webhook 签名 —— 详见 Webhooks

官方 PHP SDK 正在开发中,敬请期待。