shanginn/cloudflare-browser
Composer 安装命令:
composer require shanginn/cloudflare-browser
包简介
A strongly-typed, modern PHP SDK for the Cloudflare Browser Rendering REST API
README 文档
README
A strongly-typed, modern PHP SDK for the Cloudflare Browser Rendering REST API. Built with amphp/http-client for performance and crell/serde for robust object serialization.
Features
- Full Coverage: Supports
/content,/screenshot,/pdf,/scrape,/json,/snapshot,/links, and/markdownendpoints. - Strong Typing: Uses Request DTOs and specific Response objects.
- AI Extraction: Seamless integration with
spiral/json-schema-generatorto extract structured data from webpages into PHP objects. - Custom Exceptions: Granular error handling for API errors.
Installation
composer require shanginn/cloudflare-browser
Documentation
📚 View the full Wiki Documentation for detailed guides on all endpoints.
Quick Links
- Getting Started & REST API Overview
- Fetching HTML Content
- Capturing Screenshots
- Generating PDFs
- Taking Snapshots
- Scraping Elements
- AI Structured Data Extraction
- Retrieving Links
- Extracting Markdown
Basic Usage
Setup
use Shanginn\CloudflareBrowser\CloudflareClient; use Shanginn\CloudflareBrowser\CloudflareBrowser; $accountId = getenv('CLOUDFLARE_ACCOUNT_ID'); $apiToken = getenv('CLOUDFLARE_API_TOKEN'); $client = new CloudflareClient($accountId, $apiToken); $browser = new CloudflareBrowser($client);
Take a Screenshot
use Shanginn\CloudflareBrowser\Requests\ScreenshotRequest; $pngData = $browser->screenshot(new ScreenshotRequest( url: 'https://example.com', screenshotOptions: ['fullPage' => true] )); file_put_contents('screenshot.png', $pngData);
See the Screenshot Documentation for more examples including:
- Custom HTML screenshots
- Authenticated pages
- Full-page captures
- Element-specific screenshots
- High-resolution captures
Scrape Elements
use Shanginn\CloudflareBrowser\Requests\ScrapeRequest; $results = $browser->scrape(new ScrapeRequest( url: 'https://news.ycombinator.com', elements: [ ['selector' => '.titleline > a'] ] )); foreach ($results as $group) { foreach ($group->results as $element) { echo "Found: {$element->text} ({$element->attributes['href'] ?? ''})\n"; } }
See the Scraping Documentation for more details.
Advanced Usage
AI Structured Data Extraction
Define your target data structure using a PHP class and attributes. The SDK will generate the JSON schema and map the AI response back to your object.
1. Define Schema:
use Spiral\JsonSchemaGenerator\Attribute\Field; class ProductSchema { public function __construct( #[Field(title: 'Product Title', description: 'The main name of the item')] public string $title, #[Field(title: 'Price', description: 'Current price')] public float $price, ) {} }
2. Extract:
use Shanginn\CloudflareBrowser\Requests\JsonRequest; /** @var ProductSchema $product */ $product = $browser->json( new JsonRequest( url: 'https://example-shop.com/item/123', prompt: 'Extract the main product details' ), ProductSchema::class ); echo "Product: {$product->title} - \${$product->price}\n";
See the JSON Endpoint Documentation for more examples including array extraction and custom AI models.
Generating PDFs
use Shanginn\CloudflareBrowser\Requests\PdfRequest; use Shanginn\CloudflareBrowser\Requests\Common\Viewport; $pdfData = $browser->pdf(new PdfRequest( url: 'https://example.com', viewport: new Viewport(width: 1200, height: 800) )); file_put_contents('page.pdf', $pdfData);
See the PDF Documentation for more examples including:
- Custom headers and footers
- Page format options (A4, A5, Letter, etc.)
- Blocking images/resources
- Dynamic placeholders (page numbers, dates, titles)
Fetch HTML Content
$html = $browser->content('https://example.com'); echo $html;
See the Content Documentation for handling JavaScript-heavy pages and blocking resources.
Extract Markdown
$markdown = $browser->markdown('https://example.com'); echo $markdown;
See the Markdown Documentation for more details.
Get All Links
$links = $browser->links('https://example.com'); // Or get only internal links $internalLinks = $browser->links( url: 'https://example.com', excludeExternal: true );
See the Links Documentation for more details.
Take a Snapshot
use Shanginn\CloudflareBrowser\Requests\SnapshotRequest; $snapshot = $browser->snapshot(new SnapshotRequest( url: 'https://example.com' )); echo "Title: " . $snapshot->title . "\n"; echo "Content: " . $snapshot->content . "\n";
See the Snapshot Documentation for more examples.
Authentication
Before you begin, make sure you create a custom API Token with the following permissions:
Browser Rendering - Edit
Set these environment variables:
CLOUDFLARE_ACCOUNT_ID- Your Cloudflare Account IDCLOUDFLARE_API_TOKEN- Your Cloudflare API Token
Monitoring Usage
You can monitor Browser Rendering usage in two ways:
- In the Cloudflare dashboard, go to the Browser Rendering page to view aggregate metrics. Go to Browser Rendering
X-Browser-Ms-Usedheader: Returned in every REST API response, reporting browser time used for that request (in milliseconds).
Troubleshooting
If you have questions or encounter an error, see the Browser Rendering FAQ and troubleshooting guide.
License
MIT
shanginn/cloudflare-browser 适用场景与选型建议
shanginn/cloudflare-browser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 shanginn/cloudflare-browser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shanginn/cloudflare-browser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 22
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-01