2017-08-08 159 views
0

我想从PHP脚本向我的服务器发送SOAP请求。我们的供应商提供的肥皂模板是这样的:从PHP发送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> 

这里的用户名和密码都必须在头中发送,$号在体内。并在响应中询问CustomerID和OnPeakAccountID_FU参数。 我想使用curl来执行这个post请求,但不知道如何格式化请求本身。即WDL。

编辑: HTTP请求去我们的内部服务器,如:

http://IP:PORT/TisService 
+0

php有一个内置的[soapclient](http://php.net/manual/en/class.soapclient.php)类,可能会更容易。你确定你想用卷发与肥皂api谈谈吗? –

+0

http://nsn.com/ossbss/charge.once/wsdl/entity/Tis/xsd/1 is 404 –

+0

我已阅读一些使用cURL的建议。无论如何,这不是一个问题,我只是想知道如何格式化我的请求,并在头中传递登录细节,并请求回复 – Fshamri

回答

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中。令我担心的是你的示例xml看起来不像是一个转换成xml的对象,但是它具有某些属性的属性,并且复杂性不是必需的。也许你可以看到wsdl如何使用wsdl viewer预期数据。

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

+0

感谢@Peter Rakmanyi你的解决方案是非常有用的,它解决了我的问题,我有问题为SOAP API生成wsse头文件,现在使用此代码现在我可以发送自定义头文件和'wsse :UsernameToken'信息。 –

+0

@ engr.waqas您还可以扩展soapheader类来创建它,就像在[这个答案](https://stackoverflow.com/questions/953639/connecting-to-ws-security-protected-web-service-with- PHP/6677930#6677930)。 –