如何使用WSSE与SOAP API建立PHP连接

时间:2014-03-21 16:17:26

标签: php soap

我需要帮助!我几乎到处搜索,但不幸的是,没有太多关于PHP& amp;的信息。 SOAP& WSSE。我在这里张贴,看看是否有人可以帮助我。 我使用此库使用WSSE连接到SOAP服务:http://goo.gl/en7hvh 这是我的连接设置:http://pastebin.com/vc3X5aDj但我收到此错误:

 Fault string: Unauthorized request. Make sure the Customer name, User and 
 Password are provided.

 Fault code: soap:Client

我已经仔细检查过我的用户名&密码,他们都是正确的。如果我调用getLastResponse和getLastReponseRequest,则会出现:

 HTTP/1.1 500 Internal Server Error Cache-Control: private Content-Type: text/xml; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Date: Thu, 20 Mar 2014 23:43:53 GMT Content-Length: 1226

 http://schemas.xmlsoap.org/ws/2004/08/addressing/faulturn:uuid:b38a9ad8-d819-463c-a801-122009040f04urn:uuid:3f9247a3-4cbd-4d0d-964a-bf9182867d4ahttp://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous2014-03-20T23:43:53Z2014-03-20T23:48:53Zsoap:ClientUnauthorized request. Make sure the Customer name, User and Password are provided.

这是我需要生成的请求:http://pastebin.com/aRMmVMj0 我不知道我错过了什么....请帮助我!!

1 个答案:

答案 0 :(得分:1)

我知道你的痛苦,幸运的是我已经克服了它!

class WSSEAuth {

   private $Username;
   private $Password;

   function __construct($username, $password) {
      $this->Username = $username;
      $this->Password = $password;
   }

}

class WSSEToken {

   private $UsernameToken;

   function __construct($innerVal) {
      $this->UsernameToken = $innerVal;
   }

}

class MySoapClient {

   private $username;
   private $password;
   private $options;
   private $header;
   private $wsdl;
   private $wsse;
   private $gw;
   public $start_time;

   public function getClient($wsse, $user, $pw, $wsdl, $gw) {

      $this->wsse = $wsse;
      $this->username = $user;
      $this->password = $pw;
      $this->wsdl = $wsdl;
      $this->gw = $gw;
      $this->start_time = microtime(true);

      $this->options = array(
          'trace'=>1,
          'username'=>$this->username,
          'password'=>$this->password,
          'exceptions'=>true,
          'connection_timeout'=>5,
      );

      $objSoapVarUser = new SoapVar($this->username, XSD_STRING, NULL, $this->wsse, NULL, $this->wsse);
      $objSoapVarPass = new SoapVar($this->password, XSD_STRING, NULL, $this->wsse, NULL, $this->wsse);
      $objWSSEAuth = new WSSEAuth($objSoapVarUser, $objSoapVarPass);
      $objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $this->wsse, 'UsernameToken', $this->wsse);
      $objWSSEToken = new WSSEToken($objSoapVarWSSEAuth);
      $objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $this->wsse, 'UsernameToken', $this->wsse);
      $this->header = new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $this->wsse, 'Security', $this->wsse);
      $objSoapVarWSSEHeader[] = new SoapHeader($this->wsse, 'Security', $this->header, true, $this->gw);

      $objClient = new SoapClient($this->wsdl, $this->options);
      $objClient->__setSoapHeaders($objSoapVarWSSEHeader);

      return $objClient;
   }

}

在所有这些类之后,执行以下操作:

$var = new MySoapClient;
$var_client = $var->getClient('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'your_user', 'your_password', 'your_wsdl', 'your_gate');

然后例如,你可以调用类似的东西:

$var_client->__soapCall('Function', $soap_payload);

编辑:我修正了一些拼写错误!