php 使用 guzzle 6 发送 (POST) xml 的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34726530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way to send (POST) xml with guzzle 6
提问by user3485417
I want to perform a post with guzzle sending an xml file. I did not find an example.
我想用 guzzle 发送一个 xml 文件来执行一个帖子。我没有找到一个例子。
What I 've done so far is :
到目前为止我所做的是:
$xml2=simplexml_load_string($xml) or die("Error: Cannot create object");
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
//
$request = new Request('POST', $uri, [ 'body'=>$xml]);
$response = $client->send($request);
//
//$code = $response->getStatusCode(); // 200
//$reason = $response->getReasonPhrase(); // OK
//
echo $response->getBody();
No matter what I try I get back error -1 which means that xml is not valid. XML that I send passes online validation though and is valid %100
无论我尝试什么,我都会返回错误 -1,这意味着 xml 无效。我发送的 XML 虽然通过了在线验证并且是有效的 %100
Please help.
请帮忙。
回答by user3485417
After some experiments, I have figured it out. Here is my solution in case someone reaches a dead end.
经过一些实验,我已经弄清楚了。这是我的解决方案,以防有人走到死胡同。
$request = new Request(
'POST',
$uri,
['Content-Type' => 'text/xml; charset=UTF8'],
$xml
);
回答by Abz
This is what worked for me on Guzzle 6:
这就是在 Guzzle 6 上对我有用的方法:
// configure options
$options = [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
'body' => $xml,
];
$response = $client->request('POST', $url, $options);
回答by Shaolin
If you want to send xml using the post method, here is an example:
如果你想使用 post 方法发送 xml,这里是一个例子:
$guzzle->post($url, ['body' => $xmlContent]);
回答by user3785966
You can do that in a below way
你可以通过以下方式做到这一点
$xml_body = 'Your xml body';
$request_uri = 'your uri'
$client = new Client();
$response = $client->request('POST', $request_uri, [
'headers' => [
'Content-Type' => 'text/xml'
],
'body' => $xml_body
]);
回答by Borche Bojcheski
Try posting the data like:
尝试发布如下数据:
$xml2=simplexml_load_string($xml) or die("Error: Cannot create object");
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
//
$request = new Request('POST', $uri, [
'form_params' => [
'xml' => $xml,
]
]);
$response = $client->send($request);
//$code = $response->getStatusCode(); // 200
//$reason = $response->getReasonPhrase(); // OK
echo $response->getBody();