【问题标题】:Invoking WCF service using PHP with SOAP 1.2 gives an error使用带有 SOAP 1.2 的 PHP 调用 WCF 服务会出错
【发布时间】:2022-11-10 07:56:02
【问题描述】:

我正在尝试使用 PHP 肥皂客户端访问 WCF 服务。 我不断收到错误消息:“错误获取 http 标头” 我也尝试增加 default_socket_time 。 访问服务时,我需要使用基本身份验证以及方法参数。 以下是我的 PHP 代码。我无法更改服务器配置。

<?php
    $wsdl = "https://example.com/NewServer/Services/WCFServices.svc?wsdl";

$url=$wsdl;
$svc = 'WCFServices';
$func = 'getValId';
$username = "admin";
$password = "admin";


$apiauth =array('UserName'=>$username,'Password'=>$password);
$authHeader = new SoapHeader('https://tempuri.org/', 'AuthHeader', $apiauth);

$client = new SoapClient($url, array('soap_version' => SOAP_1_2, 
                                     'exceptions' => true,
                                     'trace' => true);

try
{ 
    ini_set('default_socket_timeout', 600);
    $client->__setSoapHeaders($authHeader);
    $info = $client->__soapCall($func, array('extRef' => 'ABCD'));
      var_dump($info);
} 
catch (SoapFault $fault) 
{ 
    var_dump($fault);
      $xml=$fault->faultstring;
    die;
}
?>

正确路径的指导将不胜感激。

【问题讨论】:

    标签: php wcf soap1.2


    【解决方案1】:

    我正在调用的 WCF 服务具有可用的 HTTP 和基本绑定。 所以我调用了使用 SOAP 1.1 版本的基本端点。发布代码,因为它对将来的某人有用。

    <?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);
            }
        }
        
        $options = array( 
                    'soap_version'    => SOAP_1_1, 
                    'exceptions'      => true, 
                    'trace'           => 1, 
                    'wdsl_local_copy' => true);
        
        
        $username = "username";
        $password = "password";
        
        $wsse_header = new WsseAuthHeader($username, $password);    
        
        $client = new SoapClient('https://ServerName/WCFService.svc?wsdl', $options); 
        $client->__setSoapHeaders(array($wsse_header));
        $client->__setLocation('https://ServerName/WCFService.svc/basicSecurity');
        
        try
        {
            $params = array('param1' => 'xxxx');
            $phpresponse = $client->__soapCall('methodname', array('parameters' => $params));
        
            var_dump($phpresponse);
            echo "</b><BR/><BR/>";
        }
        catch(Exception $e) 
        { 
            echo "<h2>Exception Error!</h2></b>"; 
            echo $e->getMessage(); 
        }
        
        ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多