我一直在尝试用PHP学习SOAP,非wsdl模式很好用 . 我将它们包含在寻求学习的人们的信息中 .
暴露library.php的类
class Library {
public function getDwarves(){
$dwarves = array("Bashful","Doc","Dopey");
return $dwarves;
}
public function greetUser($name){
return array("message"=>"hello,".$name);
}
}
?>
非wsdl客户端
try{
$options = array("location" => "http://192.168.1.20/ws/server.php" , "uri" => "http://192.168.1.20/ws");
$client=new SoapClient(null,$options);
$dwarves = $client->getDwarves();
echo nl2br("Result of getDwarves:\n");
var_dump($dwarves);
echo nl2br("\n\n");
$greeting = $client->greetUser("Fairmutex");
echo nl2br("Result of greetUser:\n");
var_dump($greeting);
echo nl2br("\n\n");
}catch(SoapFault $e){
var_dump($e);
echo "
".$e->getMessage()."
";
}
?>
非wsdl服务器
require('library.php');
$options = array("uri" => "http://192.168.1.20");
$server = new SoapServer(null,$options);
$server->setClass('Library');
$server->handle();
?>
但是,当WSDL被引入图片时,服务器返回NULL作为方法调用的结果 . 谁能帮我识别我做错了什么?
server.php
require('library.php');
$server = new SoapServer("wsdl");
$server->setClass('Library');
$server->handle();
?>
client.php
try{
$client=new SoapClient("http://192.168.1.20/ws/wsdl",array( "trace" => 1 ) );
$dwarves = $client->getDwarves();
echo nl2br("Result of getDwarves:\n");
var_dump($dwarves);
echo nl2br("\n\n");
$greeting = $client->greetUser("Fairmutex");
echo nl2br("Result of greetUser:\n");
var_dump($greeting);
echo nl2br("\n\n");
}catch(SoapFault $e){
var_dump($e);
echo "
".$e->getMessage()."
";
}
?>
client.php带有调试信息
try{
$client=new SoapClient("http://192.168.1.20/ws/wsdl",array( "trace" => 1 ) );
$dwarves = $client->getDwarves();
echo nl2br("GetFunctions:\n"); var_dump($client->__getFunctions()); echo nl2br("\n\n");
echo nl2br("GetTypes:\n"); var_dump($client->__getTypes()); echo nl2br("\n\n");
echo nl2br("Request Header:\n" . htmlentities(str_ireplace('>\n__getLastRequestHeaders())) . "\n");
echo nl2br("REQUEST:\n" . htmlentities(str_ireplace('>\n__getLastRequest())) . "\n");
echo nl2br("Response Header:\n" . htmlentities(str_ireplace('>\n__getLastResponseHeaders())) . "\n");
echo nl2br("Response:\n" . htmlentities(str_ireplace('>\n__getLastResponse())) . "\n");
echo nl2br("Result of getDwarves:\n");
var_dump($dwarves);
echo nl2br("\n\n");
$greeting = $client->greetUser("Fairmutex");
echo nl2br("REQUEST:\n" . htmlentities(str_ireplace('>\n__getLastRequest())) . "\n");
echo nl2br("Response Header:\n" . htmlentities(str_ireplace('>\n__getLastResponseHeaders())) . "\n");
echo nl2br("Response:\n" . htmlentities(str_ireplace('>\n__getLastResponse())) . "\n");
echo nl2br("Result of greetUser:\n");
var_dump($greeting);
echo nl2br("\n\n");
}catch(SoapFault $e){
var_dump($e);
echo "
".$e->getMessage()."
";
}
?>
WSDL
路径:
http://192.168.1.20/ws/wsdl
http://192.168.1.20/ws/server.php
http://192.168.1.20/ws/client.php
调试信息客户端的结果
GetFunctions:
array(2) { [0]=> string(17) "void getDwarves()" [1]=> string(29) "void greetUser(anyType $name)" }
GetTypes:
array(0) { }
Request Header:
POST /ws/server.php HTTP/1.1
Host: 192.168.1.20
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.3.3-7+squeeze14
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:LibraryAction"
Content-Length: 385
REQUEST:
Response Header:
HTTP/1.1 200 OK
Date: Sat, 01 Mar 2014 00:45:39 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 393
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8
Response:
Result of getDwarves:
NULL
REQUEST:
Fairmutex
Response Header:
HTTP/1.1 200 OK
Date: Sat, 01 Mar 2014 00:45:39 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 392
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8
Response:
Result of greetUser:
NULL