webcrawlerapi/sdk
Composer 安装命令:
composer require webcrawlerapi/sdk
包简介
A PHP SDK for WebCrawler API - turn website into data
README 文档
README
A PHP SDK for interacting with the WebCrawlerAPI - a powerful web crawling and scraping service.
In order to use the API you have to get an API key from WebCrawlerAPI
Read documentation at WebCrawlerAPI Docs for more information.
Requirements
- PHP 8.0 or higher
- Composer
ext-jsonPHP extension- Guzzle HTTP Client 7.0 or higher
Installation
You can install the package via composer:
composer require webcrawlerapi/sdk
Usage
use WebCrawlerAPI\WebCrawlerAPI; // Initialize the client $crawler = new WebCrawlerAPI('your_api_key'); // Synchronous crawling (blocks until completion) $job = $crawler->crawl( url: 'https://example.com', itemsLimit: 10, webhookUrl: 'https://yourserver.com/webhook', maxPolls: 100 // Optional: maximum number of status checks ); echo "Job completed with status: {$job->status}\n"; // Access job items and their content foreach ($job->jobItems as $item) { echo "Page title: {$item->title}\n"; echo "Original URL: {$item->originalUrl}\n"; echo "Item status: {$item->status}\n"; // Get the content based on job's scrape_type // Returns null if item is not in "done" status $content = $item->getContent(); if ($content) { echo "Content length: " . strlen($content) . "\n"; echo "Content preview: " . substr($content, 0, 200) . "...\n"; } else { echo "Content not available or item not done\n"; } } // Access job-level metadata directly from the job echo "Crawl target: {$job->url}\n"; echo "Job status: {$job->status}\n"; // Or use asynchronous crawling $response = $crawler->crawlAsync( url: 'https://example.com', itemsLimit: 10, webhookUrl: 'https://yourserver.com/webhook' ); // Get the job ID from the response $jobId = $response->id; echo "Crawling job started with ID: {$jobId}\n"; // Check job status and get results $job = $crawler->getJob($jobId); echo "Job status: {$job->status}\n"; // Access job details echo "Crawled URL: {$job->url}\n"; echo "Created at: {$job->createdAt->format('Y-m-d H:i:s')}\n"; echo "Number of items: " . count($job->jobItems) . "\n"; // Cancel a running job if needed $cancelResponse = $crawler->cancelJob($jobId); echo "Cancellation response: " . json_encode($cancelResponse) . "\n";
API Methods
crawl()
Starts a new crawling job and waits for its completion. This method will continuously poll the job status until:
- The job reaches a terminal state (done, error, or cancelled)
- The maximum number of polls is reached (default: 100)
- The polling interval is determined by the server's
recommendedPullDelayMsor defaults to 5 seconds
crawlAsync()
Starts a new crawling job and returns immediately with a job ID. Use this when you want to handle polling and status checks yourself, or when using webhooks.
getJob()
Retrieves the current status and details of a specific job.
cancelJob()
Cancels a running job. Any items that are not in progress or already completed will be marked as canceled and will not be charged.
Parameters
Crawl Methods (crawl and crawlAsync)
url(required): The seed URL where the crawler starts. Can be any valid URL.scrapeType(default: "markdown"): The type of scraping you want to perform. Can be "html", "cleaned", or "markdown".itemsLimit(default: 10): Crawler will stop when it reaches this limit of pages for this job.webhookUrl(optional): The URL where the server will send a POST request once the task is completed.whitelistRegexp(optional): A regular expression to whitelist URLs. Only URLs that match the pattern will be crawled.blacklistRegexp(optional): A regular expression to blacklist URLs. URLs that match the pattern will be skipped.maxPolls(optional, crawl only): Maximum number of status checks before returning (default: 100)
Responses
CrawlAsync Response
The crawlAsync() method returns a CrawlResponse object with:
id: The unique identifier of the created job
Job Response
The Job object contains detailed information about the crawling job:
id: The unique identifier of the joborgId: Your organization identifierurl: The seed URL where the crawler startedstatus: The status of the job (new, in_progress, done, error)scrapeType: The type of scraping performedcreatedAt: The date when the job was createdfinishedAt: The date when the job was finished (if completed)webhookUrl: The webhook URL for notificationswebhookStatus: The status of the webhook requestwebhookError: Any error message if the webhook request failedjobItems: Array of JobItem objects representing crawled pagesrecommendedPullDelayMs: Server-recommended delay between status checks
JobItem Properties
Each JobItem object represents a crawled page and contains:
id: The unique identifier of the itemjobId: The parent job identifierjob: Reference to the parent Job objectoriginalUrl: The URL of the pagepageStatusCode: The HTTP status code of the page requeststatus: The status of the item (new, in_progress, done, error)title: The page titlecreatedAt: The date when the item was createdcost: The cost of the item in $referredUrl: The URL where the page was referred fromlastError: Any error message if the item failederrorCode: The error code if the item failed (if available)getContent(): Method to get the page content based on the job's scrapeType (html, cleaned, or markdown). Returns null if the item's status is not "done" or if content is not available. Content is automatically fetched and cached when accessed.rawContentUrl: URL to the raw content (if available)cleanedContentUrl: URL to the cleaned content (if scrapeType is "cleaned")markdownContentUrl: URL to the markdown content (if scrapeType is "markdown")
Testing
Running Tests
-
Install dependencies:
composer install
-
Run unit tests:
vendor/bin/phpunit tests/Unit --testdox
-
Run integration tests (optional, requires API key):
export WEBCRAWLER_API_KEY="your-api-key" vendor/bin/phpunit tests/Integration --testdox
Or use the test runner script: ./run-tests.sh
License
MIT License
webcrawlerapi/sdk 适用场景与选型建议
webcrawlerapi/sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112 次下载、GitHub Stars 达 7, 最近一次更新时间为 2024 年 12 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「data」 「api」 「sdk」 「scraper」 「crawler」 「website」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webcrawlerapi/sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webcrawlerapi/sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webcrawlerapi/sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
A PSR-7 compatible library for making CRUD API endpoints
Data provider for yii2
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 112
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-31