PHP SOAP 使用方式,基础理解

SOAP SWLD 模式:

服务端:
server.php

class testA
{
    function greet($param)
    {
        $value  = 'Hello ' . $param->name;
        $result = [
            'greetReturn' => $value
        ];
        return $result;
    }
}
$server = new \SoapServer('wsdl.wsdl', array('trace' => 1));
$server->setClass('testA');
$server->handle();
// php -S 192.168.0.242:12312 server.php 可以使用PHP内置服务器,开启监听服务

客户端:
client.php

    $client = new \SoapClient('wsdl.wsdl', array('trace' => 1));
    
    //调用方法
    $result = $client->__call('greet', [
        [
            'name' => 'Suhua'
        ]
    ]);

    var_dump($result);

wsdl:

    <wsdl:definitions
        xmlns:impl='http://localhost/php-soap/wsdl/helloService'
        xmlns:intf='http://localhost/php-soap/wsdl/helloService'
        xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
        xmlns:wsdlsoap='http://schemas.xmlsoap.org/wsdl/soap/'
        xmlns:xsd='http://www.w3.org/2001/XMLSchema'
        targetNamespace='http://localhost/php-soap/wsdl/helloService'>
    <wsdl:types>
        <schema elementFormDefault='qualified'
                xmlns:impl='http://localhost/php-soap/wsdl/helloService'
                xmlns:intf='http://localhost/php-soap/wsdl/helloService'
                xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
                xmlns="http://www.w3.org/2001/XMLSchema"
                targetNamespace='http://localhost/php-soap/wsdl/helloService' >
            <element name='greet'>
                <complexType>
                    <sequence>
                        <element name='name' type='xsd:string' />
                    </sequence>
                </complexType>
            </element>
            <element name='greetResponse'>
                <complexType>
                    <sequence>
                        <element name='greetReturn' type='xsd:string' />
                    </sequence>
                </complexType>
            </element>
        </schema>
    </wsdl:types>
    <wsdl:message name='greetRequest'>
        <wsdl:part name='parameters' element='impl:greet' />
    </wsdl:message>
    <wsdl:message name='greetResponse'>
        <wsdl:part name='parameters' element='impl:greetResponse' />
    </wsdl:message>
    <wsdl:portType name='helloService'>
        <wsdl:operation name='greet'>
            <wsdl:input name='greetRequest' message='impl:greetRequest' />
            <wsdl:output name='greetResponse' message='impl:greetResponse' />
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name='helloServiceSoapBinding' type='impl:helloService'>
        <wsdlsoap:binding transport='http://schemas.xmlsoap.org/soap/http' style='document' />
        <wsdl:operation name='greet'>
            <wsdlsoap:operation soapAction='helloService#greet' />
            <wsdl:input name='greetRequest'>
                <wsdlsoap:body use='literal' />
            </wsdl:input>
            <wsdl:output name='greetResponse'>
                <wsdlsoap:body use='literal' />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name='helloService'>
        <wsdl:port binding='impl:helloServiceSoapBinding' name='helloService'>
            <wsdlsoap:address location='http://192.168.0.242:12312' />
        </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>

NON-WSDL 模式

服务端:
server.php

    class testA
{
    public function sayHi($str)
    {
        return 'hi,' . $str;
    }

    public function add($a, $b)
    {
        return $a + $b;
    }
}

$ss = new \SoapServer(null, array('uri' => 'sampleA'));


$ss->setClass('testA');
$ss->handle();

客户端:
client:

    $client = new \SoapClient(null, array(
        'location'=>'http://192.168.0.242:12312',
        'uri'=>'sampleA'
    ));
    
    echo $client->sayHi('Taylor,Swift');
    echo "<br/>";
    echo $client->add(1,2);

小提示:

  1. 服务端可以使用php内置的服务尝试运行例子,php -S 192.168.0.242:12312 server.php
  2. trace 跟踪模式尽量开启,不然的话 __getLastRequest 这等函数获取不到数据
  3. 增加扩展后,默认WSDL会缓存,调试情况下关闭,避免很多奇怪问题 php.ini -> soap.wsdl_cache_enabled=0 默认 1 开启, 0 关闭
  4. 如果WSDL客户端调用的时候没有响应值的话应该是WSDL文件里不符合要求限制了
  5. NON-SWLD 模式下 location 和 uri 应和服务器保持一致,要不然会报错
  6. 通过学习认为这是一种RPC的模式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值