cSOAP Implementation Guide $Revision: 1.2 $
FerhatAyaz
2004csoap
Introduction
This document gives you information about how to use the cSOAP API in your applications.
Implementing a simple SOAP Client
A simple SOAP client
/* FILE: simpleclient.c */
static const char *url = "http://csoap.sourceforge.net/cgi-bin/csoapserver";
static const char *urn = "urn:examples";
static const char *method = "sayHello";
int main(int argc, char *argv[])
{
SoapEnv *env, *res;
env = soap_env_new_with_method(urn, method);
soap_env_add_item(env, "xsd:string",
"name", "Jonny B. Good");
res = soap_client_invoke(env, url , "");
soap_xml_doc_print(res->root->doc);
soap_env_free(res);
soap_env_free(env);
return 0;
}
First we create a SOAP envelope (SoapEnv) using the
soap_env_new_with_method(). This creates the
following SOAP envelope.
Generated SOAP envelope
]]>
soap_env_add_item() will add a xml parameter to the envelope like follows
Added "name" parameter
Jonny B. Good
]]>
Now we can send our soap call using soap_client_invoke(). The third argument
is the SoapAction header value. The result is also a SOAP envelope.
soap_client_invoke() will return always a SoapEnv object. It is
the result SOAP envelope or a fault object.
res->root is always the root xml element of the result SOAP envelope
and res->root->doc points always to the xmlDocPtr.
soap_xml_doc_print() is a helper function to print out a xml document.
Free both envelope objects
Implementing a simple SOAP Server
A simple SOAP server
/* FILE: simpleserver.c */
int main(int argc, char *argv[])
{
SoapRouter *router;
if (!soap_server_init_args(argc, argv)) {
return 0;
}
router = soap_router_new();
soap_router_register_service(router, say_hello, "sayHello", "urn:examples");
soap_server_register_router(router, "/csoapserver");
soap_server_run();
soap_router_free(router);
soap_server_destroy();
return 0;
}
Init server with commandline arguments. The init argument at this time is only
-NHTTPport <portnum> .
Create a router
Register the service function say_hello under the methodname sayHello
a
and urn urn:examples
at the router object router.
Register the router under the context /csoapserver
.
Now your service is avaiable at the address http://localhost:port/csoapserver
.
Default http port is 10000
Enter the http server loop. after calling soap_server_run() ,
the application will enter the http server (nanohttp) loop.
Free the router.
Destroy and free soap server stuff.
Compiling SOAP applications
csoap has pkg-config support. So you can can use
pkg-config with libcsoap
parameter.
An example Makefile
Building XML tree using libxml2 API
One of the ways building a xml tree into an
SOAP envelope is to use directly the libxml2 API.
You can obtain the xmlNodePtr of an envelope
with the SoapEnv structure.
typedef struct _SoapEnv
{
xmlNodePtr root;
xmlNodePtr cur;
}SoapEnv;
Here is "root" your xml node to <SOAP-ENV:Envelope>.
Building XML tree using csoap
You can build a xml tree using following functions
xmlNodePtr
soap_env_add_item(SoapEnv* env, const char *type,
const char *name, const char *value);
xmlNodePtr
soap_env_add_itemf(SoapEnv* env, const char *type,
const char *name, const char *value, ...);
xmlNodePtr
soap_env_push_item(SoapEnv *env, const char *type,
const char *name);
void
soap_env_pop_item(SoapEnv* env);
soap_env_add_itemf() does the same thing like soap_env_add_item()
but with a C style argument list. (Max buffer for value is 1054)
Building xml using csoap stack pattern
This will create the following xml structure
Generated SOAP envelope
09189
MyCity
12456
snowdrop
passphrase64
]]>