百度空间 | 百度首页 
 
文章列表
 
您正在查看 "Php" 分类下的文章

2008年12月15日 星期一 18:11
utf8 版本的 Discuz! 7.0 边栏的标题出现乱码,解决办法如下:

进入后台:
界面 -> 界面设置 -> 边栏

选择出现乱码的模块,例如,边栏 -> 主题列表页 -> 边栏模块_最新主题,选择编辑,进入页面后,滚动到页面到最低部,看到“强制字符转换”,选中GBK,提交就可以了。
类别:Php | 评论(1) | 浏览()
 
2008年12月03日 星期三 18:44
From: http://www.christophdorn.com/Blog/2008/09/02/firephp-and-zend-framework-16/

翻译:
漫长的等待, Zend Framework 1.6 支持 FirePHP 了。意味着你不需要下载 FirePHPCore库,Zend Framework 就可以通过 FirePHP 写信息到 Firebug Console

ZF 现在有两个主要的组件可用,一个是 Zend_Log_Writer_Firebug ,另一个是 Zend_Db_Profiler_Firebug让我们来看看如何使用这两个组件,我认为大部分的 ZF应用都会使用 Zend_Controller_Front ,所以接下来的实例中我就假设你是基于 MVC 的应用......
类别:Php | 评论(0) | 浏览()
 
2008年11月11日 星期二 12:26
From:http://www.avnetlabs.com/php/two-step-view-with-codeigniter

要实现想Zend fromework 中的 layout,所谓的“the Two Step View pattern”,只需要在扩展 CI 的控制器,详细的代码和说明请参阅原文,如上的链接。

在这里我只是简单的说明一下思路,通过继承 CI 的控制器类显示自己的 MY_Controller 类,这个类会加载一些默认的数据,并且会有一个 render 函数来实现“两步视图”。参照 Zend framework 中的做法,会在 views 下面创建一个以控制器类命名的文件夹并在这个文件夹下创建一个以动作(action)命名的默认模板文件,让render 函数自动加载。rendre 函数首先会判断这个默认(action)的模板文件是否存在,如果存在就将模板的内容赋值给“第一步视图”(即 layout 文件)变量 ’content‘ ,这个变量会注入到“第一步视图”文件中。

原文作者使用了 PHP5 类的写法来扩展 CI 的控制器,你可以自行修改为 PHP4 版本,更详细的代码和说明请参阅原文。

Layout 文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title; ?></title>

<?php echo $css; ?>

</head>

<body>
<div id="page">

<div id="header">
<h1><?php echo $title; ?></h1>
<?php $this->load->view('partials/menu.tpl.php'); ?>
</div>

<div id="content">
<h2><?php echo $heading; ?></h2>
<?php echo $content; ?>
</div>

<div id="footer">
<p>&copy; 2008 AVNet Labs.</p>
</div>

</div>
</body>
</html>

控制器类:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller {

protected $data = array();
protected $class_name;
protected $action;
protected $param;

public function __construct() {
parent::__construct();
$this->load_defaults();
}

protected function load_defaults() {
$this->data['heading'] = 'Page Heading';
$this->data['content'] = '';
$this->data['css'] = '';
$this->data['title'] = 'Page Title';

$this->class_name = strtolower(get_class($this));
$this->action = $this->uri->segment(2, 'index');

}

protected function render($template='main') {

if (file_exists(APPPATH . 'views/' . $this->class_name . '/' . $this->action . '.tpl.php')) {
$this->data['content'] .= $this->load->view($this->class_name . '/' . $this->action .
'.tpl.php', $this->data, true);
}

$this->load->view("layouts/$template.tpl.php", $this->data);
}

protected function add_css($filename) {
$this->data['css'] .= $this->load->view("partials/css.tpl.php", array(
'filename'
=> $filename), true);
}

}
?>
类别:Php | 评论(0) | 浏览()
 
2008年09月29日 星期一 00:23
谁还会问这些环境变量对应旧的名字,TMD 真白痴!

以下完全摘录 PHP 手册:

$GLOBALS


    包含一个引用指向每个当前脚本的全局范围内有效的变量。该数组的键名为全局变量的名称。从 PHP 3 开始存在 $GLOBALS 数组。

$_SERVER

    变量由 web 服务器设定或者直接与当前脚本的执行环境相关联。类似于旧数组 $HTTP_SERVER_VARS 数组(依然有效,但反对使用)。

$_GET

    经由 URL 请求提交至脚本的变量。类似于旧数组 $HTTP_GET_VARS 数组(依然有效,但反对使用)。

$_POST

    经由 HTTP POST 方法提交至脚本的变量。类似于旧数组 $HTTP_POST_VARS 数组(依然有效,但反对使用)。

$_COOKIE

    经由 HTTP Cookies 方法提交至脚本的变量。类似于旧数组 $HTTP_COOKIE_VARS 数组(依然有效,但反对使用)。

$_FILES

    经由 HTTP POST 文件上传而提交至脚本的变量。类似于旧数组 $HTTP_POST_FILES 数组(依然有效,但反对使用)。详细信息请参阅 POST 方法上传。

$_ENV

    执行环境提交至脚本的变量。类似于旧数组 $HTTP_ENV_VARS 数组(依然有效,但反对使用)。

$_REQUEST

    经由 GET,POST 和 COOKIE 机制提交至脚本的变量,因此该数组并不值得信任。所有包含在该数组中的变量的存在与否以及变量的顺序均按照 php.ini 中的 variables_order 配置指示来定义。此数组在 PHP 4.1.0 之前没有直接对应的版本。参见 import_request_variables()。
    注意

        自 PHP 4.3.0 起,$_FILES 中的文件信息不再存在于 $_REQUEST 中。
        注: 当运行于命令行模式时,此数组将不会包含 argv 和 argc 条目;它们已经存在于数组 $_SERVER 中。

$_SESSION

    当前注册给脚本会话的变量。类似于旧数组 $HTTP_SESSION_VARS 数组(依然有效,但反对使用)。详细信息,请参照 Session 处理函数章节
类别:Php | 评论(0) | 浏览()
 
2008年08月05日 星期二 12:12
之前是在使用网上有人用 perl 写的 memcached-tool 工具,memcached-tool 是个很好用的工具,不过它是个命令行的工具,对于它的使用方法可以参考《查看memcache的统计数据》。

如果你是懒得使用命令行的人,正好,我要介绍一个图形化的管理工具,它是 PHP 写的一个管理 Memcached 的工具,已经具有比较完整的功能,最近还新增加了,查看 Item 的详细信息并可以作删除,It's cool!

1. 具体的统计信息与 memcached-tool 差不多,增加了图形化的统计
2. 比较强大的变量管理,查看 Item 的详细信息并能删除。

工具下载
类别:Php | 评论(0) | 浏览()
 
2008年08月03日 星期日 19:05
FROM:http://gilfster.blogspot.com/2005_08_01_archive.html

好不容易找到 DUAL 表在 MYSQL 中都介绍,比较简单,就不翻译了。

一个简单 DUAL 表的应用,防止插入重复都数据都 SQL:

INSERT INTO table1 (name, age) SELECT 'Angel', 18 FROM dual WHERE NOT EXISTS (SELECT * FROM table1 WHERE name='Angel')


以下是转载的完整原文:

Most of the time when you are using SQL you will be selecting against a table in your database. But there are the odd occassions where you don't actually want to select something from a table but still display information.

In Oracle this is handled by the DUAL table. DUAL is a single row all purpose table which allows you to issue a select statement on values which are not stored in an actual table. Take for example the sysdate function which returns the current date.
SQL> select sysdate from dual;

SYSDATE
---------
23-AUG-05
MySQL has full support for the DUAL table also.
mysql> select curdate() from dual;
+------------+
| curdate() |
+------------+
| 2005-08-23 |
+------------+
1 row in set (0.00 sec)
However MySQL doesn't really need the DUAL table to return values. You can simply issue a select statement that doesn't have a from clause. It's perfectly legal and in fact the most common way to select values that don't belong in a table.
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2005-08-23 |
+------------+
1 row in set (0.00 sec)
You can use this method to return information about the MySQL server or perform calculations. It's especially good for testing SQL you are going to use in the select section of an SQL statement.

Warning : It should be noted that you shouldn't issue anything other than select statements against the DUAL table, issuing INSERTS, UPDATES and worst of all DELETES against DUAL can have at least undesirable results.
类别:Php | 评论(0) | 浏览()
 
2008年08月03日 星期日 13:02
刚刚翻译了四篇关于 PHP-SOAP 的文章,使用的是 NuSOAP 工具集,介绍了如何使用 NuSOAP 来创建和使用 Web service,通过对这几篇文章翻译的同时我也对 NuSOAP 的使用有更深的理解,NuSOAP 的使用并不复杂,只是对于新手来数,对 SOAP 和 WSDL 的概念有点难理解,相对于使用 XML-RPC 而言吧,可能对于新手来说更容易一点吧,早前我也演示了一个简单的 XML-RPC 的 client 和server 交互的实例(PHP XMLRPC simple sample)。

在《Professional Open Source Web Services》的第八章(PHP and Web Services)中也介绍了PHP SOAP(NuSOAP) 和 XML-RPC(PHP XML-RPC),这一章有电子版可以免费阅读,可以在这里下载

另外,还有一个PHP Web Service 的框架 WSO2 WSF/PHP,这个我没有在项目中使用过,这里简单介绍一下,有兴趣的朋友可以去实践一下,可以到这里下载

WSO2 Web Services Framework for PHP (WSO2 WSF/PHP) 是一个开源的企业级的 PHP 扩展,它提供了使用 PHP 来创建和使用 Web Services。WSO2 WSF/PHP 是创建和配置 Web services 的一个完整的解决方案,它只是使用广泛的 WS-* 规范实现的一个 PHP 的扩展。关键的特性包括安全服务和 WS-Security 的客户端支持,使用 MTOM 二进制附件,自动创建 WSDL (需要首先创建model),支持服务和客户端的 WSDL 模式(首页约定model),最后一条是可以与 .NET 和 J2EE 进行交互。

WSO2 WSF/PHP 是以 Apache License, Version 2.0 发布,并基于 WSO2 WSF/C

最后总结一下我关于 PHP Web Services 的文章:

PHP XML-RPC:
PHP XMLRPC simple sample

PHP NuSOAP:
介绍NuSOAP
使用NuSOAP编程
使用NuSOAP编程(2)
使用 NuSOAP 结合 WSDL 来编程
类别:Php | 评论(0) | 浏览()
 
2008年07月30日 星期三 23:32

From:http://www.scottnichol.com/nusoapprogwsdl.htm

这篇文章是接着 Introduction to NuSOAP, Programming with NuSOAPProgramming with NuSOAP Part 2 这三篇,增加了一些实例来说明如何使用 NuSOAP 结合 WSDL 来创建和使用 SOAP web service。

Hello, World Redux
The New Client
Defining New Data Structures

Hello, World Redux

我在 Introduction to NuSOAP 使用普遍的 “Hello,World” 实例,在那篇文章中,我演示了客户端和服务器端的请求和响应的交互,这里,我将使用 WSDL 来扩展那个实例。

WSDL 文件为 service 提供了 metadata,NuSOAP 允许程序员指定使用 soap_server 类的附加字段和方法的 service 创建的 WSDL。

Service 的代码必须依照产生的正确的 WSDL 的顺序做很多事情。service 的信息通过调用 configureWSDL 方法来指定,每个方法的信息也通过提供 register 方法的附加参数来指定,使用 WSDL 的 service 代码在下面的实例中演示:
<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
// Register the method to expose
$server->register('hello', // method name
array('name' => 'xsd:string'), // input parameters
array('return' => 'xsd:string'), // output parameters
'urn:hellowsdl', // namespace
'urn:hellowsdl#hello', // soapaction
'rpc', // style
'encoded', // use
'Says hello to the caller' // documentation
);
// 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);
?>

现在有些魔幻了,在你的浏览器上打开 service 的地址,在我的环境上是 http://localhost/phphack/hellowsdl.php,页面返回的内容提供了可以查看 service 的 WSDL 或者 查看每个方法信息的链接,这个实例是 hello 方法,页面显示的内容类似下图:


因此,只需要在 service 中加入很少的代码,NuSOAP 就可以提供 service 的阅读文档,但是那不是全部。在页面单击每一个 WSDL 链接或者在 URL 后加上 “?wsdl” 字符串,你就可以看到如下的 WSDL :
<?xml version="1.0"?>
<definitions 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"
xmlns:tns="urn:hellowsdl"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="urn:hellowsdl">
<types>
<xsd:schema targetNamespace="urn:hellowsdl">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
</xsd:schema>
</types>
<message name="helloRequest">
<part name="name" type="xsd:string" />
</message>
<message name="helloResponse">
<part name="return" type="xsd:string" />
</message>
<portType name="hellowsdlPortType">
<operation name="hello">
<documentation>Says hello to the caller</documentation>
<input message="tns:helloRequest"/>
<output message="tns:helloResponse"/>
</operation>
</portType>
<binding name="hellowsdlBinding" type="tns:hellowsdlPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="hello">
<soap:operation soapAction="urn:hellowsdl#hello" style="rpc"/>
<input>
<soap:body use="encoded" namespace="urn:hellowsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:hellowsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="hellowsdl">
<port name="hellowsdlPort" binding="tns:hellowsdlBinding">
<soap:address location="http://localhost/phphack/hellowsdl.php"/>
</port>
</service>
</definitions>

The New Client

在 service 中加入一些 NuSOAP WSDL 调用让它产生 WSDL 和其它的文档。相比之下,支持 WSDL 的客户端是突减的(anti-climactic),是少在这个简单的例子是。下面这个简单的例子和之前没有 WSDL 的客户端代码没有什么不同,唯一的不同是 soapclient 类的构造函数提供了一个 WSDL 的 URL 作为参数,而不是service 的地址。

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/hellowsdl.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// 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 '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

这里是 WSDL 实现的请求和响应信息:

POST /phphack/hellowsdl.php HTTP/1.0
Host: localhost
User-Agent: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: &quoturn:hellowsdl#hello"
Content-Length: 550

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope 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"
xmlns:tns="urn:hellowsdl">
<SOAP-ENV:Body>
<tns:hello xmlns:tns="urn:hellowsdl">
<name xsi:type="xsd:string">Scott</name>
</tns:hello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 03 Nov 2004 21:05:34 GMT
X-Powered-By: ASP.NET
X-Powered-By: PHP/4.3.4
Server: NuSOAP Server v0.6.8
X-SOAP-Server: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
Content-Length: 551

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope 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">
<SOAP-ENV:Body>
<ns1:helloResponse xmlns:ns1="urn:hellowsdl">
<return xsi:type="xsd:string">Hello, Scott</return>
</helloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Defining New Data Structures

WSDL 一个重要的方面是它封装了一个或多个 XML 结构,允许程序员通过 service 来描述数据结构,为了说明 NuSOAP 如何支持这个,我会在 Programming with NuSOAP Part 2 文章中的 SOAP struct 实例中加入 WSDL 代码。

service 代码的改变已经显示在 Hello, World 实例中,但是它也包含了定义 Person 数据结构的代码:

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl2', 'urn:hellowsdl2');
// Register the data structures used by the service
$server->wsdl->addComplexType(
'Person',
'complexType',
'struct',
'all',
'',
array(
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
'age' => array('name' => 'age', 'type' => 'xsd:int'),
'gender' => array('name' => 'gender', 'type' => 'xsd:string')
)
);
$server->wsdl->addComplexType(
'SweepstakesGreeting',
'complexType',
'struct',
'all',
'',
array(
'greeting' => array('name' => 'greeting', 'type' => 'xsd:string'),
'winner' => array('name' => 'winner', 'type' => 'xsd:boolean')
)
);
// Register the method to expose
$server->register('hello', // method name
array('person' => 'tns:Person'), // input parameters
array('return' => 'tns:SweepstakesGreeting'), // output parameters
'urn:hellowsdl2', // namespace
'urn:hellowsdl2#hello', // soapaction
'rpc', // style
'encoded', // use
'Greet a person entering the sweepstakes' // documentation
);
// Define the method as a PHP function
function hello($person) {
$greeting = 'Hello, ' . $person['firstname'] .
'. It is nice to meet a ' . $person['age'] .
' year old ' . $person['gender'] . '.';

$winner = $person['firstname'] == 'Scott';

return array(
'greeting' => $greeting,
'winner' => $winner
);
}
// 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);
?>

除了支持 WSDL 的附加代码之外,service 方法的代码本身也有一点改变,使用 WSDL ,不再需要使用 soapval 对象来为返回值指定名称和数据类型。

相似的, WSDL 客户端不需要使用 soapval 指定参数的名称和数据类型,演示代码如下:

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/hellowsdl2.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');
$result = $client->call('hello', array('person' => $person));
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

WSDL 是客户端多于一个功能,使用代理而不是用 soapclinet 类的 call 方法。代理(proxy)是一个类,它映射到 service 。因此,它具备了与 service 相同参数的相同方法,一些程序员更喜欢使用代理因为方法是作为用户一个实例的方法来调用的,而不是通过 call 方法,一个使用代理的实例如下:

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/hellowsdl2.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Create the proxy
$proxy = $client->getProxy();
// Call the SOAP method
$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');
$result = $proxy->hello($person);
// Check for a fault
if ($proxy->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $proxy->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($proxy->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($proxy->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($proxy->debug_str, ENT_QUOTES) . '</pre>';
?>

尽管可以使用常规的和代理的编码风格,但是请求和响应的信息是相同的。

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

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope 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"
xmlns:tns="urn:hellowsdl2">
<SOAP-ENV:Body>
<tns:hello xmlns:tns="urn:hellowsdl2">
<person xsi:type="tns:Person">
<firstname xsi:type="xsd:string">Willi</firstname>
<age xsi:type="xsd:int">22</age>
<gender xsi:type="xsd:string">male</gender>
</person>
</tns:hello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 03 Nov 2004 21:20:44 GMT
X-Powered-By: ASP.NET
X-Powered-By: PHP/4.3.4
Server: NuSOAP Server v0.6.8
X-SOAP-Server: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
Content-Length: 720

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope 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"
xmlns:tns="urn:hellowsdl2">
<SOAP-ENV:Body>
<ns1:helloResponse xmlns:ns1="urn:hellowsdl2">
<return xsi:type="tns:SweepstakesGreeting">
<greeting xsi:type="xsd:string">
Hello, Willi. It is nice to meet a 22 year old male.
</greeting>
<winner xsi:type="xsd:boolean">0</winner>
</return>
</helloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

You can download the source for these examples as well.

类别:Php | 评论(0) | 浏览()
 
2008年07月30日 星期三 23:27

From: http://www.scottnichol.com/nusoapprog2.htm

这篇文章是接着上两篇 Introduction to NuSOAPProgramming with NuSOAP ,增加了一些实例来说明如何使用 SOAP 数组(Arrays)和结构(Structs)类型来创建和使用 SOAP web service。

SOAP Arrays
SOAP Structs
Resources

SOAP Arrays

我在 Introduction to NuSOAPProgramming with NuSOAP 两篇文章中使用了普遍的 ’Hello, World' 实例,显然是没有什么想象力,然而这里,我将会展示如何改进原有的代码,不是对单个人打招呼而使用数组来对多个人打招呼。

SOAP 数组是数字做索引的(不相关的),这与其它编程语言如 C 和 FORTRAN 很相似。因此,我们的 service 可以使用数组索引来访问数组的元素而不是关联的key值。

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server;
// Register the method to expose
// Note: with NuSOAP 0.6.3, only method name is used w/o WSDL
$server->register(
'hello' // method name
);
// Define the method as a PHP function
function hello($names) {
for ($i = 0; $i < count($names); $i++) {
$retval[$i] = 'Hello, ' . $names[$i];
}

return $retval;
}
// 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);
?>

客户端要改变的只是传递的参数是一个数组的名字,而不是单个标量的值。

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/helloworld5.php');
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Constructor error: ' . $err . '</b></p>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$names = array('Scott', 'Albert', 'Robert', 'Phyllis');
$result = $client->call(
'hello', // method name
array('names' => $names) // input parameters
);
// Check for a fault
if ($client->fault) {
echo '<p><b>Fault: ';
print_r($result);
echo '</b></p>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Error: ' . $err . '</b></p>';
} else {
// Display the result
print_r($result);
}
}
?>

请求和响应的信息如下:

POST /phphack/helloworld5.php HTTP/1.0
User-Agent: NuSOAP/0.6.3
Host: localhost:80
Content-Type: text/xml; charset="ISO-8859-1"
Content-Length: 736
SOAPAction: ""

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
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">
<SOAP-ENV:Body>
<ns1:hello xmlns:ns1="http://testuri.org">
<names xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[4]">
<item xsi:type="xsd:string">Scott</item>
<item xsi:type="xsd:string">Albert</item>
<item xsi:type="xsd:string">Robert</item>
<item xsi:type="xsd:string">Phyllis</item>
</names>
</ns1:hello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Thu, 29 May 2003 18:46:12 GMT
X-Powered-By: PHP/4.0.6
Server: NuSOAP Server v0.6.3
Connection: Close
Content-Type: text/xml; charset=UTF-8
Content-Length: 743

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
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">
<SOAP-ENV:Body>
<helloResponse>
<soapVal xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[4]">
<item xsi:type="xsd:string">Hello, Scott</item>
<item xsi:type="xsd:string">Hello, Albert</item>
<item xsi:type="xsd:string">Hello, Robert</item>
<item xsi:type="xsd:string">Hello, Phyllis</item>
</soapVal>
</helloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SOAP Structs

我们已经使用标量类型和数组标量作为参数和返回值开发过实例,在这一部分,我将会使用SOAP 结构,在 XML 结构中它或多或少地符合复杂类型。这个例子是另一个不同的 Hello, World ,这次使用结构为参数来提供更多的问候信息。

NuSOAP 利用 PHP 的功能,使用关联数组来表现 SOAP 结构,这个例子,我们会使用一个包含了名字,年龄和性别的数组,在 PHP 中会像这样:

$person = array(
'firstname' => 'Betty',
'age' => 32,
'gender' => 'female'
);

这个 service 接受一个上面显示的关联数组为参数,它返回另一个数组:

$return = array(
'greeting' => 'Hello...',
'winner' => false
);

service 的代码:

<?php
// 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' // method name
);
// Define the method as a PHP function
function hello($person) {
$greeting = 'Hello, ' . $person['firstname'] .
'. It is nice to meet a ' . $person['age'] .
' year old ' . $person['gender'] . '.';

$winner = $person['firstname'] == 'Scott';

$retval = array(
'greeting' => $greeting,
'winner' => $winner
);

return new soapval('return', 'ContestInfo', $retval, false, 'urn:MyURN');
}
// 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);
?>

要注意的一件事情是 service 方法返回的是一个 soapval,所以这里的 xml 数据类型 urn:MyURN:ContestInfo 已可以被指定为返回值。

这里是客户端代码:

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/helloworld6.php');
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Constructor error: ' . $err . '</b></p>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');
$result = $client->call(
'hello', // method name
array('person' => new soapval('person', 'Person',
$person, false, 'urn:MyURN')) // input parameters
);
// Check for a fault
if ($client->fault) {
echo '<p><b>Fault: ';
print_r($result);
echo '</b></p>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Error: ' . $err . '</b></p>';
} else {
// Display the result
print_r($result);
}
}
?>

像服务器端,这里使用一个 soapval 为参数,以便 XML 数据类型 urn:MyURN:Person 可以被指定。

请求和响应的信息如下:

POST /phphack/helloworld6.php HTTP/1.0
User-Agent: NuSOAP/0.6.3
Host: localhost:80
Content-Type: text/xml; charset="ISO-8859-1"
Content-Length: 688
SOAPAction: ""

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
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">
<SOAP-ENV:Body>
<ns1:hello xmlns:ns1="http://testuri.org">
<person xmlns:ns6678="urn:MyURN" xsi:type="ns6678:Person">
<firstname xsi:type="xsd:string">Willi</firstname>
<age xsi:type="xsd:int">22</age>
<gender xsi:type="xsd:string">male</gender>
</person>
</ns1:hello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Thu, 29 May 2003 19:50:30 GMT
X-Powered-By: PHP/4.0.6
Server: NuSOAP Server v0.6.3
Connection: Close
Content-Type: text/xml; charset=UTF-8
Content-Length: 679

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
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">
<SOAP-ENV:Body>
<helloResponse>
<return xmlns:ns7437="urn:MyURN"
xsi:type="ns7437:ContestInfo">
<greeting xsi:type="xsd:string">
Hello, Willi. It is nice to meet a 22 year old male.
</greeting>
<winner xsi:type="xsd:boolean">0</winner>
</return>
</helloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这个实例已经展示了如何发送一个 SOAP 结构参数和返回结果,它也展示了如何指定每个结构的 XML 类型。没有附加的工具,使用这些类型服务器端和客户端的交互遵守了这些结构的约定就可以进行高级的请求和交互。这是原生的 SOAP 1.1 规范的极大的自由之一,但是他也引起了交互的问题。这个系列的下一篇文章将会展示如何使用 WSDL 来为 web service 提供metadata ,也包括我们在使用的数据结构。

You can download the source for these examples as well.

类别:Php | 评论(0) | 浏览()
 
2008年07月30日 星期三 23:24
From:http://www.scottnichol.com/nusoapprog.htm

这篇文章是接着上一篇 Introduction to NuSOAP ,并附加了一些实例来说明使用 NuSOAP 来创建和使用 SOAP web services。

Hello, World Redux
Simple Types
SOAP Faults
Resources

Hello, World Redux

没有什么想象力的,在 Introduction to NuSOAP 中我使用了普遍的 "Hello World" 为例子。在那个文档我展示了在客户端和服务器端 SOAP 的请求和响应的交换。我们这里第一件事情要研究的是如何改进这个实例,这也是这个改进的限制。

在 SOAP 响应中 XML payload 有两个方面可以改进,第一,响应的元素应该有一个域名(命名空间),第二,响应的第一个孩子应该取一个有意义的名称,而不只是接受一个默认的名字 soapVal。

有人会认为响应的元素会通过 sopa_server->register 方法的 $namespace 参数来设置,否则,他可能会通过在输出参数的数组中指定返回元素的名字。但是,以下显示的已改变的代码会返回与原先的 helloworld.php 实例一样的 SOAP 响应。有趣的是,尽管一个命名空间和 SOAPAction 已经设置了,但是客户端不需要在请求中提供这些。

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server;
// Register the method to expose
// Note: with NuSOAP 0.6.3, only method name is used w/o WSDL
$server->register(
'hello', // method name
array('name' => 'xsd:string'), // input parameters
array('return' => 'xsd:string'), // output parameters
'uri:helloworld', // namespace
'uri:helloworld/hello', // SOAPAction
'rpc', // style
'encoded' // use
);
// 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);
?>

客户端的代码按照指定的命名空间和 SOAPAction 来调用,尽管上面已经注意到, NuSOAP service 是可以忽略它的,但是,既然他们会请求很多基于其它的 SOAP 实现的 services 的交互,例如 .NET 或者 Apache Axis,那么包含他们是很好的实践。

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/helloworld3.php');
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Constructor error: ' . $err . '</b></p>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$result = $client->call(
'hello', // method name
array('name' => 'Scott'), // input parameters
'uri:helloworld', // namespace
'uri:helloworld/hello' // SOAPAction
);
// Strange: the following works just as well!
//$result = $client->call('hello', array('name' => 'Scott'));
// Check for a fault
if ($client->fault) {
echo '<p><b>Fault: ';
print_r($result);
echo '</b></p>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Error: ' . $err . '</b></p>';
} else {
// Display the result
print_r($result);
}
}
?>

因此,服务器端使用的只有我们注册它时指定的方法名,我们仍然可以响应数据的名称和类型。相反,我们返回的只是一个字符串,我们可以返回 soapval,它是 NuSOAP 提供的一个类。这里是在原来的 helloworld.php 实例上的一个小改变:

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server;
// Register the method to expose
// Note: with NuSOAP 0.6.3, only method name is used w/o WSDL
$server->register(
'hello', // method name
array('name' => 'xsd:string'), // input parameters
array('return' => 'xsd:string'), // output parameters
'uri:helloworld', // namespace
'uri:helloworld/hello', // SOAPAction
'rpc', // style
'encoded' // use
);
// Define the method as a PHP function
function hello($name) {
return new soapval('return', 'xsd:string', '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);
?>

如果你浏览 soapval 类的构造函数,你会看到,如果真的想为包含返回值的元素指定命名空间(namespace),我们也可以为它指定,在这里不需要指定它。来自新的 service 的响应会在下面被展示,注意,包含相应元素的元素现在是返回名字,而不是 soapval。

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Thu, 24 Apr 2003 15:15:28 GMT
X-Powered-By: PHP/4.0.6
Server: NuSOAP Server v0.6.3
Connection: Close
Content-Type: text/xml; charset=UTF-8
Content-Length: 522

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
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">
<SOAP-ENV:Body>
<helloResponse>
<return xsi:type="xsd:string">Hello, Scott</return>
</helloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Simple Types

"Hello, World" 实例使用一个字符串作为参数和一个字符串作为返回值,我们也可以使用其它的类型,PHP4 支持 string,float 和 boolean 类型,以下的 service 有一个使用了这四种类型的方法。

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server;
// Register the method to expose
$server->register('joinparams');
// Define the method as a PHP function
function joinparams($s, $i, $f, $b) {
$ret = $s . ' is a ' . gettype($s);
$ret .= ' ' . $i . ' is a ' . gettype($i);
$ret .= ' ' . $f . ' is a ' . gettype($f);
$ret .= ' ' . $b . ' is a ' . gettype($b);

return new soapval('return', 'xsd:string', $ret);
}
// 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);
?>

这是客户端代码:

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/scalar.php');
// Call the SOAP method
$result = $client->call(
'joinparams',
array('s' => 'foo', 'i' => 21, 'f' => 43.21, 'b' => true)
);
// Check for a fault
if ($client->fault) {
echo '<p><b>Fault: ';
print_r($result);
echo '</b></p>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Error: ' . $err . '</b></p>';
} else {
// Display the result
print_r($result);
}
}
?>

结果是:

foo is a string 21 is a integer 43.21 is a double 1 is a integer

结果有让你惊讶吗?第四个参数是我们支持的 boolean 类型,在这个 service 中出现的是一个 integer 类型,SOAP 请求第一件事情是要检查类型,如下:

POST /phphack/scalar.php HTTP/1.0
User-Agent: NuSOAP/0.6.3
Host: localhost
Content-Type: text/xml; charset="ISO-8859-1"
Content-Length: 630
SOAPAction: ""

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
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">
<SOAP-ENV:Body>
<ns1:joinparams xmlns:ns1="http://testuri.org">
<s xsi:type="xsd:string">foo</s>
<i xsi:type="xsd:int">21</i>
<f xsi:type="xsd:float">43.21</f>
<b xsi:type="xsd:boolean">1</b>
</ns1:joinparams>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

事实上,这里显示的第四个参数在 XML 结构中被指定是一个 boolean 类型,这是 NuSOAP 的一个奇怪的事情。当标值从 XML payload 中被解串时,XML 类型的信息被忽略了,除了 boolean,这可能是影响 strings 的一个坏方法。例如,一个包含的都是数字的 string ,它会被解串为一个 integer 类型。像如果是美国的邮政编码的话,那就非常的糟糕了。在有很多数字不能成功转换到字符串(string)的情况也是非常糟糕的,PHP 将会产生一个溢出的错误。

这个例子也引起了使用运行时类型(也称弱类型)的 PHP 的一个讨论,一个 NuSOAP service 方法的参数类型不能够被强制。(事实上,他们能够使用 WSDL 被强制,但是我们还没有说到那里)尽管这是我的本意,使用一个 string,integer,float 和 boolean 为参数来调用这个方法,但是以这个顺序,PHP 和 NuSOAP 没有强制执行。如果数据的类型对你来说是很重要的话,你的代码必须要对它们作检查,下一部分将会展示一个这样的实例。

SOAP Faults

SOAP 1.1 specification 为服务器端返回给客户端的错误报告定义了一个方法,就是 SOAP Fault。客户端的代码中我们已经有检查错误和显示返回的信息,在某些情况下,NuSOAP 服务器端代码将会自动地产生,例如,当客户端请求一个不存在的方法时,你写的 service 代码也可以产生错误信息。

上一个例子,假设我们想强制传递过来的数据类型,我们能够检查数据类型,如果有不正确就返回一个 SOAP Fault。改进的 service 代码在下面展示。还有注意,基于发现的 NuSOAP 的解串标值,service 方法必须检查变量的类型,但比你期望会更宽松。例如,当字符串(string)'55.55' 被解串为一个浮点值(float)时,我们允许浮点值被指定为字符串(我们的代码也会映射浮点值为字符串)。

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server;
// Register the method to expose
$server->register('joinparams');
// Define the method as a PHP function
function joinparams($s, $i, $f, $b) {
if (!(is_string($s) || is_int($s) || is_float($s))) {
return new soap_fault('SERVER', '', 's must be a string', $s);
}
if (!is_int($i)) {
return new soap_fault('SERVER', '', 'i must be an integer', $i);
}
if (!(is_float($f) || is_int($f))) {
return new soap_fault('SERVER', '', 'f must be a float', $f);
}
if (!(is_bool($b) || is_int($b))) {
return new soap_fault('SERVER', '', 'b must be a boolean', $b);
}

$ret = $s . ' is a ' . gettype($s);
$ret .= ' ' . $i . ' is a ' . gettype($i);
$ret .= ' ' . $f . ' is a ' . gettype($f);
$ret .= ' ' . $b . ' is a ' . gettype($b);

return new soapval('return', 'xsd:string', $ret);
}
// 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);
?>
通过提供一个客户端指定了不兼容的类型参数,例如字符串 ‘bar' 作为整型(integer)参数 ¥i,你可以看到服务器端会返回一个 SOAP Fault 到客户端,这里是响应的信息:
HTTP/1.1 500 Internal Server Error
Server: Microsoft-IIS/5.0
Date: Thu, 24 Apr 2003 16:08:50 GMT
X-Powered-By: PHP/4.0.6
Server: NuSOAP Server v0.6.3
Connection: Close
Content-Type: text/xml; charset=UTF-8
Content-Length: 620

<?xml version="1.0"?>
<SOAP-ENV:Envelope 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">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SERVER</faultcode>
<faultactor></faultactor>
<faultstring>i must be an integer</faultstring>
<detail>
<soapVal xsi:type="xsd:string">bar</soapVal>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

You can download the source for these examples as well.

类别:Php | 评论(0) | 浏览()
 
     
 
 
文章分类
 
 
 
Ria(25)
 
 
 
 
 
Ruby(2)
 
 
Php(87)
 
Linux(17)
 
Jquery(34)
 
 
Joomla(17)
 
     
 
文章存档
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
     
 
最新文章评论
   
 
 

oiii
 

请 看不
 
 
     


©2009 Baidu