在guzzle 6中设置原始POST请求正文

时间:2018-04-11 13:35:50

标签: php guzzle6

是否仍然可以在Guzzle 6中设置原始请求正文(在其他地方正确创建的XML数据)?

这似乎不再适用:Specify raw body of a POST request with Guzzle

2 个答案:

答案 0 :(得分:0)

是的,可以使用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 :(得分:0)

Guzzle 6 docs

示例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]);