nzo / grabber-bundle
Symfony bundle to crawl websites and extract links, images, scripts and stylesheets
Requires
- php: >=8.1
- ext-curl: *
- ext-dom: *
- symfony/config: ^6.4 || ^7.0 || ^8.0
- symfony/css-selector: ^6.4 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/dom-crawler: ^6.4 || ^7.0 || ^8.0
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/http-client: ^6.4 || ^7.0 || ^8.0
- symfony/http-client-contracts: ^3.0
- symfony/http-foundation: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.68
- phpstan/phpstan: ^1.12 || ^2.0
- phpunit/phpunit: ^9.6
- rector/rector: ^1.2 || ^2.0
- symfony/filesystem: ^6.4 || ^7.0 || ^8.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A Symfony bundle that crawls HTML pages and extracts links, images, scripts and stylesheets for tasks such as building a link inventory.
Which version do I need?
| Your application | Bundle version | Install |
|---|---|---|
| Symfony 6.4, 7 or 8, with PHP 8.1 or later as required by Symfony | 4 | composer require nzo/grabber-bundle:^4.0 |
| Symfony 3 or 4 | 3 | composer require nzo/grabber-bundle:^3.0 |
The documentation for version 3 is on the 3.x branch.
Coming from version 3? See UPGRADE-4.0.md for the changes to service wiring, results and error handling.
Installation
Enable the PHP cURL (ext-curl) and DOM (ext-dom) extensions, then install the bundle:
composer require nzo/grabber-bundle:^4.0
Symfony Flex registers the bundle automatically. Without Flex, add it to config/bundles.php:
return [ // ... Nzo\GrabberBundle\NzoGrabberBundle::class => ['all' => true], ];
No configuration is required to start.
Usage
Crawl a section of a site
Inject Grabber into a service, command or controller. For example, collect the links found in a documentation site while skipping its archive, previews and downloads:
use Nzo\GrabberBundle\Grabber\Grabber; class DocumentationLinks { public function __construct(private Grabber $grabber) { } /** @return list<string> */ public function collect(): array { return $this->grabber->grabUrls( 'https://docs.example.com/guide/', notScannedUrlsTab: ['https://docs.example.com/guide/archive/'], exclude: '/preview/', extensionTab: ['pdf', 'zip'], ); } }
The crawler follows <a href> links in breadth-first order. By default, it stays on the starting URL's origin: the same scheme, host and effective port. A starting path such as /guide/ does not otherwise restrict the crawl to that path.
notScannedUrlsTabexcludes exact, normalized absolute URLs.excludeexcludes URLs containing a case-sensitive substring.extensionTabexcludes path suffixes such as.pdf, case-insensitively and regardless of the query string. The leading dot is optional in the list.
The starting URL is always fetched and appears first in the result. The filters apply to discovered links and redirect targets. The result lists links discovered, even if they were not fetched due to a limit or if fetching them failed. It is not a list of verified working URLs.
Use grabUrlsNoRecursive($url, $exclude, $extensionTab) to list links from just the starting document, without following them. Both filters are optional. The bundle still follows allowed redirects to reach that document.
Extract assets from one page
$assets = $grabber->grabAssets('https://docs.example.com/guide/');
The result is a flat list of unique URLs, ordered by stylesheets, images, then scripts. Use grabCss(), grabImg() or grabJs() on their own if you need only one type. They read link[rel][href] with a stylesheet relationship, img[src] and script[src], respectively. External asset URLs are included, but no assets are downloaded. Inline content, srcset and links generated by JavaScript are not read.
Links and asset URLs are resolved relative to the final page URL. If the first <base href> points to a valid HTTP or HTTPS URL, that URL is used as the base instead. Fragments are stripped, empty and fragment-only references are ignored, and query strings are retained. /guide and /guide/ remain distinct. Use ASCII hostnames, including Punycode for internationalized domain names. References and normalized URLs longer than 8,192 bytes are rejected.
Handle errors
An invalid starting URL or exclusion URL raises \InvalidArgumentException. If fetching the starting document fails, the service throws Nzo\GrabberBundle\Exception\CrawlException. That covers transport failures, non-success HTTP statuses, refused redirects and responses that exceed the size limit or declare a non-HTML content type. HTML and XHTML are parsed as HTML using the charset declared in Content-Type when provided. Responses without a Content-Type are treated as HTML. During a recursive crawl, failures on later pages are skipped and the crawler continues with the remaining pages.
Configuration reference
# config/packages/nzo_grabber.yaml nzo_grabber: max_depth: 2 # The starting page is at depth 0. Links found at the last depth are reported but not fetched. max_pages: 25 # Maximum page requests the crawler starts, including redirects and failed attempts. timeout: 5.0 # Idle timeout and maximum cURL transfer time per request, in seconds (minimum: 0.001). max_response_bytes: 2097152 # Maximum response body size per page, in bytes. Oversized pages are discarded. same_origin: true # Restrict page links and redirects to the starting scheme, host and port. max_links: 1000 # Maximum unique URLs returned per call, including the start in link results.
The crawl stops once it reaches the result limit. The request budget counts requests started by the crawler, not lower-level connection attempts. The timeout does not include the private-network guard's DNS lookup and is not a deadline for the whole crawl. A page is discarded if its declared Content-Length exceeds the limit or if reading the body reaches a chunk that would exceed it. This limits the body sent to the parser, not total network traffic or parsing memory.
Security
- Only absolute HTTP and HTTPS URLs without embedded credentials are requested. Each redirect is checked before it is followed; it consumes one request from the same budget.
- The bundle uses a dedicated Symfony
CurlHttpClientwrapped inNoPrivateNetworkHttpClientto block private and reserved network destinations, including loopback. This transport bypasses HTTP proxies, disables HTTP/2 server push and does not inherit application-wide HttpClient credentials or headers. - Prefer application-owned starting URLs. If callers choose URLs, enforce a destination allowlist and outbound network restrictions in addition to these checks. Same-origin crawling still permits requests to paths on that origin. Do not expose the crawler as an unrestricted public endpoint.
- When constructing
Grabbermanually or replacingnzo_grabber.http_client, you are responsible for the transport's network policy and ensuring it honors the crawler's request options.
License
This bundle is under the MIT license. See the LICENSE file.