|
php4時(shí)代調(diào)用webservice大部分使用的nusoap。到了php5已經(jīng)有了自己的soap擴(kuò)展。所以可以完全的拋棄nusoap這個(gè)許久沒有更新過的東西了。
因?yàn)槟壳笆潜镜亻_發(fā)需要。只說windows下的。
配置環(huán)境
windows下找到php安裝目錄下的php.ini。打開后編輯。找到
然后將前面的;號(hào)去掉。
然后就是寫一個(gè)php文件來驗(yàn)證一下。
實(shí)例程序
在apache的htdocs目錄下創(chuàng)建ws.php
代碼如下:
<?php header("content-type:text/html;charset=utf-8"); try { $client = new SoapClient( 'http: var_dump($client->__getFunctions()); } catch (SOAPFault $e) { print_r($e); } ?>
在瀏覽器中輸入:http://localhost/ws.php后會(huì)出現(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./SendSMS/Service.asmx?wsdl',array('trace' =>
true, 'exceptions' => true ));
這里的SoapClient類可以作為給定的ws的客戶端。這個(gè)SoapClient有兩種操作模式。
一個(gè)是WSDL模式,一個(gè)是Non-WSDL模式。
當(dāng)然這里用的是WSDL模式。所以重點(diǎn)來說第一種。
WSDL模式中,SoapClient的構(gòu)造參數(shù)分別是ws的請(qǐng)求地址以及各種請(qǐng)求配置參數(shù)。
var_dump($client->__getFunctions());
這里就是訪問后輸出的這個(gè)接口可提供的方法,返回值以及參數(shù)。
那么如何去調(diào)用方法呢。
<?php header("content-type:text/html;charset=utf-8"); try { $client = new SoapClient( 'http: array('trace' => true, 'exceptions' => true )); var_dump($client->__getFunctions()); $v = array("Token"=>""); $headers = new SoapHeader("http:,"AuthenticationHeader",$v, false, SOAP_ACTOR_NEXT); $client->__setSoapHeaders(array($headers)); $sms1 = array( 'Id'=>100000, 'SjNo'=>'13512222222', 'UnickName'=>'tuangou', 'SmsContent'=>'test', 'Type'=>1000, 'OrderIdString'=>'1231114567' ); $param = array( 'sms1'=>$sms1 ); $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. Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http:///SendSMS1"
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:xsd="http://www./2001/XMLSchema" xmlns:soap="http://schemas./soap/envelope/"> <soap:Header> <AuthenticationHeader xmlns="http:///"> <Token>string</Token> </AuthenticationHeader> </soap:Header> <soap:Body> <SendSMS1 xmlns="http:///"> <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é)點(diǎn)是方法名。
sms1節(jié)點(diǎn)就是參數(shù)。
sms1節(jié)點(diǎn)下的就是參數(shù)的屬性。
一定要按照順序?qū)傩赃M(jìn)行逐個(gè)賦值。
|