【问题标题】:Laravel Guzzle GET requestLaravel Guzzle GET 请求
【发布时间】:2018-12-22 05:38:33
【问题描述】:
    $client = new Client(['base_uri' => 'http://api.tvmaze.com/']);

    $res = $client->request('GET', '/schedule?country=US&date=2014-12-01');

    return $res;

返回此错误:

"Class 'Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory' not found"

我尝试在我的 composer.json 文件中包含 "symfony/psr-http-message-bridge": "0.2"

【问题讨论】:

    标签: php laravel guzzle


    【解决方案1】:

    先删除 guzzle 包:composer remove guzzlehttp/guzzle

    然后做:

    composer dump-autoload
    

    终于重新安装了:

    composer require guzzlehttp/guzzle

    还要确保您使用的是 guzzle 命名空间:

    use GuzzleHttp\Client;
    

    【讨论】:

      【解决方案2】:

      您正在实例化一个客户端,但您似乎没有明确说明要实例化的类。试试这个:

      $client = new \GuzzleHttp\Client(['base_uri' => 'http://api.tvmaze.com/']);
      
      $res = $client->request('GET', '/schedule?country=US&date=2014-12-01');
      
      return $res;
      

      【讨论】:

        【解决方案3】:

        这是一个有点老的问题。但是,由于答案无法为我解决,这是使用 Guzzle Http 时要尝试的最后一件事。

        根据 Laravel 文档https://laravel.com/docs/5.3/requests#psr7-requests

        PSR-7 标准指定了 HTTP 消息的接口,包括请求和响应。如果您想获取 PSR-7 请求的实例而不是 Laravel 请求,您首先需要安装一些库。 Laravel 使用 Symfony HTTP 消息桥组件将典型的 Laravel 请求和响应转换为 PSR-7 兼容的实现:

        composer require symfony/psr-http-message-bridge
        composer require zendframework/zend-diactoros
        

        这解决了我的问题。

        编辑------

        正如 cmets 中所建议的,包 zendframework/zend-diactoros 已被废弃。那么最好使用替代品。

        Symfony PSR7 Bridge推荐:

        composer require nyholm/psr7
        

        Laminas Diactoros Github Package:

        composer require laminas/laminas-diactoros
        

        【讨论】:

        • 对于那些使用"guzzlehttp/guzzle": "^6.5" 的人,似乎:包zendframework/zend-diactoros 已被废弃,您应该避免使用它。请改用laminas/laminas-diactoros
        • 你也可以使用 composer require nyholm/psr7 代替 Symfony 建议的 zendframework/zend-diactoros。它对我有用!
        • 全新安装 laravel 8 并存在问题。安装了“psr-http-message-bridge”和“nyholm/psr7”。作曲家 dumpautoload 调用。但仍然无法工作“类“Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory”未找到“
        【解决方案4】:

        您需要使用 composer 安装以下额外的 symfony 组件。

        composer require symfony/psr-http-message-bridge
        composer require nyholm/psr7
        

        https://symfony.com/doc/..

        【讨论】:

          【解决方案5】:

          你需要得到答案的正文:

          $client = new \GuzzleHttp\Client(['base_uri' => 'http://api.tvmaze.com/']);
          
          $res = $client->request('GET', '/schedule?country=US&date=2014-12-01');
          
          return $res->getBody();
          

          【讨论】:

          • 就这么简单。谢谢。
          猜你喜欢
          • 1970-01-01
          • 2023-03-04
          • 2017-11-15
          • 2017-04-10
          • 1970-01-01
          • 2020-07-14
          • 1970-01-01
          • 2019-12-17
          • 2017-04-16
          相关资源
          最近更新 更多