使用 guzzle 6 发送 (POST) xml 的正确方法
Proper way to send (POST) xml with guzzle 6
我想用 guzzle 发送 xml 文件来执行 post。我没有找到例子。
到目前为止我所做的是:
$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();
无论我尝试什么,我都会返回错误 -1,这意味着 xml 无效。
我发送的 XML 虽然通过了在线验证并且有效 %100
请帮忙。
尝试像这样发布数据:
$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();
经过一些实验,我明白了。这是我的解决方案,以防有人走入死胡同。
$request = new Request(
'POST',
$uri,
['Content-Type' => 'text/xml; charset=UTF8'],
$xml
);
如果您想使用post方法发送xml,这里有一个例子:
$guzzle->post($url, ['body' => $xmlContent]);
这就是我在 Guzzle 6 上的工作方式:
// configure options
$options = [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
'body' => $xml,
];
$response = $client->request('POST', $url, $options);
您可以通过以下方式进行
$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
]);
我发现我还必须 trim 正文 - 正文中有一个前导换行符,Guzzle 根本拒绝发送正文,直到我 trim 处理它。
我想用 guzzle 发送 xml 文件来执行 post。我没有找到例子。
到目前为止我所做的是:
$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();
无论我尝试什么,我都会返回错误 -1,这意味着 xml 无效。 我发送的 XML 虽然通过了在线验证并且有效 %100
请帮忙。
尝试像这样发布数据:
$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();
经过一些实验,我明白了。这是我的解决方案,以防有人走入死胡同。
$request = new Request(
'POST',
$uri,
['Content-Type' => 'text/xml; charset=UTF8'],
$xml
);
如果您想使用post方法发送xml,这里有一个例子:
$guzzle->post($url, ['body' => $xmlContent]);
这就是我在 Guzzle 6 上的工作方式:
// configure options
$options = [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
'body' => $xml,
];
$response = $client->request('POST', $url, $options);
您可以通过以下方式进行
$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
]);
我发现我还必须 trim 正文 - 正文中有一个前导换行符,Guzzle 根本拒绝发送正文,直到我 trim 处理它。