2010-04-08 72 views
5

我是soapclient的新手,我试着在网上做一些研究,也尝试过在肥皂上编码,但看起来这仍然不适用于我,只是在这里徘徊任何人都可以指出,也许给我一些例子,我怎样才能真正使用soapclint从下面的Web服务器获得反馈?如何在php上使用SoapClient

POST /webservices/tempconvert.asmx HTTP/1.1 
Host: www.w3schools.com 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://tempuri.org/CelsiusToFahrenheit" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <CelsiusToFahrenheit xmlns="http://tempuri.org/"> 
     <Celsius>string</Celsius> 
    </CelsiusToFahrenheit> 
    </soap:Body> 
</soap:Envelope> 

HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <CelsiusToFahrenheitResponse xmlns="http://tempuri.org/"> 
     <CelsiusToFahrenheitResult>string</CelsiusToFahrenheitResult> 
    </CelsiusToFahrenheitResponse> 
    </soap:Body> 
</soap:Envelope> 



<?php 
$url = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"; 
$client = new SoapClient($url); 


?> 

我应该怎样做下一步,以便我能得到回应?

回答

10

你首先要实例化SoapClient类,像你这样:

$url = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"; 
$client = new SoapClient($url); 


然后,你必须电话要使用的方法 - 可以找到方法名在WSDL中。

例如,我们可以称之为一个名为CelsiusToFahrenheit方法,在此WebService:

$result = $client->CelsiusToFahrenheit(/* PARAMETERS HERE */); 


现在的问题是要知道哪些应PARAMATERS传递;如何...

如果你看一下WSDL,你会看到这个部分:

<s:element name="CelsiusToFahrenheit"> 
    <s:complexType> 
    <s:sequence> 
     <s:element minOccurs="0" maxOccurs="1" name="Celsius" type="s:string" /> 
    </s:sequence> 
    </s:complexType> 
</s:element> 

表明该方法应传递一个数组,其中包含1个项目,这将对“Celsius “作为关键,并将价值转化为价值。

这意味着你必须使用PHP代码的这一部分:

$result = $client->CelsiusToFahrenheit(array('Celsius' => '10')); 


执行此调用,并在转储结果:

var_dump($result); 

获取了这样的输出:

object(stdClass)#2 (1) { 
    ["CelsiusToFahrenheitResult"]=> 
    string(2) "50" 
} 


这意味着你必须使用此:

echo $result->CelsiusToFahrenheitResult . "\n"; 

为了得到结果值

50 


注:该结果的结构可以在WSDL文件中找到也是如此,当然 - 请参阅CelsiusToFahrenheitResponse部分。

+0

@Pascal MARTIN你是超人......感谢这个例子......现在我更了解soapclient的工作方式。谢谢:) – 2010-04-08 22:57:59

+0

不客气:-)玩得开心! – 2010-04-09 04:24:35

+0

我再次面对saop的一些问题..不知道你是否知道我的代码出了什么问题?请clikc在这个问题的链接http://stackoverflow.com/questions/2619519/soap-client-not-working-in-php谢谢 – 2010-04-12 02:22:12