为soap请求构建XML

时间:2014-04-11 07:07:11

标签: php xml soap

您将如何手动构建soap请求?我没有为php寻找肥皂客户端,所以我试图手动构建请求。以下是请求的示例:

Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <EWalletLoginCmd xmlns="xxxxxxx">
      <ewalleTID>string</ewalleTID>
      <PIN>string</PIN>
    </EWalletLoginCmd>
  </soap12:Body>
</soap12:Envelope>

我发了一个函数来发送请求,如:

private function send($url, $xml) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/soap+xml; charset=utf-8"));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => $xml));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

    return $result;
}

但我对如何实际构建XML感到迷茫,我开始:

    $xml = new DOMDocument('1.0', 'UTF-8');

    $body = $xml->createElement('body');
    $xml->appendChild($body);

    return $xml->saveXML();

但是你如何指定<soap12:?我是php的新手soap和xml。

1 个答案:

答案 0 :(得分:0)

PHP内置了SoapClient类用于使用SOAP。 如果您的SOAP服务器有wsdl尝试

$wsdl= 'location of your servers wsdl';
$options = array('soap_version'=>SOAP_1_2, 'trace'=>true);
$client = new SoapClient($wsdl, $options);

然后你可以在你的客户端上调用任何方法,如

$results = $client->EWalletLoginCmd(array('ewalleTID'=>'...', 'PIN'=>'...'));