我尝试了使用SOAP PHP服务器和ajax客户端,并找到了可行的代码
首先从这里下载nusoap库
然后创建server.php
<?php
require_once ('lib/nusoap.php');
function TriangleArea($b, $h) { return 'The triangle area is: ' .(($b*$h)/2); }
function RectangleArea($L, $l) { return 'The rectangle area is: ' .($L*$l); }
function get_message($your_name)
{
if(!$your_name){
return new soap_fault('Client','','Put Your Name!');
}
$result = "Welcome to ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
return $result;
}
$server = new soap_server;
$server->configureWSDL('mathwsdl', 'urn:mathwsdl');
$server->register('TriangleArea', // method name
array('b' => 'xsd:int', 'h' => 'xsd:int'),
array('area_t' => 'xsd:string'),
'urn:mathwsdl',
'urn:mathwsdl#TriangleArea',
'rpc',
'encoded',
'1=> : Calculate a triangle area as (b*h)/2'
);
$server->register('RectangleArea', // method name
array('L' => 'xsd:int', 'l' => 'xsd:int'),
array('area_r' => 'xsd:string'),
'urn:mathwsdl',
'urn:RectangleAreawsdl#RectangleArea',
'rpc',
'encoded',
'2=> : Calculate a rectangle area as (L*l)'
);
$server->register('get_message', // method name
array('nm' => 'xsd:string'),
array('area_r' => 'xsd:string'),
'urn:mathwsdl',
'urn:get_messagewsdl#get_message',
'rpc',
'encoded',
'3=> : Print a Message as name'
);
if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
接着创建 client.php
<?php
require_once ('lib/nusoap.php');
$param = array( 'your_name' => 'Monotosh Roy');
$client = new soapclient('http://localhost:81/WebServiceSOAP/server.php?wsdl', true);
$err = $client->getError();
if ($err)
$result = $client->call('TriangleArea',
array('b' => 10, 'h' => 15));
if ($client->fault) else else
}
$result = $client->call('RectangleArea',
array('L' => 40, 'l' => 20));
if ($client->fault) else else
}
/* echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request,
ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response,
ENT_QUOTES) . '</pre>';*/
$response = $client->call('get_message',$param);
if($client->fault)
else
?>
<!DOCTYPE html PUBLIC "-
<html xmlns="http:
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title>Web Service SOAP and AJAX</title>
</head>
<script type="text/javascript" src="ajaxSOAP.js"></script>
<body>
<div style="position:relative;left:0px;
top:-12px;background-color:#1D3968;margin:0px;">
<h2 align="center"><font color="#ffffff">
Consume WebServices through SOAP-AJAX calls</font></h2></div>
<table align="center" cellpading="0px" cellspacing="3px"
bordercolor="#000000" border="0"
style="position:relative;width:300px;height:200px;">
<tr>
<td colspan="2" align="center"><h1>Rectangle Area</h1></td>
</tr>
<tr>
<td valign="center"><font color="#cc0000" size="3">
Insert value for l:</font></td>
<td><input id="l_id" type="text"></td>
</tr>
<tr>
<td><font color="#cc0000" size="3">Insert value for L:</font></td>
<td><input id="L_id" type="text"></td>
</tr>
<tr>
<td><input type="button" value="Calculate Area" onclick="myAjax();"></td>
</tr>
<tr>
<td colspan="2">
<div id="resultDiv"></div>
</td>
</tr>
</table>
</body>
</html>
ajaxSOAP.js 文件包含
var xhrTimeout=100;
function myAjax(){
var l_var = document.getElementById("l_id").value;
var L_var = document.getElementById("L_id").value;
var soapMessage ='<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:mathwsdl"> <SOAP-ENV:Body><tns:RectangleArea xmlns:tns="urn:mathwsdl"><L xsi:type="xsd:int">'+L_var+'</L><l xsi:type="xsd:int">'+l_var+'</l></tns:RectangleArea></SOAP-ENV:Body></SOAP-ENV:Envelope>';
var url='http://localhost:81/WebServiceSOAP/server.php';
if(window.XMLHttpRequest) {
httpRequest=new XMLHttpRequest();
}
else if (window.ActiveXObject) {
httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
httpRequest.open("POST",url,true);
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType("text/xml");
}
httpRequest.onreadystatechange=callbackAjax;
httpRequest.setRequestHeader("Man","POST http://localhost:81/WebServiceSOAP/server.php HTTP/1.1")
httpRequest.setRequestHeader("MessageType", "CALL");
httpRequest.setRequestHeader("Content-Type", "text/xml");
httpRequest.send(soapMessage);
}
function callbackAjax(){
try {
if(httpRequest.readyState==4) {
if(httpRequest.status==200) {
clearTimeout(xhrTimeout);
resultDiv=document.getElementById("resultDiv");
resultDiv.style.display='inline';
resultDiv.innerHTML='<font color="#cc0000" size="4"><b>'+httpRequest.responseText+'</b></font>';
}
}
} catch(e) {
alert("Error!"+e);
}
}