【发布时间】:2016-06-03 10:56:12
【问题描述】:
我已经编写了获取 Web 服务的 php 代码 我的想法是,如果我给书名,Web 服务应该返回我编写的代码的书价
service.php
<?php
include 'lib/nusoap.php'; //load the library file
include 'lib/fun.php';
$server=new nusoap_server(); //create instance to the class
$server->configureWSDL("demo","urn:demo"); //it will accept the two parameters one name of the webservice,two namespace of the web service
$server->register(
"price", //name of function
array("name"=>'xsd:string'), //inputs
array("return"=>"xsd:integer") //outputs
);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA :'';
$server->service($HTTP_RAW_POST_DATA);
?>
client.php
<?php
require 'lib/nusoap.php';
$client=new nusoap_client("http://localhost/demo/service.php?wsdl");
$book_name ="abc";
$price = $client->call('price',array("name"=>"$book_name"));
if(empty($price))
echo "book data not available";
else
echo $price;
?>
fun.php
<?php
function price($name){
$details=array(
'abc'=>100,
'xyz'=>200
);
foreach($details as $n=>$p)
{
if($name==$n)
$price=$p;
}
return $price;
echo json_encode($price);
}
?>
当我运行这段代码时它工作正常,如果我在 soaclient 上检查这个,当我给出书名时
name: abc
it returning the value :100
当我给出书名时,我需要这个 json 格式的输出。需要更改哪些代码,请帮助我
【问题讨论】:
-
return结束当前方法/函数,因此您的 echo 永远不会运行。它也永远不会做超过一次循环的迭代。