我如何在php中使用soapclient和wsdl webservice

时间:2014-12-01 13:51:14

标签: c# php web-services soap wsdl

我正在尝试使用SOAP / wsdl连接到web服务并进行身份验证,但是我收到此错误:   对象引用未设置为对象的实例

我有这样的WSDL: http://ibsm.ir/webservice/ibsmwebservice.asmx?op=BikeCount

我试图通过此代码在PHP soapclient中使用它:

$client=new SoapClient('http://ibsm.ir/webservice/ibsmwebservice.asmx?wsdl',['trace'=>1]);

$auth = array(
    "Username"=>"xxxx",
    "Password"=>"xxxx"
     );

$soap_header = new SoapHeader(
    "http://ibsm.ir/",
    "SecuredWebServiceHeaders", 
$auth,
    FALSE
);

$client->__setSoapHeaders($soap_header);
$res= $client->BikeCount(['Station_Code'=>"9"]);
print_r($res->BikeCountResult);
print_r($client->__getLastRequest());

我有以下c#代码可以正常工作。

ServiceReference1.SecuredWebServiceHeader ServiceHeader = new ServiceReference1.SecuredWebServiceHeader();
        Console.WriteLine("Enter UserName:");
        ServiceHeader.Username = Console.ReadLine();
        Console.WriteLine("EnterPass:");
        ServiceHeader.Password = Console.ReadLine();
        string STA_CODE = "";
        Console.WriteLine("Enter STA_CODE : ");
        STA_CODE = Console.ReadLine();

        ServiceReference1.IbsmWebServiceSoapClient ServiceSoapClient = new ServiceReference1.IbsmWebServiceSoapClient();
        Console.WriteLine(ServiceSoapClient.BikeCount(ServiceHeader, STA_CODE));
        Console.ReadLine();

有人可以指出我做错了什么?或者我错过了什么?

感谢任何帮助。

更新:

namespace ConsoleApplication1.ServiceReference1 {


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://ibsm.ir/", ConfigurationName="ServiceReference1.IbsmWebServiceSoap")]
public interface IbsmWebServiceSoap {

    // CODEGEN: Generating message contract since message BikeCountRequest has headers
    [System.ServiceModel.OperationContractAttribute(Action="http://ibsm.ir/BikeCount", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
    ConsoleApplication1.ServiceReference1.BikeCountResponse BikeCount(ConsoleApplication1.ServiceReference1.BikeCountRequest request);
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://ibsm.ir/")]
public partial class SecuredWebServiceHeader : object, System.ComponentModel.INotifyPropertyChanged {

    private string usernameField;

    private string passwordField;

    private System.Xml.XmlAttribute[] anyAttrField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public string Username {
        get {
            return this.usernameField;
        }
        set {
            this.usernameField = value;
            this.RaisePropertyChanged("Username");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public string Password {
        get {
            return this.passwordField;
        }
        set {
            this.passwordField = value;
            this.RaisePropertyChanged("Password");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyAttributeAttribute()]
    public System.Xml.XmlAttribute[] AnyAttr {
        get {
            return this.anyAttrField;
        }
        set {
            this.anyAttrField = value;
            this.RaisePropertyChanged("AnyAttr");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName="BikeCount", WrapperNamespace="http://ibsm.ir/", IsWrapped=true)]
public partial class BikeCountRequest {

    [System.ServiceModel.MessageHeaderAttribute(Namespace="http://ibsm.ir/")]
    public ConsoleApplication1.ServiceReference1.SecuredWebServiceHeader SecuredWebServiceHeader;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://ibsm.ir/", Order=0)]
    public string Station_Code;

    public BikeCountRequest() {
    }

    public BikeCountRequest(ConsoleApplication1.ServiceReference1.SecuredWebServiceHeader SecuredWebServiceHeader, string Station_Code) {
        this.SecuredWebServiceHeader = SecuredWebServiceHeader;
        this.Station_Code = Station_Code;
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName="BikeCountResponse", WrapperNamespace="http://ibsm.ir/", IsWrapped=true)]
public partial class BikeCountResponse {

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://ibsm.ir/", Order=0)]
    public string BikeCountResult;

    public BikeCountResponse() {
    }

    public BikeCountResponse(string BikeCountResult) {
        this.BikeCountResult = BikeCountResult;
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IbsmWebServiceSoapChannel : ConsoleApplication1.ServiceReference1.IbsmWebServiceSoap, System.ServiceModel.IClientChannel {
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class IbsmWebServiceSoapClient : System.ServiceModel.ClientBase<ConsoleApplication1.ServiceReference1.IbsmWebServiceSoap>, ConsoleApplication1.ServiceReference1.IbsmWebServiceSoap {

    public IbsmWebServiceSoapClient() {
    }

    public IbsmWebServiceSoapClient(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }

    public IbsmWebServiceSoapClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public IbsmWebServiceSoapClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public IbsmWebServiceSoapClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    ConsoleApplication1.ServiceReference1.BikeCountResponse ConsoleApplication1.ServiceReference1.IbsmWebServiceSoap.BikeCount(ConsoleApplication1.ServiceReference1.BikeCountRequest request) {
        return base.Channel.BikeCount(request);
    }

    public string BikeCount(ConsoleApplication1.ServiceReference1.SecuredWebServiceHeader SecuredWebServiceHeader, string Station_Code) {
        ConsoleApplication1.ServiceReference1.BikeCountRequest inValue = new ConsoleApplication1.ServiceReference1.BikeCountRequest();
        inValue.SecuredWebServiceHeader = SecuredWebServiceHeader;
        inValue.Station_Code = Station_Code;
        ConsoleApplication1.ServiceReference1.BikeCountResponse retVal = ((ConsoleApplication1.ServiceReference1.IbsmWebServiceSoap)(this)).BikeCount(inValue);
        return retVal.BikeCountResult;
    }
}

}

0 个答案:

没有答案