php 致命错误:未捕获的SoapFault异常:[客户端]在中查找"uri"属性时出错

1aaf6o9v  于 2023-01-08  发布在  PHP
关注(0)|答案(1)|浏览(222)

标题给了我一些SO链接,但不幸的是我不能让它们工作。我对SOAP完全是新手。我已经得到了一个wsdl链接,有一些方法我需要通过SOAP调用。
我卡在一个特定的点上
未捕获的SoapFault异常:[客户端]在中查找“uri”属性时出错,在此代码处生成错误。

class SoapClientCompatibility extends SoapClient{
    public function __construct($wsdl, $options){
        // do things here, like:
        // download the wsdl using curl
    }}function soapClientWSSecurityHeader($user, $password){
  // Creating date using yyyy-mm-ddThh:mm:ssZ format
  $tm_created = gmdate('Y-m-d\TH:i:s\Z');
  $tm_expires = gmdate('Y-m-d\TH:i:s\Z', gmdate('U') + 180); //only necessary if using the timestamp element
  // Generating and encoding a random number
  $simple_nonce = mt_rand();
  $encoded_nonce = base64_encode($simple_nonce);
  // Compiling WSS string
  $passdigest = base64_encode(sha1($simple_nonce . $tm_created . $password, true));

  // Initializing namespaces
  $ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
  $ns_wsu = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
  $password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest';
$encoding_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';

  // Creating WSS identification header using SimpleXML
  $root = new SimpleXMLElement('<root/>');

  $security = $root->addChild('wsse:Security', null, $ns_wsse);
  //the timestamp element is not required by all servers
  $timestamp = $security->addChild('wsu:Timestamp', null, $ns_wsu);
  $timestamp->addAttribute('wsu:Id', 'Timestamp-28');
  $timestamp->addChild('wsu:Created', $tm_created, $ns_wsu);
  $timestamp->addChild('wsu:Expires', $tm_expires, $ns_wsu);

  $usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse);
  $usernameToken->addChild('wsse:Username', $user, $ns_wsse);
  $usernameToken->addChild('wsse:Password', $passdigest, $ns_wsse)->addAttribute('Type', $password_type);
  $usernameToken->addChild('wsse:Nonce', $encoded_nonce, $ns_wsse)->addAttribute('EncodingType', $encoding_type);
  $usernameToken->addChild('wsu:Created', $tm_created, $ns_wsu);


  // Recovering XML value from that object
  $root->registerXPathNamespace('wsse', $ns_wsse);
  $full = $root->xpath('/root/wsse:Security');
  $auth = $full[0]->asXML();
  print_r($auth);
  return new SoapHeader($ns_wsse, 'Security', new SoapVar($auth, XSD_ANYXML), true,'https://mywebsite.com');
}
class Valid {
    function Valid($hn, $vc) 
    {
        $this->healthNumber = $healthNumber;
        $this->versionCode = $versionCode;
    }
}
$hcvRequest = array(
    "healthNumber" => "1286844022",
    "versionCode" => "YX"
);
$valid = array(
    "hcvRequest" => $hcvRequest
);

$params = array(
    "requests" => $valid
);

$client = new SoapClientCompatibility('https://example.com/?wsdl');$client->__setSoapHeaders(soapClientWSSecurityHeader('someemail@email.com', 'somepassword'));
echo "<pre>";
$response = $client->__soapCall("validate", array($params));
print_r($client);
print_r($response);
tkclm6bt

tkclm6bt1#

我发现这个页面是因为我在使用php SoapClient时遇到了Error finding "uri" property异常。
最后,我发现错误是由于我引用的WSDL文件的保存版本(保存在本地)已经过期,Web服务已经更改,所以一旦我删除了本地WSDL文件并下载了它的最新版本,错误就不再发生了。
希望这能帮助任何有同样问题的php SoapClient用户。
请注意,我将WSDL文件的本地存储版本传递给SoapClient的第一个参数,我相信这意味着SoapClient不会在每次Soap请求时都拉下WSDL文件。

相关问题