Home Backend Development PHP Problem What does php soap mean?

What does php soap mean?

Nov 19, 2021 am 10:00 AM
php soap

Soap is based on XML and HTTP communication protocols. The way to use soap in php is to add the soap module of php, that is, add "soap.so" to php.ini.

What does php soap mean?

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

1. What does php soap mean? What are wsdl and why should we use them?

SOAP is based on XML and HTTP communication protocols, and is supported by various xml platforms and languages. http is supported by all Internet browsers and servers.

WSDL refers to Web Services Description Language (Web Services Description Language), which is a document written in XML. This document describes a Web service. It specifies the location of the service and the operations provided by this service.

I work in PHP, you work in Java, and he works in .NET. What if the three of us want to communicate and exchange data? We need a tool that can communicate with us all. soap, wsdl were created so that applications running on different operating systems and using different technologies and programming languages ​​can communicate with each other.

2. Example

If PHP wants to use soap, the usual method is to add the soap module of PHP and add soap.so in php.ini , here is a method to implement soap

//包函nusoap.php
require_once('./lib/nusoap.php');
//创建服务端
$server=newsoap_server;
//定义客户端调用方法
$server->register('hello');
//调用方法以及参数
functionhello($name) {
return'Hello, '.$name;
}
$HTTP_RAW_POST_DATA= isset($HTTP_RAW_POST_DATA) ?$HTTP_RAW_POST_DATA:'';
$server->service($HTTP_RAW_POST_DATA);
?>
Copy after login

p without adding soap.so file

nusoap is a functional file written in PHP, you can use it if you include it. There are a lot of them online, so search them out yourself.

1. Do not use wsdl

a. Server helloworld2.php

b. Client hello.php

//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap客户端,调用服务端提供的wsdl
//$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);
$client=newsoapclient('http://localhost/test/helloworld2.php');
//查看一下是不是报错
$err=$client->getError();
if($err) {
//显示错误
echo'
Constructor error
'.$err.'
';
}
//调用服务端的方法
$result=$client->call('hello',array('person'=>"this is a test"));
echo'
Result
';
print_r($result);
echo'
';
?>
Copy after login

2. Use wsld

a, Server side

//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap服务
$server=newsoap_server();
//初始化支持wsdl
$server->configureWSDL('hellowsdl2','urn:hellowsdl2');
//定义数据结构来接收数据
$server->wsdl->addComplexType(
'Person',
'complexType',
'struct',
'all',
'',
array(
'firstname'=>array('name'=>'firstname','type'=>'xsd:string'),//后面的type定义数据的类型,这个是string
'age'=>array('name'=>'age','type'=>'xsd:int'),//后面的type定义数据的类型,这个是int
'gender'=>array('name'=>'gender','type'=>'xsd:string')//后面的type定义数据的类型,这个是string
)
);
$server->wsdl->addComplexType(
'SweepstakesGreeting',
'complexType',
'struct',
'all',
'',
array(
'greeting'=>array('name'=>'greeting','type'=>'xsd:string'),
'winner'=>array('name'=>'winner','type'=>'xsd:string')
)
);
//服务器定义的soap调用方法
$server->register('hello',// 方法名字hello,方法就在下面
array('person'=>'tns:Person'),// 客户端传来的变量
array('return'=>'tns:SweepstakesGreeting'),//返回参数
'urn:hellowsdl2',// soap名
'urn:hellowsdl2#hello',// soap的方法名
'rpc',// 使用的方式
'encoded',// 编码
'test'// 存档
);
//定义上面注册过的函数hello
functionhello($person) {
$greeting='Hello, '.$person['firstname'].'. It is nice to meet a '.$person['age'].' year old '.$person['gender'].'.';
$winner='Scott';
//要返回的数据
returnarray(
'greeting'=>$greeting,
'winner'=>$winner
);
}
// 请求时(试图)调用服务
$HTTP_RAW_POST_DATA= isset($HTTP_RAW_POST_DATA) ?$HTTP_RAW_POST_DATA:'';
$server->service($HTTP_RAW_POST_DATA);
?>
Copy after login

b, Client side

//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap客户端,调用服务端提供的wsdl
//$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);
$client = new soapclient('http://localhost/test/helloworld2.php');
//查看一下是不是报错
$err = $client->getError();
if ($err) {
//显示错误
echo '
Constructor error
' . $err . '
';
}
//要向服务端要传的参数
$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');
//调用服务端的方法
$result = $client->call('hello', array('person' => $person));
//错误审核
if ($client->fault) {
echo '
Fault
';
print_r($result);
echo '
';
} else {
$err = $client->getError();
if ($err) {
echo '
Error
' . $err . '
';
} else {
echo '
Result
';
print_r($result);
echo '
';
}
}
//显示请求信息
echo '
Request
';
echo '
' . htmlspecialchars($client->request, ENT_QUOTES) . '
';
//显示返回信息
echo '
Response
';
echo '
' . htmlspecialchars($client->response, ENT_QUOTES) . '
';
//显示调试信息
echo '
Debug
';
echo '
' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '
';
?>
Copy after login

The above two examples, whether client or server, are written in PHP. You can try to use Write it in multiple languages ​​to test it out. Regardless of whether you are using PHP modules or nusoap, I won’t go into details about the specific methods here, they are all in the manual.

SOAP does not need to be introduced here. Here is just a simple example of implementing SOAP. Not much to say, let’s look at the code. Soap is divided into server and client. We need to make the client call the server's code. First, look at the short code of the server:

This is the server-side code: server.php

<?php     
//声明一个函数add() ,并返回它的值        function add($a,$b){        return $a+$b;        }    //实例化一个SoapServer对象, 并将add函数注册成为其方法        $server = new SoapServer(null,array(&#39;uri&#39;=>&#39;http://localhost/&#39;)); //指定server端代码的URI(资源标志符)        $server->addFunction("add");        $server->handle();?>
Copy after login

Then use the client-side code Code to call the server-side code: The client code is also very simple: as follows:

This is the client-side code client.php

<?php     
//建立一个参数数组,存储要访问的提供soap服务的计算机的地址与程序        $arrOptions=array(            &#39;uri&#39;=>&#39;http://localhost/&#39;,            &#39;location&#39;=>&#39;http://localhost/soap/server.php&#39;,  //注意: 这个location指定的是server端代码在服务器中的具体位置, 我的是在本地根目录下的soap目录中,            &#39;trace&#39;=>true,        );        $soapObject = new SoapClient(null,$arrOptions); //实例化客户端对象        echo $soapObject->add(20,30); //调用服务器端的函数add并返回值50?>
Copy after login

ok, it’s over!

Recommended learning : "PHP Video Tutorial"

The above is the detailed content of What does php soap mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles