php实现webservice实例

yizhihongxing

1. 准备工作

在 php 中实现 webservice,需要先确认以下几点:

  • 确认 php 版本支持 SoapClient 模块。可以通过 phpinfo() 函数检查。
  • 编写 wsdl 文件,定义 webservice 的函数、参数和返回值等信息。

2. 创建 wsdl 文件

创建 webservice 所需的 wsdl 文件需要遵循 WSDL(Web Services Description Language)规范。wsdl 文件的内容很重要,它告诉调用方有哪些接口可用,接口的输入输出参数是什么。

示例 wsdl 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.com/stockquote.wsdl" targetNamespace="http://example.com/stockquote.wsdl">
  <message name="GetLastTradePriceInput">
    <part name="symbol" type="xsd:string"/>
  </message>
  <message name="GetLastTradePriceOutput">
    <part name="price" type="xsd:float"/>
  </message>
  <portType name="StockQuotePortType">
    <operation name="GetLastTradePrice">
      <input message="tns:GetLastTradePriceInput"/>
      <output message="tns:GetLastTradePriceOutput"/>
    </operation>
  </portType>
  <binding name="StockQuoteBinding" type="tns:StockQuotePortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="GetLastTradePrice">
      <soap:operation soapAction="http://example.com/GetLastTradePrice"/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="StockQuoteService">
    <port name="StockQuotePort" binding="tns:StockQuoteBinding">
      <soap:address location="http://example.com/stockquote"/>
    </port>
  </service>
</definitions>

其中:

  • message 是方法的输入参数和输出参数。
  • portType 定义WebService的接口。operation指定了方法的细节,比如输入、输出类型等。
  • binding 指向并描述了特定的传输协议和消息封装方式(常用:soap:binding)。
  • service 定义 webservice 能够用哪些协议(bindings)访问,以及访问的具体位置。

3. 编写服务器端代码

php 中提供 SoapServer 类来实现服务器端代码。代码基本思路如下:

<?php
class StockQuote {
    /**
     * @param string $symbol
     * @return float
     */
    public function GetLastTradePrice($symbol) {
        // 实现具体的业务逻辑,进行数据处理
        $price = 10.0;
        return $price;
    }
}

$server = new SoapServer('stockquote.wsdl', array('soap_version' => SOAP_1_2));
$server->setClass('StockQuote');
$server->handle();

其中:

  • SoapServer() 构造函数中第一个参数指定 wsdl 文件的位置,第二个参数指定SOAP协议的版本。
  • setClass() 指定 webservice 中包装的具体类名称。
  • handle() 开始对外提供 webservice 接口,接收请求并返回结果。

4. 编写客户端代码

php 中提供 SoapClient 类来实现客户端代码。代码基本思路如下:

<?php
$client = new SoapClient("stockquote.wsdl",array(
        'location'=>'http://example.com/stockquote', 
        'uri'=>'http://example.com/stockquote'
    ));
$res = $client->GetLastTradePrice("GOOG");
echo $res;

其中:

  • SoapClient() 构造函数中第一个参数指定 wsdl 文件的位置,第二个参数指定 webservice 的端点地址和命名空间.。
  • $client->GetLastTradePrice() 表示调用webservice中的GetLastTradePrice方法,并传递"GOOG"参数。

5. 示例说明

以下示例基于一个简单的计算器 webservice,展示了服务器端代码和客户端代码。

wsdl 文件:

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://localhost/WebService/TestWebService.asmx" targetNamespace="http://localhost/WebService/TestWebService.asmx">
  <message name="AddOneInput">
    <part name="num" type="xsd:int"/>
  </message>
  <message name="AddOneOutput">
    <part name="result" type="xsd:int"/>
  </message>
  <portType name="TestWeb">
    <operation name="AddOne">
      <input message="tns:AddOneInput"/>
      <output message="tns:AddOneOutput"/>
    </operation>
  </portType>
  <binding name="TestWebSoap" type="tns:TestWeb">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="AddOne">
      <soap:operation soapAction="http://localhost/WebService/TestWebService.asmx/AddOne"/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="TestWebService">
    <port name="TestWebSoap" binding="tns:TestWebSoap">
      <soap:address location="http://localhost/WebService/TestWebService.asmx"/>
    </port>
  </service>
</definitions>

服务器端代码:

<?php
require_once('lib/nusoap.php');

function AddOne($num)
{
    $num++;
    return array("result" => $num);
}

$server = new soap_server();
$server->configureWSDL('TestWebService', 'http://localhost/WebService/TestWebService.asmx');
$server->wsdl->schemaTargetNamespace = 'http://localhost/WebService/TestWebService.asmx';
$server->register('AddOne',
    array('num'=>'xsd:int'),
    array('result'=>'xsd:int'),
    'http://localhost/WebService/TestWebService.asmx',
    'http://localhost/WebService/TestWebService.asmx#AddOne',
    'rpc',
    'encoded',
    'Add one to input parameter and return');
$server->service(isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '');
?>

客户端代码:

<?php
require_once('lib/nusoap.php');

$num = 5;

$client = new nusoap_client('http://localhost/WebService/TestWebService.asmx?wsdl', true);
$result = $client->call('AddOne', array('num'=>$num));
echo ($result["result"]);
?>

以上就是实现 PHP 中 webservice 的完整攻略,其中包括了代码的编写流程和基本思路。可以根据实际需求进行相应的修改,实现自己的 webservice 接口。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:php实现webservice实例 - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • 用PHP实现小型站点广告管理

    下面我会详细讲解“用PHP实现小型站点广告管理”的完整攻略。 什么是小型站点广告管理? 小型站点广告管理指的是在自己创建的小型网站中,自己进行广告投放、展示和管理。通过该功能,我们可以将广告资源积累起来,简化了从接收广告到发布广告的流程。 实现广告管理的技术 要实现小型站点广告管理功能,需要掌握以下技术: PHP编程技能 MySQL数据库基础 前端知识如HT…

    PHP 2023年5月23日
    00
  • PHP实现上传图片到数据库并显示输出的方法

    PHP实现上传图片到数据库并显示输出的方法,一般分为以下几个步骤: 创建上传表单,允许用户选择要上传的图片文件。 <form action="upload.php" method="post" enctype="multipart/form-data"> <input type=&…

    PHP 2023年5月26日
    00
  • PHP合并数组+与array_merge的区别分析

    PHP中合并数组有两种方式:使用+运算符和使用array_merge函数。这两种方式虽然都可以用于数组合并,但它们有一些重要的区别,下面我们就来详细讲解。 使用+运算符合并数组 使用+运算符可以将两个数组合并成一个新的数组,其中的键名为可用的整数或字符串,但如果两个数组中有相同的键名,那么后面的数组将覆盖前面的数组。 示例1: $array1 = array…

    PHP 2023年5月26日
    00
  • PHP中重启php-fpm的几种方法汇总

    下面是“PHP中重启php-fpm的几种方法汇总”的完整使用攻略,包括重启php-fpm的几种方法和两个示例。 重启php-fpm的几种方法 在PHP应用程序中,有时候需要重启php-fpm进程,以便应用程序能够重新加载配置文件或者更新代码。以下是几种重启php-fpm的方法: 方法1:使用systemctl命令 systemctl命令是Linux系统中管理…

    PHP 2023年5月12日
    00
  • php download.php实现代码 跳转到下载文件(response.redirect)

    下面是实现下载功能的完整攻略,包括两个示例说明: 1. 准备下载文件及下载页面 首先需要准备一个要下载的文件,放在服务器上的合适位置。接着在网站上创建一个下载页面,可以在下载页面上放置下载按钮或链接,方便用户点击下载。 2. 编写php下载代码 在下载页面上需要加入php下载代码,使用response.redirect跳转到下载文件,示例代码如下: <…

    PHP 2023年5月27日
    00
  • PHP改进计算字符串相似度的函数similar_text()、levenshtein()

    一、介绍 在PHP编程中,经常会涉及到比较两个字符串的相似程度,例如搜索引擎的关键词匹配,为此,PHP提供了两个用于计算字符串相似度的函数——similar_text()和levenshtein()。 similar_text()函数通过计算两个字符串相同字符的数量来确定它们之间的相似度百分比。 levenshtein()函数通过计算将一个字符串转换成另一个…

    PHP 2023年5月26日
    00
  • php部分常见问题总结

    下面我来详细讲解“PHP部分常见问题总结”的完整攻略,总结内容包括以下几部分: 1. PHP安装 PHP是一个跨平台的脚本语言,可在Windows、Linux等不同操作系统中运行,下面介绍PHP在常见操作系统中的安装方式。 1.1 Windows平台下的PHP安装 下载PHP压缩包 PHP官方提供了Windows平台下的PHP安装包,你可以从PHP官网的下载…

    PHP 2023年5月26日
    00
  • php reset() 函数指针指向数组中的第一个元素并输出实例代码

    当我们处理数组时,经常要在数组元素之间移动指针。在PHP中,数组指针表示数组中当前指向元素的位置。PHP中的reset()函数用于将数组指针指向数组中的第一个元素。本文将详细介绍PHP reset()函数,并提供一些示例来说明如何在实际编码中使用该函数。 reset()函数的语法 reset()函数的语法如下: reset(array $array): mi…

    PHP 2023年5月26日
    00
合作推广
合作推广
分享本页
返回顶部