Skip to content

Instantly share code, notes, and snippets.

@pawitp
Created April 6, 2014 06:21
Show Gist options
  • Save pawitp/10002197 to your computer and use it in GitHub Desktop.
Save pawitp/10002197 to your computer and use it in GitHub Desktop.
Example for array/struct with nuSOAP
<?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' => 'testuser@example.com'
);
} 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'));
?>
@jcunhamt
Copy link

Good nigth pawitp
Do you know this error "Warning: strpos() expects parameter 1 to be string" ?
Do you know how to resolve?

@mermamerma
Copy link

Excellent! I served very helpful. Thank you

@mermamerma
Copy link

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

@wuelcas
Copy link

wuelcas commented Feb 20, 2016

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')
        )
)

@luisbd
Copy link

luisbd commented Apr 22, 2017

Thanks @wuelcas, that's exactly what I was looking for!

@antorofe
Copy link

Thanks!!

@eloquentpixel
Copy link

eloquentpixel commented Jul 10, 2018

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!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment