Home  >  Article  >  Backend Development  >  What does php soap mean?

What does php soap mean?

藏色散人
藏色散人Original
2021-11-19 10:00:032303browse

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);
?>

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'
';
?>

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);
?>

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) . '
';
?>

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();?>

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?>

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:
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