PHP code example of skyblack / diz-scraping

1. Go to this page and download the library: Download skyblack/diz-scraping 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/ */

    

skyblack / diz-scraping example snippets


class MyCrawler extends Crawler
{
    public function __construct()
	{
		parent::__construct('sample.com', 'my'); // https://my.sample.com
		$this->addJSONPipe();
		$this->addCallbackPipe([$this, 'checkResponse']);
	}
	
	// Add a callback pipe to check the status of the response from the server
	public function checkResponse(array $result): array
	{
		if (($result['status'] ?? null) !== 'ok') {
		   throw new \Exception($result['message'] ?? null); 
		}
		return $result['content'];
	}
	
	public function getItems(string $category): array
	{
	    return $this->get('items', ['category' => $category]);
	}
	
	// Listen the download event to prevent downloading erroneous files
	protected function onDownloadEvent(DownloadEvent $event): void
	{
		$response = $event->getResponse();

		$status_code = $response->getStatusCode();
		$size = $response->getDownloadSize();
		$destination = $event->getDestination();

		if ($status_code != 200 || $size == 0) {
			unlink($destination);
			if ($status_code == 404) {
				throw new \Exception('File not found.');
			} else {
			    throw new \Exception('File download error.');
			}
		}
    }
}