从PHP发送SOAP请求

时间:2017-08-08 11:19:19

标签: php curl soap

我正在尝试从PHP脚本向我的服务器发送SOAP请求。我们供应商提供的soap模板如下:

 <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
  xmlns:q0="http://nsn.com/ossbss/charge.once/wsdl/entity/Tis/xsd/1"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:wsa="http://www.w3.org/2005/08/addressing"
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Header>
    <wsse:Security>
      <wsse:UsernameToken>
        <wsse:Username>$USER_NAME</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">$USER_PASSWORD</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soapenv:Header>

    <soapenv:Body>
        <q0:CommandRequestData>
          <q0:Environment>
            <q0:Parameter name="ApplicationDomain" value="CAO_LDM_00" />
            <q0:Parameter name="DefaultOperationNamespace" value="GMF" />
          </q0:Environment>
           <q0:Command>
    <q0:Transaction>  
    <!-- read the Balance of a customer -->
        <q0:Operation name="Read" modifier="ROP">
            <q0:ParameterList>
                <q0:StringParameter name="CustomerId">$NUMBER</q0:StringParameter>
                <q0:SymbolicParameter namespace="@" name="SelectionDate">NOW</q0:SymbolicParameter>
            </q0:ParameterList>

            <q0:OutputSpecification>
                <q0:StructParam namespace="GDM" name="Customer">>
                    <q0:SimpleParam name="CustomerId" />
                </q0:StructParam>
                <q0:SimpleParam name="OnPeakAccountID_FU" />
            </q0:OutputSpecification>
        </q0:Operation>
    </q0:Transaction>  
           </q0:Command>
        </q0:CommandRequestData>
    </soapenv:Body>
</soapenv:Envelope>

此处用户名和密码都必须在标题上发送,$ NUMBER在正文中。并在响应中请求CustomerID和OnPeakAccountID_FU参数。 我想使用curl来执行这个帖子请求但是我不知道如何格式化请求本身。即WDL。

修改 http请求发送到我们的内部服务器,如:

http://IP:PORT/TisService

1 个答案:

答案 0 :(得分:1)

您可以设置这样的soapclient:

class some_service {
  private function securityheader($username, $password){
    $ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
    $password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
    $root = new SimpleXMLElement('<root/>');
    $root->registerXPathNamespace('wsse', $ns_wsse);
    $security = $root->addChild('wsse:Security', null, $ns_wsse);
    $usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse);
    $usernameToken->addChild('wsse:Username', $username, $ns_wsse);
    $usernameToken->addChild('wsse:Password', $password, $ns_wsse)->addAttribute('Type', $password_type);
    $auth = $root->xpath('/root/wsse:Security')[0]->asXML();
    return new SoapHeader($ns_wsse, 'Security', new SoapVar($auth, XSD_ANYXML), true);
  }

  function __construct($username, $password){
    //assuming this is the wsdl file
    $this->client = new SoapClient('http://nsn.com/ossbss/charge.once/wsdl/entity/Tis/xsd/1', array('trace' => true));
    $this->client->__setSoapHeaders($this->securityheader($username, $password));
  }
}

$USER_NAME = 'username';
$USER_PASSWORD = 'password';

$service = new some_service($USER_NAME, $USER_PASSWORD);

标题看起来不错,但我无法针对您正在使用的服务进行测试。

你可以这样打电话:

$response = $service->client->CommandRequestData($some_data_object);
print_r($response);

并看到发送的xml,请执行以下操作:

print_r($service->client->__getLastRequest());

如果没有wsdl,我不知道如何在$ some_data_object中安排数据。令我担心的是,您的示例x​​ml看起来不像是转换为xml的对象,但在某些属性上具有属性,并且不需要复杂性。也许您可以使用wsdl viewer来了解wsdl对数据的期望。

可能以这种方式预期数据,或者文档可能与wsdl不匹配。