【问题标题】:Setting User Agent header with Goutte/Guzzle? (undefined method error)使用 Goutte/Guzzle 设置用户代理标头? (未定义的方法错误)
【发布时间】:2020-12-27 07:19:12
【问题描述】:

我正在尝试在 Goutte 中设置用户代理,我正在使用 setHeader,但在控制台中出现未定义方法 setHeader 错误。

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

如何在 Goutte 中设置用户代理标头?

public function getGoogleResults(Request $request)
    {
        $terms = 'Nata & Chocolate Gluten Free Bakery';//$request->input('terms');
        $url = 'http://www.google.es/search?'.http_build_query(array('q' => $terms));
        
        $client = new Client;
        $client->setHeader('user-agent', "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36");
        $crawler = $client->request('GET', $url);
}

【问题讨论】:

    标签: javascript php laravel vue.js


    【解决方案1】:

    您可以使用 setServerParameter 设置标头:

    $client->setServerParameter('name', 'value');
    

    如:

    $client->setServerParameter('HTTP_USER_AGENT', 'user agent');
    

    参考:https://github.com/FriendsOfPHP/Goutte/issues/401#issuecomment-591760247

    绝对在 symfony 中,您可以使用 HttpClient Symfony 组件在“new Client();”中提供收缩参数

    你可以在下面的链接中看到它

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 2016-05-16
      • 2020-08-20
      • 2016-06-18
      • 2020-07-12
      • 2014-10-21
      相关资源
      最近更新 更多