Easiest way to create SOAP API using PHP.
SERVER.PHP
require_once('lib/nusoap.php');
include('function.php');
$server = new nusoap_server;
$server->configureWSDL('server', 'urn:server');
$server->wsdl->schemaTargetNamespace = 'urn:server';
//SOAP complex type return type (an array/struct)
$server->wsdl->addComplexType(
'Person',
'complexType',
'struct',
'all',
'',
array(
'id_user' => array('name' => 'id_user', 'type' => 'xsd:int'),
'fullname' => array('name' => 'fullname', 'type' => 'xsd:string'),
'email' => array('name' => 'email', 'type' => 'xsd:string'),
'level' => array('name' => 'level', 'type' => 'xsd:int')
)
);
//first simple function
$server->register('hello',
array('username' => 'xsd:string'), //parameter
array('return' => 'xsd:string'), //output
'urn:server', //namespace
'urn:server#helloServer', //soapaction
'rpc', // style
'encoded', // use
'Just say hello'); //description
//this is the second webservice entry point/function
$server->register('login',
array('username' => 'xsd:string', 'password'=>'xsd:string'), //parameters
array('return' => 'tns:Person'), //output
'urn:server', //namespace
'urn:server#loginServer', //soapaction
'rpc', // style
'encoded', // use
'Check user login'); //description
//SOAP complex type return type (an array/struct)
$server->wsdl->addComplexType(
'asstele',
'complexType',
'struct',
'all',
'',
array(
'cnt' => array('name' => 'cnt', 'type' => 'xsd:integer'),
'cStat' => array('name' => 'cStat', 'type' => 'xsd:integer')
)
);
//this is the second webservice entry point/function
$server->register('assigntele',
array('branch' => 'xsd:integer'), //parameters
array('return' => 'tns:asstele'), //output
'urn:server', //namespace
'urn:server#assignteleServer', //soapaction
'rpc', // style
'encoded', // use
'data assign tele'); //description
//$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
//$server->service($HTTP_RAW_POST_DATA);
$server->service(file_get_contents("php://input"));
?>
FUNCTION.PHP
//first function implementation
function hello($username) {
return 'Howdy, '.$username.'!';
}
//second function implementation
function login($username, $password) {
//should do some database query here
// .... ..... ..... .....
//just some dummy result
return array(
'id_user'=>1,
'fullname'=>'John Reese',
'email'=>'john@reese.com' ,
'level'=>99
);
}
function assigntele($branch){
$conn = new mysqli('host','user','password','database');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
$var=array();
$result = $conn->query("select count(lm.mobile) as cnt from lead_master lm where lm.branch=".$branch);
while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
return $var;
}
?>