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

时间:2014-06-26 10:21:42

标签: php soap wsdl

我创建了php文件。它包含有关访问(wsdl)xml文件和函数的详细信息。我需要用参数调用函数,但我无法调用函数。在该功能中,我需要打印参数的值。 $ ve = $ soap_client-> sayHello($ b); --->>>执行此行时出现错误连接到主机(它直接传递给catch).Anyone plz指出我在该代码中做了什么错误。我在网址" http://xxx.xxx.x.xx/testcode.php?method=sayHello&name=shankar" 中传递了值 在我的php文件中看起来像这个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 个答案:

答案 0 :(得分:0)

如果wsdl架构中尚不存在,请尝试设置位置参数:

WSDL模式:

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

非WSDL模式:

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

echo $client->sayHello('foo');

非WSDL模式的服务器示例:

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

和类似的wsdl ..

您也可以使用php-wsdl-creator生成新的wsdl数据


<强>加入:

WSDL模式的完整示例 - 一步一步:

1)创建简单的示例服务:

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)对于新生成的模式,我使用php-wsdl-creator (如果已经有,请转到下一步)

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可以是:

<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)为当前方案创建示例客户端:

$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)使用我们的服务方法的所有示例:

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

文件和文书:

相关问题