1. Go to this page and download the library: Download ioodev/elephscraper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ioodev / elephscraper example snippets
oodev\Elephscraper\ElephScraper;
$scraper = new ElephScraper('https://example.com');
if (!$scraper->isValid()) {
// Request failed (timeout, DNS error, 404, etc.) — will not throw a fatal error.
die('Failed to load page: ' . $scraper->getError()?->getMessage());
}
echo $scraper->title(); // "Welcome to Example.com"
echo $scraper->description(); // "Example site for testing"
print_r($scraper->h1()); // ["Main Title", "News"]
print_r($scraper->openGraph());
$scraper = new ElephScraper($url);
if (!$scraper->isValid()) {
echo 'Error: ' . $scraper->getError()->getMessage();
// continue to the next URL, etc.
}
$scraper = new ElephScraper('https://example.com', [
'client' => $myMockedGuzzleClient,
]);
$scraper->title(); // ?string
$scraper->description(); // ?string
$scraper->keywords(); // ?string[] — comma-split result, already trimmed
$scraper->keywordString(); // ?string — raw "content" attribute
$scraper->charset(); // ?string
$scraper->canonical(); // ?string
$scraper->contentType(); // ?string — from meta http-equiv="Content-Type"
$scraper->author(); // ?string
$scraper->csrfToken(); // ?string — checks <meta name="csrf-token">, falls back to <input name="csrf-token">
$scraper->image(); // ?string — shortcut for og:image
$scraper->viewport(); // ?string[] — comma-split result from meta viewport
$scraper->viewportString(); // ?string
$scraper->openGraph(); // array<string,?string> — all common og: properties
$scraper->openGraph('og:title'); // ?string — a specific property
$scraper->twitterCard(); // array<string,?string> — all common twitter: tags
$scraper->twitterCard('twitter:title'); // ?string — a specific property
$scraper->isValid(); // bool — whether the document loaded successfully
$scraper->getError(); // ?Throwable — the last exception, if any
$scraper->getHtml(); // ?string — raw HTML
$scraper->getCrawler(); // ?Symfony\Component\DomCrawler\Crawler
$scraper->getUrl(); // ?string — source URL (or base URL from loadHtml())