dsfb20227 2014-06-26 10:21 采纳率: 0%
浏览 71

如何在php中使用soap调用方法

I have created php file. It contains details about to access (wsdl)xml file and function. I need to call a function with paramaters but i can't able to call a function. In that function i need to print the value of parameters.$ve = $soap_client->sayHello($b);--->>>while executing this line am getting error couldnot connect to host(it directly passed to the catch).Anyone plz point me what mistake i did in that code. I passed the value like in the url "http://xxx.xxx.x.xx/testcode.php?method=sayHello&name=shankar" In my php file look like this testcode.php:

 <?php
   try
   {
      $soap_client=new SoapClient("HelloWorld.wsdl");
      $a = $_GET["method"];
      $b = $_GET["name"];
      echo $b;
      $ve = $soap_client->sayHello($b);
      function sayHello($b)
      {
          echo 'continue';
          echo $b;
      }
    }
    catch(SoapFault $exception)
    {
       echo $exception->getMessage();
    }
 ?>
  • 写回答

1条回答 默认 最新

  • dongshenyu4638 2014-06-26 11:18
    关注

    try set location param if not yet exist in wsdl schema:

    WSDL mode:

    $client = new SoapClient('HelloWorld.wsdl', 
                              array(
                                    'location' => 'http://xxx.xxx.x.xx/testcode.php',
                              ));
    

    Non WSDL mode:

    $client = new SoapClient(null, array(
          'location' => "http://xxx.xxx.x.xx/testcode.php",
          'uri'      => "urn://etc",
          'trace'    => 1 ));
    
    echo $client->sayHello('foo');
    

    server example for non WSDL mode:

    class Service
    {
        public function sayHello($name)
        {
            return 'Hello ' . $name;
        }
    }
    $server = new SoapServer(null, array('uri' => "urn://etc/"));
    $server->setClass("Service");
    $server->handle();
    

    and similar for wsdl..

    also you can use php-wsdl-creator for generated new wsdl data


    added:

    Full sample for WSDL mode - step by step:

    1) Create the simple example service:

    class Service
    {
        /**
         * @param string $name
         * @return string
         */
        public function sayHello($name)
        {
            return 'Hello ' . $name;
        }
    }
    $server = new SoapServer('http://example.com/wsdl-gen/'); //our auto-gen. schema
    $server->setClass("Service");
    $server->handle();
    

    2) For new generated schema, I use php-wsdl-creator (go to the next step if you already have)

    require_once 'php-wsdl/class.phpwsdl.php';
    
    $namespace = 'http://example.com/';
    $location  = 'http://example.com/server.php';
    $wsdl = PhpWsdl::CreateInstance($namespace, $location, './cache',
        array(
                '../soap-server.php' // all classes for generator
            ),
        null,
        null,
        null,
        false,
        false);
    
    //$wsdl->RunServer(); // bonus :) generated full client with documentation
    $wsdl->Optimize = false;
    $wsdl = $wsdl->CreateWsdl();
    

    3) WSDL-Schema can be:

    <wsdl:definitions xmlns:tns="http://example.com/" targetNamespace="http://example.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
        <wsdl:message name="sayHelloSoapIn">
            <wsdl:part name="name" type="s:string" />
        </wsdl:message>
        <wsdl:message name="sayHelloSoapOut">
            <wsdl:part name="return" type="s:string" />
        </wsdl:message>
        <wsdl:portType name="ServiceSoap">
            <wsdl:operation name="sayHello">
                <wsdl:input message="tns:sayHelloSoapIn" />
                <wsdl:output message="tns:sayHelloSoapOut" />
            </wsdl:operation>
        </wsdl:portType>
        <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
            <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
            <wsdl:operation name="sayHello">
                <soap:operation soapAction="http://example.com/sayHello" />
                <wsdl:input>
                    <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://example.com/" parts="name" />
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://example.com/" parts="return" />
                </wsdl:output>
            </wsdl:operation>
        </wsdl:binding>
        <wsdl:service name="Service">
            <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
                <soap:address location="http://example.com/soap-server.php" />
            </wsdl:port>
        </wsdl:service>
    </wsdl:definitions>
    

    4) Create example client for current scheme:

    $client = new SoapClient(
                      'http://example.com/wsdl-gen/', //schema
                       array(
                           'location' => 'http://test/server.php' //SOAP server addr
                           //we provide this option if the location param is not valid in scheme or not exist
                       ));
    

    5) That's all, example working with our service methods:

    $client->sayHello('reg'); //string 'Hello reg' (length=9)
    

    Documentation and instruments:

    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据