PHP code example of webscraping-ai / webscraping-ai-php
1. Go to this page and download the library: Download webscraping-ai/webscraping-ai-php 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/ */
webscraping-ai / webscraping-ai-php example snippets
use WebScrapingAI\Client;
$client = new Client(apiKey: getenv('WEBSCRAPING_AI_KEY'));
// Full HTML
$html = $client->html(url: 'https://example.com');
// Visible text
$text = $client->text(url: 'https://example.com');
// HTML for one selector
$h1 = $client->selected(url: 'https://example.com', selector: 'h1');
// HTML for multiple selectors (returns array)
$chunks = $client->selectedMultiple(
url: 'https://example.com',
selectors: ['h1', 'p', 'a'],
);
// LLM question over a page
$answer = $client->question(
url: 'https://example.com',
question: 'What is the main topic?',
);
// LLM-extracted structured fields
$fields = $client->fields(
url: 'https://example.com',
fields: [
'title' => 'Main product title',
'price' => 'Current price',
],
);
// Structured data for a page on a supported site (YouTube, TikTok, X, ...)
$video = $client->data(url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ');
// Account quota
$account = $client->account();
$serp = $client->serp(q: 'coffee machines', gl: 'us', hl: 'en', page: 1);
foreach ($serp['organic_results'] as $result) {
printf("%d. %s — %s\n", $result['position'], $result['title'], $result['link']);
}
$nextPage = $serp['pagination']['next'] ?? null; // absent on the last page
use WebScrapingAI\Exception\BadRequestException;
$result = $client->data(url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', transcript: true);
if ($result['parse_status'] === 'ok') {
echo $result['request_parameters']['provider'], ': ', $result['data']['title'], "\n";
}
try {
$client->data(url: 'https://example.com/');
} catch (BadRequestException $e) {
echo $e->getMessage(); // "Unsupported URL for /data. ..." — lists what is supported
}
use GuzzleHttp\Client as Guzzle;
use Nyholm\Psr7\Factory\Psr17Factory;
use WebScrapingAI\Client;
$factory = new Psr17Factory();
$client = new Client(
apiKey: getenv('WEBSCRAPING_AI_KEY'),
httpClient: new Guzzle(['timeout' => 30.0]),
requestFactory: $factory,
uriFactory: $factory,
);