網上流傳的gsoap與php的通信沒有能成功的,
為實際業務自己動手調通一個,如果其它方式望告之
php client
<?php
try{
$soap = new SoapClient("http://127.0.0.1:8888/add?wsdl",array('location'=>"http://127.0.0.1:8888",'uri'=>"urn:add",'connection_timeout'=>3,));
var_dump($soap);
$p=array('num1'=>6,'num2'=>2, 'sum'=>$sum);
$result1 = $soap->add($p);
var_dump($result1);
echo $result1->sum;
}catch(SoapFault $e){
echo $e->getMessage();
}catch(Exception $e){
echo $e->getMessage();
}gsoap server
#include "soapH.h"
#include <windows.h>
#include "add.nsmap"
#define BACKLOG (100) /* Max. request backlog */
DWORD WINAPI process_request(LPVOID*);
int http_get(struct soap * soap);
int main(int argc, char **argv) {
struct soap soap;
struct soap *tsoap;
const char* fmt = "accepts socket %d connection from IP %d.%d.%d.%d\n";
soap_init(&soap);
if ( argc < 2 ) {
soap_serve(&soap);
soap_destroy(&soap);
soap_end(&soap);
}
else {
soap.send_timeout = 60;
soap.recv_timeout = 60;
soap.accept_timeout = 3600;
soap.max_keep_alive = 100;
soap.fget = http_get;
DWORD tid;
HANDLE hThread;
int port = atoi(argv[1]); // first command-line arg is port
SOAP_SOCKET m, s;
m = soap_bind(&soap, NULL, port, BACKLOG);
if ( !soap_valid_socket(m) ) exit(1);
printf("Socket connection successful %d\n", m);
for ( ; ; ) {
s = soap_accept(&soap);
if ( !soap_valid_socket(s) ) {
if ( soap.errnum ) {
soap_print_fault(&soap, stderr);
exit(1);
}
printf("server timed out\n");
break;
}
printf(fmt, s, (soap.ip>>24)&0xFF, (soap.ip>>16)&0xFF, (soap.ip>>8)&0xFF, soap.ip&0xFF);
tsoap = soap_copy(&soap); // make a safe copy
if ( !tsoap ) break;
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)process_request, tsoap, 0, &tid);
if ( hThread == NULL ) {
printf("can not create a thread for SOAP request processing.\n");
exit(-1);
}
}
}
soap_done(&soap);
return 0;
}
DWORD WINAPI process_request(LPVOID* soap) {
soap_serve((struct soap*)soap);
soap_destroy((struct soap*)soap);
soap_end((struct soap*)soap);
soap_done((struct soap*)soap);
free(soap);
return 0;
}
// 服務消息的實現代碼
int ns__add(struct soap *add_soap, int num1, int num2, int *sum) {
*sum = num1 + num2;
return SOAP_OK;
}
int http_get(struct soap * soap) {
FILE *fd = NULL;
printf("call http_get.\n");
char *s = strchr(soap->path, '?');
printf("call http_get2.\n");
if ( !s || strcmp(s, "?wsdl") ) return SOAP_GET_METHOD;
printf("call http_get3.\n");
fd = fopen("./add.wsdl", "rb");
//fd = fopen("F://soap_server//soap_server//soap_server//add.wsdl", "rb");
if ( !fd ) return 404;
printf("call http_get4.\n");
soap->http_content = "text/xml";
soap_response(soap, SOAP_FILE);
printf("call http_ge5.\n");
for ( ; ; ) {
size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
if ( !r ) break;
if ( soap_send_raw(soap, soap->tmpbuf, r) ) break;
}
fclose(fd);
soap_end_send(soap);
return SOAP_OK;
}本文出自 “djshell” 博客,轉載請與作者聯系!