php4時代調用webservice大部分使用的nusoap。到了php5已經(jīng)有了自己的soap擴展。所以可以完全的拋棄nusoap這個許久沒有更新過的東西了。
因為目前是本地開發(fā)需要。只說windows下的。
windows下找到php安裝目錄下的php.ini。打開后編輯。找到
extension=php_soap.dll
然后將前面的;號去掉。
然后就是寫一個php文件來驗證一下。
在apache的htdocs目錄下創(chuàng)建ws.php
代碼如下:
<?php
header("content-type:text/html;charset=utf-8");
try {
$client = new SoapClient( 'http://erp.test.com/SendSMS/Service.asmx?wsdl',array('trace' => true, 'exceptions' => true ));
var_dump($client->__getFunctions());
} catch (SOAPFault $e) {
print_r($e);
}
?>
在瀏覽器中輸入:http://localhost/ws.php后會出現(xiàn)
array
0 => string 'SendSMSResponse SendSMS(SendSMS $parameters)' (length=44)
1 => string 'SendSMS1Response SendSMS1(SendSMS1 $parameters)' (length=47)
2 => string 'SendMailResponse SendMail(SendMail $parameters)' (length=47)
3 => string 'IsSendSMSResponse IsSendSMS(IsSendSMS $parameters)' (length=50)
4 => string 'IsSendSMS1Response IsSendSMS1(IsSendSMS1 $parameters)' (length=53)
5 => string 'SendSMSResponse SendSMS(SendSMS $parameters)' (length=44)
6 => string 'SendSMS1Response SendSMS1(SendSMS1 $parameters)' (length=47)
7 => string 'SendMailResponse SendMail(SendMail $parameters)' (length=47)
8 => string 'IsSendSMSResponse IsSendSMS(IsSendSMS $parameters)' (length=50)
9 => string 'IsSendSMS1Response IsSendSMS1(IsSendSMS1 $parameters)' (length=53)
現(xiàn)在逐行解釋一下。$client = new SoapClient('http://erp.test.com/SendSMS/Service.asmx?wsdl',array('trace' =>true, 'exceptions' => true ));
這里的SoapClient類可以作為給定的ws的客戶端。這個SoapClient有兩種操作模式。
一個是WSDL模式,一個是Non-WSDL模式。
當然這里用的是WSDL模式。所以重點來說第一種。
WSDL模式中,SoapClient的構造參數(shù)分別是ws的請求地址以及各種請求配置參數(shù)。
var_dump($client->__getFunctions());
這里就是訪問后輸出的這個接口可提供的方法,返回值以及參數(shù)。
那么如何去調用方法呢。
<?php
header("content-type:text/html;charset=utf-8");
try {
$client = new SoapClient( 'http://erp.test.com/SendSMS/Service.asmx?wsdl',
array('trace' => true, 'exceptions' => true ));
var_dump($client->__getFunctions());
//第一個參數(shù)是命名空間,第二個參數(shù)是SoapHeader頭的類名,第三個是SoapHeader參數(shù)的數(shù)組可以寫成array
$v = array("Token"=>"");
$headers = new SoapHeader("http://test.com/","AuthenticationHeader",$v, false, SOAP_ACTOR_NEXT);
$client->__setSoapHeaders(array($headers));
//$types = $client->__getTypes(); //這里是為了查看方法的類型
//print_r($types);
//這里就是根據(jù)方法參數(shù)的需要虛擬出來一個sms類型的數(shù)組
$sms1 = array(
'Id'=>100000,
'SjNo'=>'13512222222',
'UnickName'=>'tuangou',
'SmsContent'=>'test',
'Type'=>1000,
'OrderIdString'=>'1231114567'
);
$param = array(
'sms1'=>$sms1
);
//這里是需要注意到地方。調用方法的參數(shù)必須是一個數(shù)組。而且默認以parameters字段標識為參數(shù)數(shù)組。真正的參數(shù)都要放在$param變量中。
$return = $client->__soapCall("SendSMS1",array('parameters'=>$param));
print_r($return);
} catch (SOAPFault $e) {
print_r('Exception:'.$e);
}
?>
POST /SendSMS/Service.asmx HTTP/1.1
Host: erp.test.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://test.com/SendSMS1"
<?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:Header>
<AuthenticationHeader xmlns="http://test.com/">
<Token>string</Token>
</AuthenticationHeader>
</soap:Header>
<soap:Body>
<SendSMS1 xmlns="http://test.com/">
<sms1>
<Id>int</Id>
<SjNo>string</SjNo>
<UnickName>string</UnickName>
<SmsContent>string</SmsContent>
<Type>int</Type>
<OrderIdString>string</OrderIdString>
</sms1>
</SendSMS1>
</soap:Body>
</soap:Envelope>
這里可以看到。soap的Header和Body。Header里就是Token。Body里就是具體的方法了。
SendSMS1節(jié)點是方法名。
sms1節(jié)點就是參數(shù)。
sms1節(jié)點下的就是參數(shù)的屬性。
一定要按照順序對屬性進行逐個賦值。