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:

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路