跳转到主要内容

前提条件

  • PHP 8.0+ (需启用 cURL 扩展)
  • 一个 Firecrawl API 密钥——免费获取

进行网页搜索

Firecrawl 可通过 cURL 调用 REST API 在 PHP 中使用。
<?php
$apiKey = getenv('FIRECRAWL_API_KEY');

$ch = curl_init('https://api.firecrawl.dev/v2/search');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'query' => 'firecrawl web scraping',
        'limit' => 5,
    ]),
]);

$response = curl_exec($ch);
curl_close($ch);

$results = json_decode($response, true);
foreach ($results['data']['web'] as $result) {
    echo $result['title'] . ' - ' . $result['url'] . "\n";
}
{
  "success": true,
  "data": {
    "web": [
      {
        "url": "https://docs.firecrawl.dev",
        "title": "Firecrawl Documentation",
        "markdown": "# Firecrawl\n\nFirecrawl is a web scraping API..."
      }
    ]
  }
}

抓取网页

<?php
$ch = curl_init('https://api.firecrawl.dev/v2/scrape');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'url' => 'https://example.com',
    ]),
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
echo $data['data']['markdown'];
{
  "success": true,
  "data": {
    "markdown": "# Example Domain\n\nThis domain is for use in illustrative examples...",
    "metadata": {
      "title": "Example Domain",
      "sourceURL": "https://example.com"
    }
  }
}

与页面交互

启动浏览器会话,使用自然语言 prompt 与页面交互,然后关闭会话。

第 1 步 — 先抓取页面以启动会话

<?php
$ch = curl_init('https://api.firecrawl.dev/v2/scrape');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'url' => 'https://www.amazon.com',
        'formats' => ['markdown'],
    ]),
]);

$response = curl_exec($ch);
curl_close($ch);

$scrapeResult = json_decode($response, true);
$scrapeId = $scrapeResult['data']['metadata']['scrapeId'];
echo "scrapeId: $scrapeId\n";

第 2 步 — 发送交互指令

<?php
$interactUrl = "https://api.firecrawl.dev/v2/scrape/$scrapeId/interact";
$headers = [
    'Authorization: Bearer ' . $apiKey,
    'Content-Type: application/json',
];

// 搜索产品
$ch = curl_init($interactUrl);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => json_encode([
        'prompt' => 'Search for iPhone 16 Pro Max',
    ]),
]);

$response = curl_exec($ch);
curl_close($ch);
echo $response . "\n";

// 点击第一个结果
$ch = curl_init($interactUrl);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => json_encode([
        'prompt' => 'Click on the first result and tell me the price',
    ]),
]);

$response = curl_exec($ch);
curl_close($ch);
echo $response . "\n";

步骤 3 — 结束会话

<?php
$ch = curl_init($interactUrl);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'DELETE',
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
    ],
]);

curl_exec($ch);
curl_close($ch);

echo "Session stopped\n";

可复用的辅助类

如果需要反复使用,可将 API 封装为一个类:
<?php
class Firecrawl
{
    private string $apiKey;
    private string $baseUrl = 'https://api.firecrawl.dev/v2';

    public function __construct(string $apiKey)
    {
        $this->apiKey = $apiKey;
    }

    private function post(string $endpoint, array $payload): array
    {
        $ch = curl_init($this->baseUrl . $endpoint);
        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => [
                'Authorization: Bearer ' . $this->apiKey,
                'Content-Type: application/json',
            ],
            CURLOPT_POSTFIELDS => json_encode($payload),
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpCode >= 400) {
            throw new \RuntimeException("Firecrawl API error: HTTP $httpCode");
        }

        return json_decode($response, true);
    }

    public function scrape(string $url, array $options = []): array
    {
        return $this->post('/scrape', array_merge(['url' => $url], $options));
    }

    public function search(string $query, int $limit = 5): array
    {
        return $this->post('/search', ['query' => $query, 'limit' => $limit]);
    }
}

// 使用方式
$app = new Firecrawl(getenv('FIRECRAWL_API_KEY'));
$result = $app->scrape('https://example.com');
echo $result['data']['markdown'];

下一步

搜索文档

进行网页搜索并获取完整页面内容

抓取文档

包括 formats、actions 和代理在内的所有抓取选项

交互文档

点击、填写表单并提取动态内容

API 参考

完整的 REST API 文档