So i have this url $url = "localhost:8000/vehicles" that i want ot fetch through a cron job but the page returns html so i wanna use symfony dom crawler to get all the vehicles instead of regex
At the top of my file i added
use SymfonyComponentDomCrawlerCrawler;
To create a new instance i tried:
$crawler = new Crawler($data);
and i tried
$crawler = Crawler::create($data);
but that gives me an error, also tried adding
SymfonyComponentDomCrawlerCrawler::class,
to the service provider but when i execute the command:
composer dump-autoload
it gives me the following error
In Crawler.php line 66:
SymfonyComponentDomCrawlerCrawler::__construct(): Argument #1 ($node) must be of type DOMNodeList|DOMNode|array|string|null, IlluminateFoundationApplication given, called in C:xampphtdocsDrostMachinehandelDrostMachinehandelvendorlaravelfr
ameworksrcIlluminateFoundationProviderRepository.php on line 208
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
I have no idea how to fix this.
The fucntion for fetching the url is below:
public function handle()
{
$url = SettingsController::fetchSetting("fetch:vehicles");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
$vehicles = $this->scrapeVehicles($data, $url);
Log::debug($vehicles);
curl_close($ch);
}
private function scrapeVehicles(string $data, string $url): array
{
$crawler = Crawler::create($data);
$vehicles = $crawler->filter(".vehicleTile");
return $vehicles;
}
Contents of $data:
2
Answers
Since not been tested, I’m not sure.
Make sure you installed correct package
To initiate, use the full path. (since it’s not Laravel way(package))
composer require –dev "symfony/dom-crawler":"^6.3.x-dev"
Sample crawler method
namespace AppHttpControllers;
Parse content and store