PHP Soap - 无法从服务器(WSDL)请求

时间:2017-10-30 03:28:28

标签: php xml soap wsse

我对SOAP服务很新。

我在PHP中实现了这个错误。

  

服务器无法处理请求。 --->对象引用未设置为对象的实例。

代码:肥皂信封(我使用邮递员检索)



<?xml version="1.0" encoding="utf-8"?>
<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/" 
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>username</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soapenv:Header>
  <soap:Body>
    <GetDataXMLbyID xmlns="http://tempuri.org/">
      <id>3133717</id>
      <datatypes>
        <AgentName>TEST</AgentName>
      </datatypes>
    </GetDataXMLbyID>
  </soap:Body>
</soap:Envelope>
&#13;
&#13;
&#13;

代码:PHP

&#13;
&#13;
<?php  

class WsseAuthHeader extends SoapHeader {

    private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';

    function __construct($user, $pass, $ns = null) {
        if ($ns) {
            $this->wss_ns = $ns;
        }

        $auth = new stdClass();
        $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
        $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);

        $username_token = new stdClass();
        $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);

        $security_sv = new SoapVar(
                new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns), SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
        parent::__construct($this->wss_ns, 'Security', $security_sv, true);
    }

}

$username = 'username';
$password = 'password';
$wsdl = 'url';

$wsse_header = new WsseAuthHeader($username, $password);

$client = new SoapClient($wsdl, array(
    "trace" => 1,
    "exceptions" => 0,
    'SOAPAction' => 'http://tempuri.org/GetXMLDatabyID',
    'header' => 'Content-Type: text/xml; charset=utf-8'
        )
);

$client->__setSoapHeaders(array($wsse_header));

$request = array(
    'GetXMLDatabyIDResult' => array(
        'id' => '313371',
        'AgentName' => 'TEST',
    )
);

$results = $client->getXMLDatabyID($request);

print_r($results);
&#13;
&#13;
&#13;

我想将这样的数据检索到PHP(结果)

&#13;
&#13;
<Name>JOHN PAUL CORNELIUS</Name>
<DOB>1980-11-12T00:00:00+08:00</DOB>
&#13;
&#13;
&#13;

问题是,我没有在PHP中使用SOAP服务的经验,这就是我被困的原因。有人可以帮帮我吗?

谢谢:)

0 个答案:

没有答案