【发布时间】:2018-09-21 10:18:29
【问题描述】:
是否仍然可以在 Guzzle 6 中设置原始请求正文(在其他地方正确创建的 XML 数据)?
【问题讨论】:
是否仍然可以在 Guzzle 6 中设置原始请求正文(在其他地方正确创建的 XML 数据)?
【问题讨论】:
是的,Guzzle 6 可以:
...
$client = new \GuzzleHttp\Client();
$rawContent = <<<XML
<?xml version='1.0' encoding='us-ascii'?>
<slideshow>
<slide type="all">
<title>Overview</title>
</slide>
</slideshow>
XML;
$request = new \GuzzleHttp\Psr7\Request(
'post',
'https://httpbin.org/post',
['content-type' => 'text/xml'],
$rawContent
);
$responce = $client->send($request);
print_r((string) $responce->getBody());
如果您需要发送原始表单数据,您可以使用:
...
$request = new \GuzzleHttp\Psr7\Request(
'post',
'https://httpbin.org/post',
['content-type' => 'application/x-www-form-urlencoded'],
'a=1&b=2'
);
...
【讨论】:
示例 1:
// Provide the body as a string.
$r = $client->request('POST', 'http://httpbin.org/post', [
'body' => 'raw data'
]);
示例 2:
// Provide an fopen resource.
$body = fopen('/path/to/file', 'r');
$r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);
【讨论】: