Soap 用大口喝肥皂
我喜欢我刚刚发现的Guzzle框架。我使用它来使用不同的响应结构跨多个API聚合数据。它与JSON和XML一起工作,但我需要使用的一个服务使用SOAP。有没有一种内置的方法可以使用Guzzle来使用SOAP服务?IMHO Guzzle没有完全的SOAP支持,只能处理HTTP请求。 src/Guzzle/Http/ClientInterface.php行:76Soap 用大口喝肥皂,soap,guzzle,Soap,Guzzle,我喜欢我刚刚发现的Guzzle框架。我使用它来使用不同的响应结构跨多个API聚合数据。它与JSON和XML一起工作,但我需要使用的一个服务使用SOAP。有没有一种内置的方法可以使用Guzzle来使用SOAP服务?IMHO Guzzle没有完全的SOAP支持,只能处理HTTP请求。 src/Guzzle/Http/ClientInterface.php行:76 public function createRequest(
public function createRequest(
$method = RequestInterface::GET,
$uri = null,
$headers = null,
$body = null,
array $options = array()
);
即使将SOAP服务器配置为在端口80上协商,我认为php SoapClient在这里是更合适的解决方案,因为它支持WSDL旧主题,但当我搜索相同的答案时,它似乎正在做这项工作 您可以让Guzzle发送SOAP请求。 请注意,SOAP始终具有信封、标题和正文
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<NormalXmlGoesHere>
<Data>Test</Data>
</NormalXmlGoesHere>
</soapenv:Body>
Guzzle HTTP可用于SOAP请求,其工作原理与魅力类似: 下面是我实施的方式 创建变量:
public function __construct(Request $request) {
$this->request = $request;
$this->openSoapEnvelope = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">';
$this->soapHeader = '<soap:Header>
<tem:Authenticate>
<-- any header data goes here-->
</tem:Authenticate>
</soap:Header>';
$this->closeSoapEnvelope = '</soap:Envelope>';
}
定义一个body&调用generateSoapRequest方法。
e、 g:
$soapBody=”
';
$xmlRequest=$this->generateSoapRequest($soapBody);
$client=新客户端();
$options=[
“body”=>$xmlRequest,
“标题”=>[
“内容类型”=>“文本/xml”,
“接受”=>“*/*”,
接受编码“=>”gzip,放气
]
];
$res=$client->request(
"岗位",,
'http://your-soap-endpoint-url',
美元期权
);
打印($res->getBody());
我还想了解有关此主题的更多信息。Guzzle文档没有提到任何关于.wsdl文件或SOAP的内容。我认为Guzzle现在是版本6。你知道他们是否做了任何改变来支持SOAP吗?这个库使用标准的php SOAP库对eahc请求进行了额外的API调用,你知道为什么吗?我们可以去掉这个额外的API调用吗?
$soapHeader = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>';
$soapFooter = '</soapenv:Body></soapenv:Envelope>';
$xmlRequest = $soapheader . $cleanXml . $soapFooter; // Full SOAP Request
$client = new Client([
'base_url' => 'https://api.xyz.com',
]);
try {
$response = $client->post(
'/DataServiceEndpoint.svc',
[
'body' => $xmlRequest,
'headers' => [
'Content-Type' => 'text/xml',
'SOAPAction' => 'https://api.xyz.com/DataService/PostData' // SOAP Method to post to
]
]
);
var_dump($response);
} catch (\Exception $e) {
echo 'Exception:' . $e->getMessage();
}
if ($response->getStatusCode() === 200) {
// Success!
$xmlResponse = simplexml_load_string($response->getBody()); // Convert response into object for easier parsing
} else {
echo 'Response Failure !!!';
}
public function __construct(Request $request) {
$this->request = $request;
$this->openSoapEnvelope = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">';
$this->soapHeader = '<soap:Header>
<tem:Authenticate>
<-- any header data goes here-->
</tem:Authenticate>
</soap:Header>';
$this->closeSoapEnvelope = '</soap:Envelope>';
}
public function generateSoapRequest($soapBody){
return $this->openSoapEnvelope . $this->soapHeader . $soapBody . $this->closeSoapEnvelope;
}
$soapBody = '<soap:Body>
<tem:GetSomeDetails/>
</soap:Body>';
$xmlRequest = $this->generateSoapRequest($soapBody);
$client = new Client();
$options = [
'body' => $xmlRequest,
'headers' => [
"Content-Type" => "text/xml",
"accept" => "*/*",
"accept-encoding" => "gzip, deflate"
]
];
$res = $client->request(
'POST',
'http://your-soap-endpoint-url',
$options
);
print_r($res->getBody());