我在Web服務的新,所以我道歉,如果我在這裏做一些錯誤的基數,嘿嘿消費PHP SOAP服務。如何使用WCF
我已經使用PHP構建了SOAP服務。服務與SOAP 1.2兼容,並且我有WSDL可用。我已經啓用了會話,所以我可以跟蹤登錄狀態等
我不需要在這裏一些超級安全(即消息級安全),我需要的是傳輸安全(HTTPS),因爲該服務將不經常使用,表演不是一個問題。
我有使它在所有的工作困難。 C#拋出了一些不清楚的異常(「服務器返回一個無效的SOAP錯誤,請參閱InnerException獲取更多細節。」,然後說「在限定名稱中使用未綁定前綴'rpc:ProcedureNotPresent'。」),但使用PHP SOAP客戶端表現如預期(包括會話和所有)。
到目前爲止,我有以下的代碼。 注:由於真正的代碼量,我張貼最少的代碼配置
PHP SOAP服務器(使用Zend皁Server庫),包括通過服務暴露類(ES):
<?php
class Verification_LiteralDocumentProxy {
protected $instance;
public function __call($methodName, $args)
{
if ($this->instance === null)
{
$this->instance = new Verification();
}
$result = call_user_func_array(array($this->instance, $methodName), $args[0]);
return array($methodName.'Result' => $result);
}
}
class Verification {
private $guid = '';
private $hwid = '';
/**
* Initialize connection
*
* @param string GUID
* @param string HWID
* @return bool
*/
public function Initialize($guid, $hwid)
{
$this->guid = $guid;
$this->hwid = $hwid;
return true;
}
/**
* Closes session
*
* @return void
*/
public function Close()
{
// if session is working, $this->hwid and $this->guid
// should contain non-empty values
}
}
// start up session stuff
$sess = Session::instance();
require_once 'Zend/Soap/Server.php';
$server = new Zend_Soap_Server('https://www.somesite.com/api?wsdl');
$server->setClass('Verification_LiteralDocumentProxy');
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
$server->handle();
WSDL:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="https://www.somesite.com/api" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="Verification" targetNamespace="https://www.somesite.com/api">
<types>
<xsd:schema targetNamespace="https://www.somesite.com/api">
<xsd:element name="Initialize">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="guid" type="xsd:string"/>
<xsd:element name="hwid" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="InitializeResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="InitializeResult" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Close">
<xsd:complexType/>
</xsd:element>
</xsd:schema>
</types>
<portType name="VerificationPort">
<operation name="Initialize">
<documentation>
Initializes connection with server</documentation>
<input message="tns:InitializeIn"/>
<output message="tns:InitializeOut"/>
</operation>
<operation name="Close">
<documentation>
Closes session between client and server</documentation>
<input message="tns:CloseIn"/>
</operation>
</portType>
<binding name="VerificationBinding" type="tns:VerificationPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="Initialize">
<soap:operation soapAction="https://www.somesite.com/api#Initialize"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="Close">
<soap:operation soapAction="https://www.somesite.com/api#Close"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="VerificationService">
<port name="VerificationPort" binding="tns:VerificationBinding">
<soap:address location="https://www.somesite.com/api"/>
</port>
</service>
<message name="InitializeIn">
<part name="parameters" element="tns:Initialize"/>
</message>
<message name="InitializeOut">
<part name="parameters" element="tns:InitializeResponse"/>
</message>
<message name="CloseIn">
<part name="parameters" element="tns:Close"/>
</message>
</definitions>
最後,WCF C#代碼消費:
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IVerification
{
[OperationContract(Action = "Initialize", IsInitiating = true)]
bool Initialize(string guid, string hwid);
[OperationContract(Action = "Close", IsInitiating = false, IsTerminating = true)]
void Close();
}
class Program
{
static void Main(string[] args)
{
WSHttpBinding whb = new WSHttpBinding(SecurityMode.Transport, true);
ChannelFactory<IVerification> cf = new ChannelFactory<IVerification>(
whb, "https://www.somesite.com/api");
IVerification client = cf.CreateChannel();
Console.WriteLine(client.Initialize("123451515", "15498518").ToString());
client.Close();
}
}
任何想法?我在這裏做錯了什麼?
沒有。當我需要再次嘗試時,我會嘗試另一種方法。不管怎麼說,還是要謝謝你。 – 2010-06-13 13:10:52