【问题标题】:Laravel Scrape External Website Data using Goutte ErrorLaravel 使用 Goutte 错误抓取外部网站数据
【发布时间】:2020-06-07 17:46:50
【问题描述】:

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"

控制器:

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 '';
}

错误:

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

它总是在到达$crawler = $client->request 时中断。我一生都无法弄清楚发生了什么。我尝试了不同的软件包,删除它,再次添加它。感谢您的帮助!

【问题讨论】:

    标签: php laravel goutte


    【解决方案1】:

    我的猜测是您的机器上安装了两个版本的 PHP。您的命令行版本(运行 composer)似乎 >= PHP 7.2,但您的 Web 服务器运行的 PHP 版本是

    symfony/dom-crawler 中,5.0.0 版更新了Crawler::createSubCrawler() 方法,使其具有object 返回类型提示。它还将 PHP 版本依赖更新为^7.2.5,因此除非运行 Composer 的 PHP 版本 >= ^7.2.5,否则不会安装此版本。

    但是,Web 服务器运行的 PHP 版本似乎无法理解 object 类型提示,因此它正在寻找一个名为 Symfony\Component\DomCrawler\object 的实际类。由于 PHP 不理解类型提示,这意味着运行代码的版本是

    您需要确保 Web 服务器用于运行代码的 PHP 版本与运行 composer 安装依赖项的 PHP 版本相同。

    【讨论】:

    • 谢谢!我正在运行 XAMPP,我刚刚下载了它的更高版本,现在它运行顺利!
    猜你喜欢
    • 2014-08-04
    • 2021-04-28
    • 2018-05-11
    • 1970-01-01
    • 2018-10-23
    • 2018-10-17
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多