我尝试使用nusoap创建PHP WebService:
<?php
require_once "lib/nusoap.php";
function doAuthenticate(){
if(isset($_SERVER['PHP_AUTH_USER']) and isset($_SERVER['PHP_AUTH_PW']) )
{
//here I am hardcoding. You can connect to your DB for user authentication.
if($_SERVER['PHP_AUTH_USER']=="111" and $_SERVER['PHP_AUTH_PW']=="222" )
return true;
else
return false;
}
}
function set_data($test_Obj)
{
if(!doAuthenticate())
return null;
if($test_Obj!=null)
return $test_Obj['firstname'];
}
function get_data($limit_start,$limit_end)
{
if(!doAuthenticate())
return null;
/* you can use mysql and your logic here this is sample array */
$array_rtr=array();
for($i=0;$i<10;$i++){
$array_rtr[$i]['id']=0;
$array_rtr[$i]['firstname']='Nikunj';
$array_rtr[$i]['lastname']='Gandhi';
$array_rtr[$i]['email']='Nik_gandhi007@yahoo.com';
}
$return=array();
for($i=$limit_start;$i<=$limit_end;$i++){
$return[$i]=$array_rtr[$i];
}
return $return;
}
$server = new soap_server();
$server->configureWSDL("productlist", "urn:productlist");
$server->soap_defencoding = 'UTF-8';
$server->decode_utf8 = false;
$server->encode_utf8 = true;
$server->wsdl->addComplexType('testObj',
'complexType',
'struct',
'all',
'',
array(
'id' => array('id' => 'id', 'type' => 'xsd:string'),
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
'lastname' => array('name' => 'lastname', 'type' => 'xsd:string'),
'email' => array('name' => 'email', 'type' => 'xsd:string') ) );
$server->wsdl->addComplexType('return_array_php',
'complexType',
'array',
'all',
'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:testObj[]') ), 'tns:testObj' );
$server->register('get_data',
array('limit_start' => 'xsd:int','limit_end' => 'xsd:int'),
array('return' => 'tns:return_array_php'),
"urn:productlist",
"urn:productlist".'#get_data', 'rpc', 'encoded', 'Returns array data in php web service' );
$server->register('set_data',
array('test_Obj' => 'tns:testObj'),
array('return' => 'xsd:string'),
"urn:productlist",
"urn:productlist".'#set_data', 'rpc', 'encoded', 'Returns array data in php web service' );
if ( !isset( $HTTP_RAW_POST_DATA ) )
$HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
$server->service($HTTP_RAW_POST_DATA);
?>
并使用.NET C#中的WS:
productlistPortTypeClient CallWebService = new productlistPortTypeClient();
CallWebService.ClientCredentials.Windows.ClientCredential.UserName = "111";
CallWebService.ClientCredentials.Windows.ClientCredential.Password = "222";
testObj[] arrayPhps = CallWebService.get_data(1, 3);
Console.WriteLine(arrayPhps[0]);
testObj arrayPhp = new testObj() { email = "dfdf", firstname = "dfdf", id = "222", lastname = "dfdf" };
string data = CallWebService.set_data(arrayPhp);
问题是身份验证始终失败!
C#客户端未设置userName和密码。为什么呢?