调用未定义的方法 Goutte\Client::setClient()

Call to undefined method Goutte\Client::setClient()

我被这个错误困住了... 但客户端已定义。

我的代码是这样的

use Goutte\Client;
use Illuminate\Http\Request;
use GuzzleHttp\Client as GuzzleClient;

class WebScrapingController extends Controller
{
    public function doWebScraping() 
    {
        $goutteClient = new Client();
        $guzzleClient = new GuzzleClient(array(
            'timeout' => 60,
            'verify' => false
        ));
        $goutteClient->setClient($guzzleClient);
        $crawler = $goutteClient->request('GET', 'https://duckduckgo.com/html/?q=Laravel');
        $crawler->filter('.result__title .result__a')->each(function ($node) {
            dump($node->text());
        });
    }
}

我认为这一行有误

$goutteClient->setClient($guzzleClient);

goutte: "^4.0"
guzzle: "7.0"
Laravel Framework: "6.20.4"


这个答案是关于创建 Goutte 客户端的实例,一个简单的 PHP Web Scraper


版本 >= 4.0.0

直接在 Goutte 客户端实例中传递 HttpClient(guzzle httpclient 或 symfony httpclient)实例。

use Goutte\Client;
use Symfony\Component\HttpClient\HttpClient;// or use GuzzleHttp\Client as GuzzleClient;

$client = new Client(HttpClient::create(['timeout' => 60]));
// or 
// $guzzleClient = new GuzzleClient(['timeout' => 60, 'verify' => false]); // pass this to Goutte Client

版本 <= 4.0.0(即从 0.1.0 到 3.3.1)

use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;

$goutteClient = new Client();
$guzzleClient = new GuzzleClient(['timeout' => 60]);
$goutteClient->setClient($guzzleClient);