【问题标题】:What's the equivalent of GuzzleHttp\Stream\Stream::factory in Guzzle 6?Guzzle 6 中 GuzzleHttp\Stream\Stream::factory 的等价物是什么?
【发布时间】:2018-06-30 18:16:20
【问题描述】:

我有以下代码需要迁移到 Guzzle 6:

use GuzzleHttp\Stream\Stream;
use Drupal\Testing\PHPUnit\DrupalTestCase;
class OpsAbstractTest extends DrupalTestCase {

  public function responseMock($value, $code = 200) {
    $body = Stream::factory(json_encode($value));
    return new Response($code, ['Content-type' => 'application/json'], $body);
  }

}

升级到 Guzzle 6 后代码失败:

PHP 致命错误:找不到类“GuzzleHttp\Stream\Stream”

代码使用static factory method,在 Guzzle 6 中似乎是 non-existing

在 Guzzle 6 中使用的等效静态工厂方法是什么?

【问题讨论】:

    标签: php-7 guzzle6 guzzle


    【解决方案1】:

    不再需要使用静态工厂方法。编码后的 JSON 代码可以直接传递到参数中。检查以下示例。

    狂饮 5

    $response = new \GuzzleHttp\Message\Response(
        b200,
        ['Content-Type' = 'application/json'],
        \GuzzleHttp\Streams\Stream::factory(json_encode(['foo' => 'bar' ])
    );
    

    来源:aerisweather/GuzzleHttpMock (README.md)。

    狂饮 6

    $response = new \GuzzleHttp\Psr7\Response(
        200,
        ['Content-Type' = 'application/json'],
        json_encode(['foo' => 'bar' ]
    );
    

    来源:systemhaus/GuzzleHttpMock (README.md)


    查看this commit (36a7ef) 以获取更详细的示例。

    【讨论】:

      【解决方案2】:

      您可以使用GuzzleHttp\Psr7\stream_for() 函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-08
        • 1970-01-01
        相关资源
        最近更新 更多