【发布时间】:2020-09-23 12:08:16
【问题描述】:
每当我使用 Postman 发出肥皂帖子请求时,我都会返回所需的数据。使用 Guzzle,不会返回任何数据。我是 SOAP 新手,并使用在线资源继续学习。
为了便于阅读,我省略了变量。
$xml = (
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="<ACTION-URL>">
<soapenv:Body>
<app:' . $functionName . '>
<request>'
. $requestBody .
'<authentication>
<username>'. $authObject->username .'</username>
<password>'. $authObject->password .'</password>
<user_id>'. $authObject->userId .'</user_id>
<dealer_id>'. $authObject->clientBranchId .'</dealer_id>
</authentication>
</request>
</app:' . $functionName . '>
</soapenv:Body>
</soapenv:Envelope>'
);
private function makeSOAPRequest($xml)
{
$client = new \GuzzleHttp\Client();
$options = [
'headers' => [
'Content-Type' => 'text/xml; charset=utf-8'
],
'body' => $xml,
'Authenticate' => [env('USERNAME'), env('PASSWORD')]
];
$url = env('ROSETTA_API');
$promise = $client->requestAsync('POST', $url, $options);
$response = $promise->wait();
$xml = simplexml_load_string($response->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
if ($xml) {
$json = json_encode($xml);
return json_decode($json, true);
}
return false;
}
拨打makeSOAPRequest(..),我会回复false。在 Postman 中使用相同的 xml 数据,返回数据。请求标头中有什么我遗漏的吗?
编辑以使用 cURL
// Copied from Postman:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://<URL>",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $xml,
CURLOPT_HTTPHEADER => array(
"Cookie: PHPSESSID=6419f1d96025a4a2c4d454de33fc6820"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response; // xml data in string format.
cUrl 确实有效,但 Guzzle 无效。如何返回为json?当我使用simplexml_load_string 时,它是空的。
【问题讨论】:
-
如果启用调试标志会发生什么? guzzle发送的请求是否与邮递员相同? guzzle 收到回复了吗?
-
嗨。当 debug 设置为 true 时,我看到了一堆与主机服务器及其 ssl 信息相关的东西。没有什么不寻常的,我看到:“上传完全发送:648 个字节中的 648 个”。有什么具体要寻找的吗?我将尝试仅使用 cURL,然后再依赖。谢谢
-
正确完成后,无需将 xml 转换为 php 数据结构。从xml到php的转换是由soap客户端完成的。在处理肥皂请求和响应时,Guzzle 不会是我的首选。原生 PHP 客户端做得更好。