Fetches data from a certain website.
Clone or Download the code and install the dependencies using Composer
composer install
Include the autoload file and setup and headers.
use CctvDirect\Scrapper;
include __DIR__ . "/vendor/autoload.php";
header('Content-Type: application/json');The following scrapes data from the file in the bin folder
$results = array();
$scrapper = new Scrapper();
$scrapper->scrape('http://localhost/bin/dahua-analog-hd-cctv-cameras.htm');
// ? localhost demo scrapping
$collection = $scrapper->collection;
$results = array(
'title' => $collection->getTitle(),
'category_slug' => $collection->getCategorySlug(),
'products' => $collection->getProducts(),
// 'collections' => $collection->getCollections(),
);
// ? print results
echo json_encode($results, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);In this example, we get the collections from the file in the bin, then scrap products from the live site.
$collections = $scrapper->collection->getCollections();
foreach ($collections as $collectionUrl) {
$scrapper->scrape($collectionUrl);
$results[$scrapper->collection->getCategorySlug()] = array(
'title' => $scrapper->collection->getTitle(),
'products' => $scrapper->collection->getProducts()
);
}