php 访问 Webservice (基于linux c/c++ gSOAP )

准备:

1.安装gSOAP库

2.参考网友资料生成web服务

一(通过端口访问web服务)

1)将安装好的gSOAP目录下的 stdsoap2.c 、stdsoap2.cpp、 stdsoap2.h、soapcpp2、wsdl2h文件拷到服务器目录下(地址自定义)

2)定义头文件(Standalone.h)

<span style="font-size:18px;">//gsoap ns service name:    add Simple StandaloneAdd service
//gsoap ns service style:    rpc
//gsoap ns service enconding:    encoded
//gsoap ns service namespace:    http://10.75.58.107/StandaloneAdd.wsdl
//gsoap ns service location:    http://10.75.58.107:18888

//gsoap ns schema namesapce:    urn:add

int ns__add(double a, double b, double *result);
</span>

头文件主要定义了服务的接口,注释部分不能缺少,这是定义即将生成的wsdl文件相关的属性(包括命名空间,服务所在位置,服务传送的方式等)


3)执行./soapcpp2 -c Standalone.h 生成与web服务相关的文件(命名空间,wsdl文件等,-c生成c文件)

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<definitions name="add"
 targetNamespace="http://10.75.58.107/StandaloneAdd.wsdl"
 xmlns:tns="http://10.75.58.107/StandaloneAdd.wsdl"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:ns="http://10.75.58.107/StandaloneAdd.wsdl"
 xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:HTTP="http://schemas.xmlsoap.org/wsdl/http/"
 xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/"
 xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/"
 xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>

 <schema targetNamespace="http://10.75.58.107/StandaloneAdd.wsdl"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ns="http://10.75.58.107/StandaloneAdd.wsdl"
  xmlns="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="unqualified"
  attributeFormDefault="unqualified">
  <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
  <!-- operation request element -->
  <element name="a" type="xsd:double"/>
  <!-- operation request element -->
  <element name="b" type="xsd:double"/>
  <!-- operation response element -->
  <element name="result" type="xsd:double"/>
 </schema>

</types>

<message name="addRequest">
 <part name="a" element="ns:a"/><!-- ns__add::a -->
 <part name="b" element="ns:b"/><!-- ns__add::b -->
</message>

<message name="addResponse">
 <part name="result" element="ns:result"/><!-- ns__add::result -->
</message>

<portType name="addPortType">
 <operation name="add">
  <documentation>Service definition of function ns__add</documentation>
  <input message="tns:addRequest"/>
  <output message="tns:addResponse"/>
 </operation>
</portType>

<binding name="add" type="tns:addPortType">
 <SOAP:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
 <operation name="add">
  <SOAP:operation style="rpc" soapAction=""/>
  <input>
     <SOAP:body use="literal" namespace="http://10.75.58.107/StandaloneAdd.wsdl"/>
  </input>
  <output>
     <SOAP:body use="literal" namespace="http://10.75.58.107/StandaloneAdd.wsdl"/>
  </output>
 </operation>
</binding>

<service name="add">
 <documentation>Simple StandaloneAdd service</documentation>
 <port name="add" binding="tns:add">
  <SOAP:address location="http://10.75.58.107:18888"/>
 </port>
</service>

</definitions>
</span>


4)定义服务端文件(StandaloneAdd.c 实现头文件中的接口,并借助gSOAP 库函数处理特定的SOAP数据结构

<span style="font-size:18px;">#include "soapH.h"
#include "add.nsmap"

int http_get(struct soap*soap) 
{ 
        FILE*fd = NULL;
        fd = fopen("add.wsdl", "rb"); //open WSDL file to copy
        if (!fd)
        {
                return 404; //return HTTP not found error
        }
        soap->http_content = "text/xml";  //HTTP header with text /xml content
        soap_response(soap,SOAP_FILE);
        for(;;)
        {
                size_t r = fread(soap->tmpbuf,1, sizeof(soap->tmpbuf), fd);
                if (!r)
                {
                        break;
                }
                if (soap_send_raw(soap, soap->tmpbuf, r))
                {
                        break; //cannot send, but little we can do about that
                }
        }
        fclose(fd);
        soap_end_send(soap);
        return SOAP_OK; 
} 

int main()
{
    struct soap soap;
    int master, slave;
    soap_init(&soap);
    soap.fget = http_get;
    master = soap_bind(&soap, "10.75.58.107", 18888, 100); 
    if(master < 0)
        soap_print_fault(&soap, stderr);
    else
    {
        //sprintf(stderr, "Socket connection successful:master socket = %d\n", master);
    	printf("Socket connection successful\n");
    while(1)
    {
        slave = soap_accept(&soap);
        if(slave < 0)
        {
            soap_print_fault(&soap, stderr);
            break;
        }
        //sprintf(stderr, "accepted connection from IP=%d.%d.%d.%d socket=%d",(soap.ip >> 24)&0xFF, (soap.ip >> 16)&0xFF, (soap.ip >> 8)&0xFF, soap.ip&0xFF, slave);
        if(soap_serve(&soap) != SOAP_OK)
            soap_print_fault(&soap, stderr);
        fprintf(stderr, "request served\n");
        soap_destroy(&soap);
        soap_end(&soap);
    }
    }
    soap_done(&soap);
}

int ns__add(struct soap *soap, double a, double b, double *result)
{
    *result = a + b;
    return SOAP_OK;
}
</span>
<span style="font-size:18px;">
</span>


5)定义客户端(ClientTest.c)

<span style="font-size:18px;">#include "soapH.h"
#include "add.nsmap"

const char server[] = "http://10.75.58.107:18888";

int main(int argc, char **argv)
{
    struct soap soap;
    double a, b, result;
    soap_init1(&soap, SOAP_XML_INDENT);
    a = strtod(argv[2], NULL);
    b = strtod(argv[3], NULL);
    
    soap_call_ns__add(&soap, server, "", a, b, &result);
    if(soap.error)
    {
        soap_print_fault(&soap, stderr);
        exit(1);
    }
    else
        printf("result = %g\n", result);
    soap_destroy(&soap);
    soap_end(&soap);
    soap_done(&soap);
    return 0;
    
}
</span>

6)编译服务端和客户端

gcc -o StandaloneAdd StandaloneAdd.c stdsoap2.c soapC.c soapServer.c     

gcc -o ClientTest ClientTest.c stdsoap2.c soapC.c soapClient.c        


7)开启服务

./StandaloneAdd


8)客户端 连接webservice(本地远程皆可)

./ClientTest add 5 6


9)定义php客户端

<span style="font-size:18px;"><?php
	try{
		$soap = new SoapClient("http://10.75.58.107:18888/?wsdl");
		var_dump($soap);
		var_dump($soap->__getFunctions());
		echo $soap->add(1,5);                                  
	}catch(SoapFault $e){
		echo $e->getMessage();
	}catch(Exception $e){
		echo $e->getMessage();
	}
?>

</span>

(二)通过cgi访问web服务

定义头文件

<span style="font-size:18px;">// File: currentTime.h 
//gsoap ns service name: currentTime 
//gsoap ns service namespace: urn:currentTime 
//gsoap ns service location: http://10.75.58.73/currentTime.cgi 
int ns__currentTime(time_t& response);
</span>


生成wsdl文件

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<definitions name="currentTime"
 targetNamespace="urn:currentTime"
 xmlns:tns="urn:currentTime"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:ns="urn:currentTime"
 xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:HTTP="http://schemas.xmlsoap.org/wsdl/http/"
 xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/"
 xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/"
 xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>

 <schema targetNamespace="urn:currentTime"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ns="urn:currentTime"
  xmlns="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="unqualified"
  attributeFormDefault="unqualified">
  <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
  <!-- operation request element -->
  <element name="currentTime">
   <complexType>
    <sequence>
    </sequence>
   </complexType>
  </element>
  <!-- operation response element -->
  <element name="currentTimeResponse">
   <complexType>
    <sequence>
     <element name="response" type="xsd:dateTime" minOccurs="1" maxOccurs="1"/><!-- ns__currentTime::response -->
    </sequence>
   </complexType>
  </element>
 </schema>

</types>

<message name="currentTimeRequest">
 <part name="Body" element="ns:currentTime"/><!-- ns__currentTime::ns__currentTime -->
</message>

<message name="currentTimeResponse">
 <part name="Body" element="ns:currentTimeResponse"/>
</message>

<portType name="currentTimePortType">
 <operation name="currentTime">
  <documentation>Service definition of function ns__currentTime</documentation>
  <input message="tns:currentTimeRequest"/>
  <output message="tns:currentTimeResponse"/>
 </operation>
</portType>

<binding name="currentTime" type="tns:currentTimePortType">
 <SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
 <operation name="currentTime">
  <SOAP:operation soapAction=""/>
  <input>
     <SOAP:body parts="Body" use="literal"/>
  </input>
  <output>
     <SOAP:body parts="Body" use="literal"/>
  </output>
 </operation>
</binding>

<service name="currentTime">
 <documentation>gSOAP 2.8.17r generated service definition</documentation>
 <port name="currentTime" binding="tns:currentTime">
  <SOAP:address location="http://10.75.58.73/cgi-bin/currentTime.cgi"/>
 </port>
</service>

</definitions>
</span>


服务端文件

// File: currentTime.cpp 
#include "soapH.h" // include the generated declarations 
#include "currentTime.nsmap" // include the XML namespace mappings 
int main() 
{ 
   // create soap context and serve one CGI-based request: 
  return soap_serve(soap_new());
	// soap_destroy(&soap); // dealloc C++ data 
      	// soap_end(&soap);  
	// return 0;
} 
int ns__currentTime(struct soap *soap, time_t& response) 
{ 
   response = time(0); 
   return SOAP_OK; 
}


php访问

<?php

error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);


try{
   $soap = new SoapClient("http://10.75.58.73/cgi/currentTime.wsdl");
   // $soap = new SoapClient("http://10.75.58.190/webservice/add.wsdl");
	
	var_dump($soap);

	var_dump($soap->__getFunctions());
	//currentTime(currentTime $Body)
 	var_dump($soap->currentTime());
//echo $soap->add(11,15);
   
                                   
                                    
}catch(SoapFault $e){
    echo $e->getMessage();
}catch(Exception $e){
    echo $e->getMessage();
}

?>






            

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gSOAP编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现,从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多。绝大多数的C++web服务工具包提供一组API函数类库来处理特定的SOAP数据结构,这样就使得用户必须改变程序结构来适应相关的类库。与之相反,gSOAP利用编译器技术提供了一组透明化的SOAP API,并将与开发无关的SOAP实现细节相关的内容对用户隐藏起来。   gSOAP的编译器能够自动的将用户定义的本地化的C或C++数据类型转变为符合XML语法的数据结构,反之亦然。这样,只用一组简单的API就将用户从SOAP细节实现工作中解脱了出来,可以专注与应用程序逻辑的实现工作了。gSOAP编译器可以集成C/C++和Fortran代码(通过一个Fortran到C的接口),嵌入式系统,其他SOAP程序提供的实时软件的资源和信息;可以跨越多个操作系统,语言环境以及在防火墙后的不同组织。   gSOAP使编写web服务的工作最小化了。gSOAP编译器生成SOAP的代码来序列化或反序列化C/C++的数据结构。gSOAP包含一个WSDL生成器,用它   来为你的web服务生成web服务的解释。gSOAP的解释器及导入器可以使用户不需要分析web服务的细节就可以实现一个客户端或服务端程序。   下面是gSOAP的一些特点:   ×gSOAP编译器可以根据用户定义的C和C++数据结构自动生成符合SOAP的实例化代码。   ×gSOAP支持WSDL 1.1, SOAP 1.1, SOAP 1.2, SOAP RPC 编码方式以及 literal/document 方式.   ×gSOAP是少数完全支持SOAP1.1 RPC编码功能的工具包,包括多维数组及动态类型。比如,一个包含一个基类参数的远程方法可以接收客户端   传来的子类实例。子类实例通过动态绑定技术来保持一致性。   ×gSOAP 支持 MIME (SwA) 和 DIME 附件包。   ×gSOAP是唯一支持DIME附件传输的工具包。它允许你在保证XML可用性的同时能够以最快的方式(流方式)传递近乎无大小限制的二进制数据   。   ×gSOAP 支持 SOAP-over-UDP。   ×gSOAP 支持 IPv4 and IPv6.   ×gSOAP 支持 Zlib deflate and gzip compression(for HTTP, TCP/IP, and XML file storage)。   ×gSOAP 支持 SSL (HTTPS)。   ×gSOAP 支持 HTTP/1.0, HTTP/1.1 保持连接, 分块传输及基本验证。   ×gSOAP 支持 SOAP 单向消息。   ×gSOAP 包含一个 WSDL 生成器,便于web服务的发布。   ×gSOAP 包含一个WSDL解析器(将WSDL转换为gSOAP头文件),可以自动化用户客户端及服务端的开发。   ×生成可以单独运行的web服务及客户端程序。   ×因为只需要很少内存空间,所以可以运行在类似Palm OS, Symbian, Pocket PC的小型设备中。   ×适用于以C或C++开发的web服务中。   ×跨平台:Windows, Unix, Linux, Mac OS X, Pocket PC, Palm OS, Symbian等。   ×支持序列化程序中的本地化C/C++数据结构。   ×可以使用输入和输出缓冲区来提高效率,但是不用完全消息缓冲来确定HTTP消息的长度。取而代之的是一个三相序列化方法。这样,像64位   编码的图像就可以在小内存设备(如PDA)中以DIME附件或其他方式传输。   ×支持C++单继承,动态绑定,重载,指针结构(列表、树、图、循环图,定长数组,动态数组,枚举,64位2进制编码及16进制编码)。   ×不需要重写现有的C/C++应用。但是,不能用unions,指针和空指针来作为远程方法调用参数的数据结构中元素。   ×三相编组:1)分析指针,引用,循环数据结构;2)确定HTTP消息长度;3)将数据序列化位SOAP1.1编码方式或用户定义的数据编码方式。   ×双相编组:1)SOAP解释及编码;2)分解“forward”指针(例如:分解SOAP中的href属性)。   ×完整可定制的SOAP错误处理机制。   ×可定制的SOAP消息头处理机制,可以用来保持状态信息   2 gSoap2.2版与gSOAP 2.1版(或以前版本)的不同   如果你是从2.1版升级到2.2或以后版本,请注意这些变化。   为了能够分离传输、内容编码、映射中的接收/发送设置,改变了运行时选项及标志。这些标志分布再四个类中:传输(IO),内容编码(ENC   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值