-
-
Save pawitp/10002197 to your computer and use it in GitHub Desktop.
<?php | |
date_default_timezone_set('Asia/Bangkok'); | |
require_once "lib/nusoap.php"; | |
// Create SOAP Server | |
$server = new soap_server(); | |
$server->configureWSDL("Test_Service", "http://www.example.com/test_service"); | |
// Example "hello" function | |
function hello($username) { | |
if ($username == 'admin') { | |
return "Welcome back, Boss"; | |
} else { | |
return "Hello, $username"; | |
} | |
} | |
$server->register("hello", | |
array("username" => "xsd:string"), | |
array("return" => "xsd:string"), | |
"http://www.example.com", | |
"", | |
"", | |
"", | |
"say hi to the caller" | |
); | |
// Example "intCount" function (return array) | |
function intCount($from, $to) { | |
$out = array(); | |
for ($i = $from; $i <= $to; $i++) { | |
$out[] = $i; | |
} | |
return $out; | |
} | |
$server->wsdl->addComplexType( | |
'intArray', | |
'complexType', | |
'array', | |
'', | |
'SOAP-ENC:Array', | |
array(), | |
array( | |
array( | |
'ref' => 'SOAP-ENC:arrayType', | |
'wsdl:arrayType' => 'xsd:integer[]' | |
) | |
), | |
'xsd:integer' | |
); | |
$server->register("intCount", | |
array("from" => "xsd:integer", "to" => "xsd:integer"), | |
array("return" => "tns:intArray"), | |
"http://www.example.com", | |
"", | |
"", | |
"", | |
"count from 'from' to 'to'" | |
); | |
// Example "getUserInfo" function (return struct and fault) | |
function getUserInfo($userId) { | |
if ($userId == 1) { | |
return array( | |
'id' => 1, | |
'username' => 'testuser', | |
'email' => '[email protected]' | |
); | |
} else { | |
return new soap_fault('SOAP-ENV:Server', '', 'Requested user not found', ''); | |
} | |
} | |
$server->wsdl->addComplexType( | |
'userInfo', | |
'complextType', | |
'struct', | |
'sequence', | |
'', | |
array( | |
'id' => array('name' => 'id', 'type' => 'xsd:integer'), | |
'username' => array('name' => 'username', 'type' => 'xsd:string'), | |
'email' => array('name' => 'email', 'type' => 'xsd:string') | |
) | |
); | |
$server->register("getUserInfo", | |
array("userId" => "xsd:integer"), | |
array("return" => "tns:userInfo"), | |
"http://www.example.com", | |
"", | |
"", | |
"", | |
"get info for user" | |
); | |
// Run service | |
$server->service(file_get_contents('php://input')); | |
?> |
Friend, I have already a week trying to return multiple records in a table. Example userInfo type. You know how it would do? Thank you
Thanks, just what I needed for my array. FYI if someone needs to return multiple rows from a table, you can customize the elements parameter. For example:
array(
array(
'id' => array('name' => 'id', 'type' => 'xsd:integer'),
'username' => array('name' => 'username', 'type' => 'xsd:string'),
'email' => array('name' => 'email', 'type' => 'xsd:string')
)
)
Thanks @wuelcas, that's exactly what I was looking for!
Thanks!!
Thank you very much, just a question... by default, the xsi:type is shown in my response, what if I don't want to show it??? is it possible??? as an example, my soap server generates a response like this:
<operacion xsi:type="tns:operacion">
<codigoOperacion xsi:type="xsd:string">1010</codigoOperacion>
<horaOperacion xsi:type="xsd:string">141545</horaOperacion>
</operacion>
where operacion is a complex type I declared... but I need the response like this
<operacion>
<codigoOperacion>1010</codigoOperacion>
<horaOperacion>141545</horaOperacion>
</operacion>
thank you very much for any help!!!
Excellent! I served very helpful. Thank you