r/PHP • u/davidbalbino • 36m ago
I rebuilt crawl4ai in PHP: ~20× faster web → Markdown for LLMs, typed extraction, no browser needed
Hey r/PHP 👋
I've been using crawl4ai (the popular Python crawler for LLM/RAG pipelines) and wanted the same thing natively in PHP. So I built PAM Crawl: it covers all of crawl4ai's features, with a different engine underneath.
The idea: crawl4ai opens a browser for every page. Most pages don't need one. PAM Crawl speaks HTTP/2 directly (curl_multi), parses with PHP's native HTML5 parser (Dom\HTMLDocument), and only renders JavaScript when a page actually needs it.
📊 Numbers
460 real pages served from a local nginx, each one turned into Markdown + main-content Markdown + links + media:
• pam-crawl, 8 worker processes: 448 pages/s, 418 MB peak RAM
• pam-crawl, 1 process: ~150 pages/s, 94 MB
• crawl4ai, HTTP mode: 7.9 pages/s, 525 MB
• crawl4ai, browser mode (default): 6.3 pages/s, 3.8 GB
The methodology and benchmark scripts are in the repo, so you can reproduce it.
🧑💻 What it looks like
// clean Markdown for RAG
$page = Crawl::url('https://www.php.net/manual/en/function.array-map.php')->get();
echo $page->content->raw; // main content only
echo $page->markdown(focus: 'callback'); // BM25: just the relevant sections
// typed extraction straight into your own classes
#[Item('article.product_pod')]
final readonly class Book {
public function __construct(
#[Css('h3 a', attr: 'title')] public string $title,
#[Css('.price_color'), Money] public int $cents,
#[Css('.availability')] public bool $inStock,
) {}
}
$books = Crawl::url('https://books.toscrape.com/')->get()->extractAll(Book::class);
// deep crawl, prioritized, polite
Crawl::site('https://docs.example.com')
->only('/docs/*')->depth(3)->limit(200)
->prioritize(Score::keywords('auth', 'tokens'))
->respectRobots()
->each(fn (Page $p) => $index->add($p->content->raw));
✨ Highlights
• Built for LLMs: clean Markdown, main-content extraction, BM25 focus, citations, chunking.
• Typed extraction with attributes, with fallback chains like #[Css('.price'), JsonLd('offers.price'), Money]. crawl4ai's JSON CSS/XPath schemas also work unchanged.
• LLM features: extraction into DTOs, schema generation, Q&A with citations. Works with OpenAI, Anthropic, Gemini or a local Ollama. "Learn once" mode: the LLM writes the selectors on the first page, and every later page runs without it.
• JavaScript apps without a browser: it reads Next.js/Nuxt/Remix hydration data and finds the JSON APIs behind a page. When you truly need rendering, the optional pam-crawl-browser plugin uses Lightpanda (~42 MB for 8 tabs) or Chrome (screenshots, PDF, logins), only for the pages that need it.
• Crawling: BFS/DFS/best-first, sitemap and Common Crawl seeding, domain mapping, adaptive crawling ("stop when the question is answered"), robots.txt, per-host politeness, HTTP cache with ETag revalidation.
• Interfaces: crawl CLI (crwl-compatible), crawl4ai-compatible HTTP API, MCP server for AI assistants, Docker image.
• Extras crawl4ai doesn't have: change monitoring with diffs, near-duplicate detection, multi-process ->workers(8), PDF parsing in pure PHP.
It runs on plain PHP 8.5 and works best on PAM (https://github.com/push-in/pam), a persistent PHP runtime where connections, caches and extraction plans stay warm between requests. PHPStan level 9, ~370 tests, Apache-2.0. Full credit to crawl4ai for the original design.
🚀 Try it
composer require pushinbr/pam-crawl
• Repo: https://github.com/push-in/pam-crawl
• 28 runnable examples: https://github.com/push-in/pam-crawl/tree/main/examples
• Browser plugin: https://github.com/push-in/pam-crawl-browser
It's a 0.1 release, so I'd love feedback, especially on the API design and on sites where the Markdown or main-content extraction comes out wrong. Happy to answer questions!