例子:
描述:server 端提供了一个加法接口函数,Client端调用函数,输出结果。
头文件:
add.h
//gsoap ns service name: add
//gsoap ns service style: rpc
//gsoap ns service namespace: http://192.168.0.137/add.wsdl
//gsoap ns service location: http://192.168.0.137
//gsoap ns service executable: add.cgi
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn: addint ns__add(int num1,int num2,int *sum);用gsoap工具中的soapcpp2.exe生成wsdl文件wsdl文件内容如下:
<?xml version="1.0"encoding="UTF-8"?>
<definitions name="add"
 targetNamespace="http://192.168.0.137/add.wsdl"
 xmlns:tns="http://192.168.0.137/add.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="urn:"
 xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
 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:"
  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:"
  xmlns="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="unqualified"
  attributeFormDefault="unqualified">
  <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
 </schema></types><message name="addRequest">
 <part name="num1" type="xsd:int"/>
 <part name="num2" type="xsd:int"/>
</message><message name="addResponse">
 <part name="sum" type="xsd:int"/>
</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="encoded" namespace="urn:" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </input>
  <output>
     <SOAP:body use="encoded" namespace="urn:" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </output>
 </operation>
</binding>
    
<service name="add">
 <documentation>gSOAP 2.7.9k generated service definition</documentation>
 <port name="add" binding="tns:add">
  <SOAP:address location="http://192.168.0.137/add.cgi"/>
 </port>
server端是用C写的,经过C写的client端调用,没有问题。
现在要用PHP写client端:
<?php
header("Cache-Control: no-cache, must-revalidate");
$client=new SoapClient("/home/php/add.wsdl");
$functions=$client->__getfunctions();
print_r($functions);
$num1=2009;
$num2=2010;
$paramenters=array($num1,$num2);
try {
     $getresult=$client->__soapcall('add',$paramenters);
     echo("$num1 + $num2 = $getresult\n");
}catch(SoapFault $fault){
     Echo "Fault! code:",$fault->faultcode,"stiring:\n",$fault->faultstring;
}
?>
出错了:Array
(
    [0] => int add(int $num1, int $num2)
)
Fault! code:HTTPstiring:
Could not connect to host请问各位高手,哪里有问题?帮帮忙,谢谢!
在同一台机器上用C写的client端调用成功。
用gsoap生成的wsdl文件还需要修改么?
如果不用wsdl文件,那个uri是什么?比如我在192.168.0.137上的12345端口提供服务

解决方案 »

  1.   

    提示错误:
    Fault! code:HTTPstiring: 
    Could not connect to host 
    $client=new SoapClient("/home/php/add.wsdl"); 
    改成绝对路径试试!~ 另外给点个人参考意见:
    我没有调用过c的service 但是调用.net 的webservice 和调用php的webservice 获取值的实现方式是不一样的。