Guzzle 6 中 GuzzleHttp\Stream\Stream::factory 的等效项是什么?
What's the equivalent of GuzzleHttp\Stream\Stream::factory in Guzzle 6?
我有以下代码需要迁移到 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 Fatal error: Class 'GuzzleHttp\Stream\Stream' not found
代码使用的是 Guzzle 6 中的 static factory method which seems to be non-existing。
在 Guzzle 6 中使用的等效静态工厂方法是什么?
没有必要再使用静态工厂方法了。编码后的 JSON 代码可以直接传递给参数。检查下面的示例。
Guzzle 5
$response = new \GuzzleHttp\Message\Response(
b200,
['Content-Type' = 'application/json'],
\GuzzleHttp\Streams\Stream::factory(json_encode(['foo' => 'bar' ])
);
来源:aerisweather/GuzzleHttpMock
(README.md
).
Guzzle 6
$response = new \GuzzleHttp\Psr7\Response(
200,
['Content-Type' = 'application/json'],
json_encode(['foo' => 'bar' ]
);
来源:systemhaus/GuzzleHttpMock
(README.md
)
另请检查 this commit (36a7ef
) 以获得更详细的示例。
您可以使用GuzzleHttp\Psr7\stream_for()
函数。
我有以下代码需要迁移到 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 Fatal error: Class 'GuzzleHttp\Stream\Stream' not found
代码使用的是 Guzzle 6 中的 static factory method which seems to be non-existing。
在 Guzzle 6 中使用的等效静态工厂方法是什么?
没有必要再使用静态工厂方法了。编码后的 JSON 代码可以直接传递给参数。检查下面的示例。
Guzzle 5
$response = new \GuzzleHttp\Message\Response(
b200,
['Content-Type' = 'application/json'],
\GuzzleHttp\Streams\Stream::factory(json_encode(['foo' => 'bar' ])
);
来源:aerisweather/GuzzleHttpMock
(README.md
).
Guzzle 6
$response = new \GuzzleHttp\Psr7\Response(
200,
['Content-Type' = 'application/json'],
json_encode(['foo' => 'bar' ]
);
来源:systemhaus/GuzzleHttpMock
(README.md
)
另请检查 this commit (36a7ef
) 以获得更详细的示例。
您可以使用GuzzleHttp\Psr7\stream_for()
函数。