我正在学习如何使用webservice
在PHP
中创建和使用SOAP
。我的client.php
文件是这样的:
require_once "lib/nusoap.php";
$client = new nusoap_client("http://localhost/ehsanashar/webservice/service.php?wsdl");
$book_name = "xyz";
$response = $client->call("price", array("name" => "$book_name"));
if ($response == null) {
echo "Nothing Found";
} else {
echo "Book Data: ". $response;
}
我的service.php
是这样的:
require_once "lib/nusoap.php";
require_once "function.php";
$server = new nusoap_server();
$server->configureWSDL('webservice', 'urn:webservice');
$server->register(
"price",
array("name" => "xsd:string"),
array("return" => "xsd:integer")
);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
requires
名为function.php
的文件是这样的:
function price($name) {
$details = array(
'abc' => 100,
'xyz' => 200
);
foreach ($details as $n => $p) {
if ($name == $n) {
$price = $p;
}
return $price;
}
}
当我运行文件client.php
时,response
为空,但它不应该是,问题出在哪里?有什么帮助吗?
答案 0 :(得分:3)
试试这个
FOR WAMP
client.php
中的
require_once ('lib/nusoap.php');
$client = new soapclient('http://localhost/SOAP/server.php');
//Call a function at server and send parameters too
//$response = $client->call('get_message',$param);
$param = array( 'name' => 'xyz');
$response = $client->call('price',$param);
//Process result
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode."</p>";
echo "String: ".$client->faultstring;
}
else
{
echo $response;
}
并在server.php
<?php
//call library
require_once ('lib/nusoap.php');
//using soap_server to create server object
$server = new soap_server;
//register a function that works on server
$server->register('get_message');
$server->register('price');
// create the function
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;
}
function price($name){
if(!$name){
return new soap_fault('Client','','Put Book name!');
}
$details=array(
'abc' => 100,
'xyz' => 200
);
foreach($details as $n => $p){
if($name == $n){
$price = $p;
}
}
return "price is ". $price;
}
// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
FOR XAMP
server.php
中的
<?php
//call library
require_once ('lib/nusoap.php');
$URL = "www.your-url.com";
$namespace = $URL . '?wsdl';
//using soap_server to create server object
$server = new soap_server;
$server->configureWSDL('pricetesting', $namespace);
//register a function that works on server
$server->register('hello');
// create the function
function price($name){
if(!$name){
return new soap_fault('Client','','Put Book name!');
}
$details=array(
'abc' => 100,
'xyz' => 200
);
foreach($details as $n => $p){
if($name == $n){
$price = $p;
}
}
return "price is ". $price;
}
// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
client.php
中的
<?php
require_once ('lib/nusoap.php');
$client = new soapclient("http://localhost/soap/server.php?wsdl");
//Call a function at server and send parameters too
//$response = $client->call('get_message',$param);
$param = array( 'name' => 'abc');
$response = $client->call('price',$param);
//Process result
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode."</p>";
echo "String: ".$client->faultstring;
}
else
{
echo $response;
}
?>