phplaravelgoutte

Laravel Scrape External Website Data using Goutte Error


https://github.com/FriendsOfPHP/Goutte

composer.json:

"php": "^7.1.3",
        "fabpot/goutte": "^4.0",
        "fideloper/proxy": "^4.0",
        "guzzlehttp/guzzle": "^6.5",
        "laravel/framework": "^6.2",
        "laravel/passport": "^8.4",
        "laravel/tinker": "^2.0",
        "laravel/ui": "^1.2",
        "symfony/translation": "4.3.8"

Controller:

use Illuminate\Http\Request;
use App\Http\Requests;
use Goutte\Client;
use Symfony\Component\HttpClient\HttpClient;



class getStuff extends Controller
{
    public function get(Request $request) {

      $client = new Client();
      $crawler = $client->request('GET', 'https://www.symfony.com/blog/');

      return '';
}

Error:

Symfony\Component\Debug\Exception\FatalThrowableError
Return value of Symfony\Component\DomCrawler\Crawler::createSubCrawler() must be an instance of Symfony\Component\DomCrawler\object, instance of Symfony\Component\DomCrawler\Crawler returned

It always breaks when it reached $crawler = $client->request. I cannot for the life of me figure out what is going on. I have tried different packages, removing it, adding it again. Your assistance it appreciated!


Solution

  • My guess is that you have two versions of PHP installed on your machine. It seems like your command line version (that runs composer) is >= PHP 7.2, but the PHP version run by your web server is < PHP 7.2.

    In symfony/dom-crawler, version 5.0.0 updated the Crawler::createSubCrawler() method to have an object return type hint. It also updated the PHP version dependency to ^7.2.5, so this version would not get installed unless the version of PHP running composer is >= ^7.2.5.

    However, the version of PHP run by the web server does not appear to understand the object type hint, so it is looking for an actual class named Symfony\Component\DomCrawler\object. Since PHP doesn't understand the type hint, that means the version running the code is < 7.2.0.

    You need to make sure the version of PHP that is used by the web server to run the code is the same version of PHP that runs composer to install the dependencies.