spatie / crawler

An easy to use, powerful crawler implemented in PHP. Can execute Javascript.
https://freek.dev/308-building-a-crawler-in-php
MIT License
2.53k stars 358 forks source link

Help extend the class #203

Closed atomixstar closed 5 years ago

atomixstar commented 5 years ago

Hello, I am trying to setup the crawler on laravel but I cannot extend the abstract class, I am sure I am missing something.

App\Abstracts\CrawlLogger.php

`<?php namespace App\Abstracts; use Psr\Http\Message\UriInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use GuzzleHttp\Exception\RequestException;

class CrawlLogger extends CrawlObserver { /* @var string / protected $observerId; public function __construct(string $observerId = '') { if ($observerId !== '') { $observerId .= ' - '; } $this->observerId = $observerId; } /**

App\Abstracts\CrawlObserver.php

`<?php

namespace App\Abstracts;

use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; use App\Abstracts\CrawlLogger;

abstract class CrawlObserver { /**

Controller:

`.....

use stdClass; use GuzzleHttp\Psr7\Uri; use Spatie\Crawler\Crawler; use Spatie\Crawler\CrawlProfile; use Psr\Http\Message\UriInterface; use Spatie\Browsershot\Browsershot; use Spatie\Crawler\CrawlSubdomains; use Spatie\Crawler\CrawlInternalUrls; use Spatie\Crawler\Exception\InvalidCrawlRequestHandler;

use App\Abstracts\CrawlLogger;

class TestController extends Controller {

public function index() {

    Crawler::create()
    ->setCrawlObserver(new CrawlLogger())
    ->startCrawling('https://github.com');

}

}`

I am getting this error: Type error: Argument 1 passed to Spatie\Crawler\Crawler::setCrawlObserver() must be an instance of Spatie\Crawler\CrawlObserver, instance of App\Abstracts\CrawlLogger given, called in

I am trying to learn how is working and I have search around the web related to extend of classes, but still cannot understand what I am doing wrong.

Thank you :)

danhunsaker commented 5 years ago

One of these classes needs to implements Spatie\Crawler\CrawlObserver - either class in App\Abstracts will do.

atomixstar commented 5 years ago

But in vendor/spatie/crawler/src/CrawlObserver.php is an interface not an abstract class

danhunsaker commented 5 years ago

Right. That's why you implements instead of extends it. That's the purpose of interfaces - for classes to implement.

d9media-git commented 5 years ago

I am sorry but I think that the documentation on the product page is simply misleading because it tells to extend rather than implements. I found this script very hard to implement unfortunately due to poor documentation.

At the moment it reads:

use Spatie\Crawler\Crawler;

Crawler::create()
    ->setCrawlObserver(<class that extends \Spatie\Crawler\CrawlObserver>)
    ->startCrawling($url);
brendt commented 5 years ago

Feel free to submit a PR to fix that typo.

Googolaire commented 4 years ago

I agree the documentation is poor