Home Backend Development PHP Tutorial NuSOAP Tutorial_PHP Tutorial

NuSOAP Tutorial_PHP Tutorial

Jul 13, 2016 pm 05:35 PM
Function and how Install Example supply Tutorial document of illustrate this

这个文档描述了如何取得和安装 NuSOAP,然后提供一些实例来说明 NuSOAP 的功能,这并不是一个全面的 NuSOAP 的介绍,但是希望能够然一些 PHP 开发者可以有一个很好的入门。

NuSOAP 是一组 PHP 类,它让开发者可以创建和使用 SOAP web services。它不需要安装任何的 PHP 扩展。它是在2004年12月3日被开发,当前的版本是 NuSOAP(0.6.7) 。支持 SOAP 1.1 规范,它能够生产 WSDL 1.1 ,当然也可以使用它,同时也支持 rpc/encoded and document/literal service。但是,必须注意 NuSOAP 没有像 .NET 和 Apache Axis 那样提供完全的实现。

Hello, World
我会以 "Hello, World" 为实例做开始,编写基本的 NuSOAP 客户端和服务器端的代码。

我们先从服务器端开始,应为没有服务器端,有客户端也是没有意义的。我们将编写一个带有单个参数并返回一个字符串,名叫 Hello 的 SOAP 方法,希望代码中的注释能够提供有效的说明。

// Pull in the NuSOAP code
require_once(nusoap.php);
// Create the server instance
$server = new soap_server;
// Register the method to expose
$server->register(hello);
// Define the method as a PHP function
function hello($name) {
    return Hello, . $name;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ;
$server->service($HTTP_RAW_POST_DATA);
?>

以下是客户端的代码,有一些重要的事情需要注意:首先,当创建实例 soapclient 时,需要指定一个 service 的 URL 为参数,在这个实例中,helloworld.php 从 http://localhost/phphack 访问的。当然,你要使用的 services 放在不同的 URL;第二,当调用service 时,第一个参数是 service 的名字,必须要匹配有效的方法名(有的服务器是大小写敏感的)。在这个实例,他必须匹配在 helloworld.php 中已经注册了的方法。最后,第二个参数是一个数组,它将是传递给 SOAP service 方法作为参数。既然 helloworld.php 中的方法 hello 只有一个参数,那么数组就只有一个元素。

// Pull in the NuSOAP code
require_once(nusoap.php);
// Create the client instance
$client = new soapclient(http://localhost/phphack/helloworld.php);
// Call the SOAP method
$result = $client->call(hello, array(name => Scott));
// Display the result
print_r($result);
?>

Debugging
编程时,当有问题出现的时候你都需要调试。NuSOAP 提供了一组工具来帮助你做这个工作。NuSOAP 调试的时候需要查看的信息是发送的请求信息和返回的相应信息。NuSOAP 的客户端类允许你通过它的两个成员来查看这些信息。例如,这里是显示请求和响应的 helloworldclient.php 的修改版。在下一部分我会回顾显示在客户端代码的请求和响应信息。

// Pull in the NuSOAP code
require_once(nusoap.php);
// Create the client instance
$client = new soapclient(http://localhost/phphack/helloworld.php);
// Call the SOAP method
$result = $client->call(hello, array(name => Scott));
// Display the result
print_r($result);
// Display the request and response
echo

Request

;
echo
 . htmlspecialchars($client->request, ENT_QUOTES) . 
;
echo

Response

;
echo
 . htmlspecialchars($client->response, ENT_QUOTES) . 
;
?>

NuSOAP 也提供了一个方法使用它的类就可以通过日志来查看调试信息。加入以下的代码将会显示冗长的调试信息。不幸的是输出的说明必须留给读者。

// Display the debug messages
echo

Debug

;
echo
 . htmlspecialchars($client->debug_str, ENT_QUOTES) . 
;

服务器端能够提供相似的调试信息,有趣的是,这些调试信息是在SOAP 的相应的末尾以 xml 格式显示,因此它可以在客户端中查看到。服务器端的调试看起来像这样:

// Pull in the NuSOAP code
require_once(nusoap.php);
// Enable debugging *before* creating server instance
$debug = 1;
// Create the server instance
$server = new soap_server;
// Register the method to expose
$server->register(hello);
// Define the method as a PHP function
function hello($name) {
return Hello, . $name;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($ HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ;
$server->service($HTTP_RAW_POST_DATA);
?>

The third method of debugging is not really debugging, it is good programming practice. The above example does not do error checking when calling SOAP. A more robust client would look like this:

// Pull in the NuSOAP code
require_once(nusoap.php);
// Create the client instance
$client = new soapclient(http://localhost /phphack/helloworld.php);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo

Constructor error: . $err .

;
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$result = $client->call(hello, array(name => Scott));
// Check for a fault
if ($client ->fault) {
echo

Fault: ;
print_r($result);
echo

;
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo

< ;b>Error: . $err .

;
} else {
        // Display the result
        print_r($result);
  }
}
?>

In order to test the code, you need to cause an error to occur, for example, change the called method name hello to goodbye.

Request and Response
I have shown how easy it is to display SOAP request and response information in the example above, here is the request information for hello2client.php:

POST /phphack/helloworld2.php HTTP/1.0
Host: localhost
User-Agent: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859 -1
SOAPAction: ""
Content-Length: 538


SOAP-ENV:encodingStyle="http://schemas. xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3. org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/ soap/encoding/"
xmlns:si="http://soapinterop.org/xsd">


Scott



In the HTTP headers, you will see that SOAPAction is an empty string, which is its default value. Your service method can set the value of the SOAPAction, and your client code can call the method specifying the SOAPAction as a parameter.

In the XML payload, you can see that NuSOAP uses ISO-8859-1, which is as well-known as Latin-1, as the encoding. To specify a different encoding, you can set the soap_defencoding attribute on the client soapclient instance. It is of course the programmer's responsibility to encode parameter data using the specified encoding. Fortunately, PHP provides many functions to encode and decode the most common encoding data in SOAP, such as UTF-8.


Another thing to note is that the element specifies the method to be called. The element named hello is placed under the domain name of http://tempuri.org. It is best practice to specify the real domain name. , it is also necessary for many services. A future document is shown here:

The response from the SOAP service looks like this:

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 03 Nov 2004 21:32:34 GMT
X-Powere

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508345.htmlTechArticleThis document describes how to obtain and install NuSOAP, and then provides some examples to illustrate the functionality of NuSOAP. This is not a A comprehensive introduction to NuSOAP, but I hope it can be a little clearer...

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

Detailed steps to install Go language on Win7 computer Detailed steps to install Go language on Win7 computer Mar 27, 2024 pm 02:00 PM

Detailed steps to install Go language on Win7 computer Go (also known as Golang) is an open source programming language developed by Google. It is simple, efficient and has excellent concurrency performance. It is suitable for the development of cloud services, network applications and back-end systems. . Installing the Go language on a Win7 computer allows you to quickly get started with the language and start writing Go programs. The following will introduce in detail the steps to install the Go language on a Win7 computer, and attach specific code examples. Step 1: Download the Go language installation package and visit the Go official website

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

Essential PHP programs: Install these to run smoothly! Essential PHP programs: Install these to run smoothly! Mar 27, 2024 pm 05:54 PM

Essential PHP programs: Install these to run smoothly! PHP is a popular server-side scripting language that is widely used to develop web applications. To successfully run a PHP program, you first need to install some necessary software and tools on the server. In this article, we will introduce the software and tools that must be installed, along with specific code examples to help you run PHP programs smoothly. 1. PHP interpreter The core of the PHP program is the PHP interpreter, which is responsible for parsing and executing PHP code. To install the PHP interpreter, you can follow

PHP FFmpeg extension installation guide: easy-to-follow tutorial PHP FFmpeg extension installation guide: easy-to-follow tutorial Mar 28, 2024 pm 02:17 PM

PHPFFmpeg Extension Installation Guide: Simple and easy-to-understand tutorial In the process of website development, sometimes we need to process various multimedia files, such as audio, video, etc. FFmpeg is a powerful multimedia processing tool that can process audio, video and other formats, and supports various transcoding, cutting and other operations. The PHPFFmpeg extension is an extension library that calls FFmpeg functions in PHP. It can be used to process multimedia files easily. Below we will introduce PHPF in detail

How to download 360 Secure Browser on your computer How to download 360 Secure Browser on your computer Apr 12, 2024 pm 01:52 PM

How to download 360 Secure Browser on your computer? It is a very secure web browser software. This browser is very rich in functions and very simple to operate. Using 360 Secure Browser to browse the web can protect user privacy and security very well. Many people like to use this browser. Browser office, but many people still don’t know how to download and install 360 Secure Browser on their computers. This article will give you a detailed introduction to the installation process of the 360 ​​Safe Browser PC version, hoping to help you solve the problem. Overview of the installation process under the computer version of 360 Secure Browser 1. On the computer’s main page, find “360 Software Manager” and enter (as shown in the picture). 2. Open 360 Software Manager and find the search box (as shown in the picture). 3. Click Search

What is GateToken(GT) currency? Introduction to GT coin functions and token economics What is GateToken(GT) currency? Introduction to GT coin functions and token economics Jul 15, 2024 pm 04:36 PM

What is GateToken(GT) currency? GT (GateToken) is the native asset on the GateChain chain and the official platform currency of Gate.io. The value of GT coins is closely related to the development of Gate.io and GateChain ecology. What is GateChain? GateChain was born in 2018 and is a new generation of high-performance public chain launched by Gate.io. GateChain focuses on protecting the security of users' on-chain assets and providing convenient decentralized transaction services. GateChain's goal is to build an enterprise-level secure and efficient decentralized digital asset storage, distribution and transaction ecosystem. Gatechain has original

How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer Jul 16, 2024 pm 09:02 PM

The expansion of the virtual market is inseparable from the circulation of virtual currency, and naturally it is also inseparable from the issue of virtual currency transfers. A common transfer error is the address copy error, and another error is the chain selection error. The transfer of virtual currency to the wrong chain is still a thorny problem, but due to the inexperience of transfer operations, novices often transfer the wrong chain. So how to recover the wrong chain of virtual currency? The wrong link can be retrieved through a third-party platform, but it may not be successful. Next, the editor will tell you in detail to help you better take care of your virtual assets. How to retrieve the wrong chain of virtual currency? The process of retrieving virtual currency transferred to the wrong chain may be complicated and challenging, but by confirming the transfer details, contacting the exchange or wallet provider, importing the private key to a compatible wallet, and using the cross-chain bridge tool

See all articles