如何在php soap客户端为wcf传递单个标记头


How to pass single tag header in php soap client for wcf?

我正在尝试从PHP使用WCF服务。我需要在PHP的soap客户端传递一个头字段,以便使用它。Soap头应该是这样的:

<header>
<LisenseKey>Lisense key goes here</LisenseKey>
</header>

LisenseKey元素的命名空间为"http://mylinsensekeynamespace"。

我想在WCF中使用的方法如下:

public string function GetMessage(string name)
{
    return "Hello , "+name;
}

在服务配置为验证头之前,我从PHP中使用服务,如下所示,它工作得很好:

try{
    $client = new SoapClient("http://localhost:8181/?wsdl");
    $param = new stdClass();
    $param->name = "My Name";
    $webService = $client->GetMessage($param);
    print_r($webService);
}
catch(Exception $e)
{
    echo $e->getMessage();  
}

当服务配置为验证头中的许可密钥后,我试图像这样消费它,它还不工作:

try{
    $client = new SoapClient("http://localhost:8181/?wsdl");
    $actionHeader = new SoapHeader("http://mycustomheader",'LisenseKey',"lisense key",true);
    $client->__setSoapHeaders($actionHeader);
    $param = new stdClass();
    $param->name = "My Name";
    $webService = $client->GetMessage($param);
    print_r($webService);
}
catch(Exception $e)
{
    echo $e->getMessage();  
}

我已经从网上文章中尝试了很多不同的方法。我怎么吃?WCF服务正在使用BasicHttpBinding, SOAP版本应该是1.1。如何传递报头信息以使用服务?

以下是. net WCF服务代码,用于验证每个请求的LicenseKey soap头。

    public class MyServiceMessageInspector : System.ServiceModel.Dispatcher.IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel,
            System.ServiceModel.InstanceContext instanceContext)
        {
            if (request.Headers.FindHeader("LisenseKey", "") == -1)
            {
                throw new FaultException("Lisense Key Was Not Provided");
            }
            var lisenseKey = request.Headers.GetHeader<string>("LisenseKey", "http://mycustomheader.com");
            if (string.IsNullOrEmpty(lisenseKey))
            {
                throw new FaultException("Lisnse key should not be empty");
            }
            if (lisenseKey != "12345x")
            {
                throw new FaultException("Lisense key is not valid");
            }
            return instanceContext;
        }
        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
        }
    }
    public class MyServiceMessageInspectorBehaviour : Attribute, System.ServiceModel.Description.IServiceBehavior
    {
        public void AddBindingParameters(System.ServiceModel.Description.ServiceDescription serviceDescription,
            System.ServiceModel.ServiceHostBase serviceHostBase,
            System.Collections.ObjectModel.Collection<System.ServiceModel.Description.ServiceEndpoint> endpoints,
            System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }
        public void ApplyDispatchBehavior(System.ServiceModel.Description.ServiceDescription serviceDescription,
            System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
            {
                foreach (var endpointDispatcher in channelDispatcher.Endpoints)
                {
                    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyServiceMessageInspector());
                }
            }
        }
        public void Validate(System.ServiceModel.Description.ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
        }
    }

尝试这样调用WCF服务:$client->__soapCall("GetMessage", $param, NULL, $header);