-
-
Save neomadara/1c8e4efa344027462027a4641f5acc81 to your computer and use it in GitHub Desktop.
Example for array/struct with nuSOAP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment