PHP SOAP

PHP SOAP 使用

什么是 SOAP

SOAP是简单对象访问协议的首字母缩写。它是一种基于XML的消息传递协议,用于在计算机之间交换信息。SOAP是XML规范的一个应用程序。
SOAP是一种旨在通过Internet进行通信的通信协议。

  • SOAP可以扩展HTTP以进行XML消息传递。

  • SOAP为Web服务提供数据传输。

  • SOAP可以交换完整的文档或调用远程过程。

  • SOAP可用于广播消息。

  • SOAP与平台和语言无关。

  • SOAP是定义发送信息的方式为XML方式。

  • SOAP使客户端应用程序能够轻松连接到远程服务并调用远程方法。

SOAP 服务端

server.php

<?php

class server
{
    public function __construct()
    {

    }

   //用于登陆验证
    public static function authenticate($header_params)
    {
        if($header_params->user_name =='jc91715'&&$header_params->password=='jc91715') return true;

        else throw new SoapFault('Wrong user/pass',401);
    }
    //客户端将要调用的方法
    public function getStudentName($id_array)
    {
        //$id_array 可拿到参数去请求数据库
        return 'hello world';
    }
}
$params = array('uri'=>'soap/server.php');
$server = new SoapServer(null,$params);
$server->setClass('server');
$server->handle();

SOAP 客户端

client.php

<?php

class client
{
    private $instance;

    public function __construct()
    {
        $params = array('location'=>'http://soap.work/server.php','uri'=>'soap.work/server.php','trace'=>true);
        $this->instance = new SoapClient(null,$params);

        //authenticate 验证
        $auth_params = new stdClass();
        $auth_params->user_name = 'jc91715';
        $auth_params->password = 'jc9171';
        $head_params = new SoapVar($auth_params,SOAP_ENC_OBJECT);
        $header = new SoapHeader('codev','authenticate',$head_params,false);
        $this->instance->__setSoapHeaders(array($header));

    }

    public function getName($id_array)
    {
        return $this->instance->__soapCall('getStudentName',$id_array);
    }
}

$client = new client();

使用

service.php

<?php

include './client.php';

$id_array = array('id'=>1);

echo $client->getName($id_array);

访问 http://soap.work/service.php soap.work 替换为你自己的虚拟域名
file

了解过后,有一个laravel soap包,可供参考

本作品采用《CC 协议》,转载必须注明作者和本文链接
Make everything simple instead of making difficulties as simple as possible
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

这个没用过,现在都 json 了

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!