PHP Webservice的发布与调用
PHP Webservice的发布与调用 1. 环境配置 配置php.ini,把php_soap.dll前面的分号去掉, 不然会报错 class soapserver not found 重启apache后通过phpinfo()查看 这样是表示环境已经支持soap的webservice了,后面的事情就是写代码了。 2. webservice的发布
PHP Webservice的发布与调用
1. 环境配置
配置php.ini,把php_soap.dll前面的分号去掉,
不然会报错
class soapserver not found
重启apache后通过phpinfo()查看
这样是表示环境已经支持soap的webservice了,后面的事情就是写代码了。
2. webservice的发布
发布出来的*.wsdl文件,其实是一个xml格式的文件,生成这个文件可以通过第3方软件,如ZendStudio 就可以生成。
示例,我用以下代码生成:
<?php class CTest { public function __construct() { } /** * * @param string $oParams * @return string */ public function Add($oParams) { $sParams = $oParams->oParams[l1] ; $oParams = json_decode($sParams); $a = $oParams->a; $b = $oParams->b; $c = $a+$b; return array('AddResult'=>$c); } } ?>
生成的xml文件是:
<?xml version="1.0" encoding="UTF-8" ?> <definitions targetnamespace="http://tempuri.org/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <types> <schema elementformdefault="qualified" targetnamespace="http://tempuri.org/"> <element name="AddRequest"> <complextype> <sequence> <element minoccurs="0" maxoccurs="1" name="oParams" type="s:string"></element> </sequence> </complextype> </element> <element name="AddResponse"> <complextype> <sequence> <element minoccurs="0" maxoccurs="1" name="AddResult" type="s:string"></element> </sequence> </complextype> </element> </schema> </types> <message name="AddSoapIn"> <part name="parameters" element="tns:AddRequest"></part> </message> <message name="AddSoapOut"> <part name="parameters" element="tns:AddResponse"></part> </message> <porttype name="CTestSoap"> <operation name="Add"> <input message="tns:AddSoapIn"> <output message="tns:AddSoapOut"></output> </operation> </porttype> <binding name="CTestSoap" type="tns:CTestSoap"> <binding transport="http://schemas.xmlsoap.org/soap/http"></binding> <operation name="Add"> <operation soapaction="http://tempuri.org/Add" style="document"></operation> <input> <output> <body use="literal"></body> </output> </operation> </binding> <binding name="CTestSoap12" type="tns:CTestSoap"> <binding transport="http://schemas.xmlsoap.org/soap/http"></binding> <operation name="Add"> <operation soapaction="http://tempuri.org/Add" style="document"></operation> <input> <output> <body use="literal"></body> </output> </operation> </binding> <service name="CTest"> <port name="CTestSoap" binding="tns:CTestSoap"> <address location="http://192.168.6.44:80/webservice/server/server.php?wsdl[l2] "></address> </port> <port name="CTestSoap12" binding="tns:CTestSoap12"> <address location="http://192.168.6.44:80/ webservice/server/server.php?wsdl[l3] "></address> </port> </service> </definitions>
生成xml文件后,就要通过一个服务发布出去,如:
<?php error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); include_once('../class/test.class.php');[l4] $server = new SoapServer('wscache/CTest.1.0.wsdl'); $server->setClass('CTest'); $server->handle(); ?>
然后可以向其他人或者系统提供出发布的地址,如:
http://192.168.6.44/webservice/server/server.php?wsdl
3. webservice的调用
通过上面提供的地址,写一个调用页面
<?php error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); header("Content-Type: text/html;charset=utf-8"); $client = new SoapClient('http://192.168.6.44/webservice/server/server.php?wsdl'); $str = '{"a":1,"b":20}'; //调用方法一 $r = $client->Add(array('oParams'=>$str)); //数组 //调用方法二 //$pParams->oParams = $str; //$r = $client->__call('Add',array($pParams));//这个得是对象 var_dump($r); ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
