zend framework zend_soap

Zend Framework 101: Zend_Soap

By Quentin Zervaas, 8 August 2009, PHP, SOAP, Zend Framework

Further Improvements

As stated previously, this is a somewhat simplified implementation of web services. There are several things you can add to make this a more advanced implementation. These may include:

  • Complex data types, such as arrays and objects.
  • Type mapping (automatically mapping SOAP data types to local PHP classes on both the server and client). This allows easier manipulation of data passed to and returned from web service calls.
  • Authentication. There are two levels of authentication: one to protect who can access the web service directly, and the second is to let users authenticate via a web service so they receive an elevated level of access.
  • Error handling. We can't always assume data passed to web service calls is valid. The server needs the ability to send errors, and clients need to be able to detect errors and handle them accordingly.
  • Caching. Since every call to a web service can potentially be quite expensive (in terms of time and processing power), it can be extremely beneficial to implement a caching mechanism that allows you re-use the response from web service calls without having to communicate with the SOAP server.
  • WSDL file caching. In this article I haven't covered anything to do with caching of WSDL files. By default, the server and client will cache these files, but you can control this behaviour when instantiating Zend_Soap_Server and Zend_Soap_Client.

Hopefully in an upcoming article I will cover each of these aspects.

http://www.bangbangma.com

develop:pear_soap_server_quick_start

After a month of not writing much, this week it’s a blitz - this time looking at how you create SOAP servers with PEAR::SOAP.

But first...

... a couple of points addressing comments which came up in response to Guidelines for Designing Classes in PHP. This site isn’t handy with an 800×600 screen and the printable version doesn’t always fit a printer. After coming across an awesome little class HTMLSax reckon I’ve got the right tool to help output PDF with R&OS PDF. The problem is there’s HTML-like markup store in the db here but HTMLSax solves (note also that PHPShelve is cunning piece of code that allows you to persist objects with text files, based on a Python Shelve).

As to the point that it’s not worth trying to teach “HTML authors” OOP and patterns... well given that anyone who’s done any HTML has already used a pattern so “they” have a right to know (anyone who’s taught themselves HTML and PHP is capable of working out OOP / patterns IMO)

1)

PEAR::SOAP Server

OK here’s a SOAP server built with PEAR::SOAP;

class Test1 {
    // Stores instance of PEAR::SOAP Server
    var $soapServer;
 
    // Constructor builds PEAR::SOAP Server
    function Test1 () {
        // Switch off notices to all GET
        error_reporting(E_ALL ^ E_NOTICE);
 
        // Instantiate PEAR::SOAP SOAP_Server
        $this->soapServer=new SOAP_Server;
 
        // Build the object map (using this instance) + add a namespace
        $this->soapServer->addObjectMap($this,'http://www.phppatterns.com#Test1');
 
        // Turn on the server
        $this->soapServer->service($GLOBALS['HTTP_RAW_POST_DATA']);
    }
 
    function serverTimestamp() {
        return time();
    }
 
    function hello($name) {
        return 'Hello '.$name;
    }
}
?>

In this example the Test class “owns” the PEAR::SOAP Server instance. To execute the above class;

// testserver1.php
 
// Include the SOAP Server
require_once('SOAP/Server.php');
 
// Include the test class
require_once('lib/Test1.php');
 
// Start the SOAP server
$test = new Test1;
?>

That’s it. PEAR::SOAP server does some introspection on the Test1 class and works out what methods are available (no need to register dispatch maps etc.).

Note the namespace used here;

$this->soapServer->addObjectMap($this,'http://www.phppatterns.com#Test1');

Multiple classes can be registered with the PEAR::SOAP server, the namespace being what distinguishes a method in one from a method in another of the same name.

Another way to do the same thing;

class Test2 {
    function Test2 () {}
 
    function serverTimestamp() {
        return time();
    }
 
    function hello($name) {
        return 'Hello '.$name;
    }
}
?>

Now in the code that executes this class;

// testserver2.php
 
// Include the SOAP Server
require_once('SOAP/Server.php');
 
// Include the test class
require_once('lib/Test2.php');
 
// Start the SOAP server
$test = new Test2;
 
// Switch off notices to all GET
error_reporting(E_ALL ^ E_NOTICE);
 
// Instantiate PEAR::SOAP SOAP_Server
$soapServer=new SOAP_Server;
 
// Build the object map (using this instance) + add a namespace
$soapServer->addObjectMap($test,'http://www.phppatterns.com#Test2');
 
// Turn on the server
$soapServer->service($GLOBALS['HTTP_RAW_POST_DATA']);
?>

You can even pass an instance of PEAR::SOAP server to a class like;

class Test3 {
    function Test3(&$soapServer) {
        $soapServer->addObjectMap($test,'http://www.phppatterns.com#Test3');
     }
}
 
// etc.

Credit to Shane Caraveo for making it so easy.

Note on the Dispatch Map

(Added 12th Apr 2003) One further note on the dispatch map is with the above examples, PEAR::SOAP is gathering every method in the object you pass it - there may be situations where you don’t what this, in which case you need to define the dispatch map yourself like;

$test = new Test4;

$test->dispatch_map['serverTimestamp'] =
    array(
        'out'   => array('timestamp'=>'string')
           );

$test->dispatch_map['hello'] =
    array(
        'in'   => array('name'=>'string'),
        'out'   => array('greeting'=>'string')
           );

// Instantiate the SOAP server
$soapServer=new SOAP_Server;
$soapServer->addObjectMap($test,'http://www.phppatterns.com#Test4');

You might also define the dispatch map inside the constructor of your own class.

2)

PEAR::SOAP Clients the hard way

If you were impressed by WSDL and PEAR::SOAP now for the bad news - unless you’re fluent in WSDL, you have to hand code the proxy code you saw generated there (as people did it in the “old days” with XML-RPC).

At some point I’ll do my best to explain WSDL, perhaps with a rant on REST as well as venting steam about how a standard meant for web services is better suited to strongly typed languages (at least on the server side). Which languages made dynamic web sites really happen? Could it Perl, JavaScript, VBScript and PHP? Not much strong typing there... And it could have been MIME RPC.

Anyway, there’s hope for PHP that at some point in the not so distant future we’ll be able to generate WSDL documents directly from a PHP class, either with the Tokenizer extension (the PHP Documentor team have already begun to take advantage of it) or perhaps the type hinting coming with PHP 5. Shane also mentions some kind of plan for something looking like IDL in the PEAR::SOAP “todo” comments but not sure how far that’s going.

Today you either have to manually code clients for PEAR::SOAP (or your preferred implementation) or learn WSDL, at which point you may be interested in Generating PHP SOAP Servers from WSDL with XSLT. Another option which I haven’t checked out yet is PEAR::SOAP is also supposed to be able to create Servers from WSDL documents in what looks like a similar way to Clients.

Building a client by hand for a small service is pretty simple though, for example;

class TestClient {
 
    var $client;
 
    var $nameSpace;
 
    function TestClient ($url,$nameSpace) {
        $this->client = new SOAP_Client($url);
        $this->nameSpace= $nameSpace;    
    }
 
    function serverTimestamp() {
        $params=array();
        return $this->client->call('serverTimestamp',$params,
                                        $this->nameSpace);
    }
 
    function hello($name) {
        $params=array($name);
        return $this->client->call('hello',$params,
                                        $this->nameSpace);
    }
}
?>

Here I’ve gone for owning the PEAR::SOAP Client - you might considering extending it though in the same way as the proxy code, giving you access to the underlying SOAP methods or even passing it around between classes like a database connection (a SOAP Client a database class have much in common).

To put the above code into action;

// testclient.php
 
// Include the SOAP Server
require_once('SOAP/Client.php');
 
// Include the test class
require_once('lib/TestClient.php');
 
// Edit these to match the server
$url='http://localhost/phppatterns/ws/testserver1.php';
$namespace='http://www.phppatterns.com#Test1';
 
// Start the SOAP server
$testClient = new TestClient($url,$namespace);
 
echo ( $testClient->serverTimeStamp().'' );
echo ( $testClient->hello('World!').'' );
?>

So far so good. Note that I haven’t implemented any error handling here - if something goes wrong PEAR::SOAP returns an object of SOAP_Fault which you can check for like

PEAR::isError($response);

Security and Authentication

Security, in regard to transfering data over the wire and making sure no is “listening in” is best handled by SSL. Authentication could be built into the application itself, should you need it to control access to certain SOAP methods, passing a username/password as arguments but in general you’re better off with HTTP Basic Authentication (best with SSL) - check out the HTTP Authentication section in the manual, to protect the server.

On the client side, PEAR has an excellent collection of network related classes so dealing with HTTP authentication is no problem. Basically just;

$proxy=array('proxy_user'=>'someone','proxy_pass'=>'secret');

$client = new SOAP_Client($url,false,false,$proxy);

// or

$wsdl = new SOAP_WSDL($url,$proxy);

It’s actually PEAR::HTTP Request that gets passed the $proxy array eventually - check out it’s constructor to see all the values you can use.

Which reminds me; you can send SOAP over other protocols such as SMTP (email) and FTP should you so desire. PEAR::SOAP supports SMPT right now (I believe).

News Flash: PEAR API Documentation

http://dickmann.homeunix.org/pear/phpdoc/PEAR/SOAP/0.7.1/ - is this on the pear.php.net site? If not why not?

Just found that.

Note on Interop: RPC/Encoded vs Document / Literal

The .NET tools encourage developers to use Document / Literal style SOAP and there’s been people stuggling to get talking to PEAR::SOAP as a result. I could say “That’s what you get for using a GUI to help you fail to understand what you’re doing” but I wont.

Basically (IMO - who really knows?) document literal style means in the body of the SOAP message you’re sending a “normal” XML document with no definitions of what data types the tags are (no encoding). Here’s where this get’s amusing: http://www.webservicex.net/usweather.asmx/GetWeatherReport?ZipCode=10005 - that’s document / literal SOAP available via the GET method generated by .NET. Now slap me for being stupid but where is there any reference to SOAP in that? This is good because it’s REST (more an that another time) but what was the whole point of SOAP in the first place? Hadn’t we done all this ages ago with WDDX (I guess that’s technically Document/Encoded which is a cross between the two)?

The WSDL document for the service above is at http://www.webservicex.net/usweather.asmx?WSDL

RPC/Encoded style is like the good ‘ol days of XML-RPC - the SOAP encoding specifications identify the data types you use in your SOAP server responses.

In terms of practical difference, RPC/Encoded is a better choice for Intranets / system integration and data exchange between small groups of web servers. Document/Literal (i.e. plain old boring XML + WSDL to help you find it) has more potential to be a true messaging mechanism for web services - i.e. you could use Google to find that this of service as opposed to a UDDI server.

PEAR::SOAP defaults to RPC/Encoded so be warned when you have confused people trying to access your RPC/Encoded service with .NET or if you try to use PEAR::SOAP Client to access a Doc/Lit service. You can pass an array of options to the SOAP_Client::call() method using the argument seen before to pass the namespace e.g (from Shane’s presentation - see end);

$options=array(
            'namespace' => $baseurl.'/livecitycams.wsdl',
            'soapaction' => $baseurl.'/livecitycams/listlivecitycams',
            'style' => 'document',
            'use' => 'literal');

$params=array()

$result = $client->call("ListLiveCityCams",$params,$options);

I haven’t played with this much but it seems that the SOAP_WSDL class knows how to handle document / literal services although no guarantees.

On the server side, I looks like PEAR::SOAP Server can generate Document Literal services but more I can’t tell you (I use this stuff on Intranets where RPC / Encoded is just fine).

Further Reading

Web Services Demystified

Web Services Tutorial - excellent tutorial from Shane Caraveo again at April 23, 2003. PHPCON, NYC, NY - explores all the features of PEAR::SOAP

Building XML Web Services with PHP NuSOAP - NuSOAP is the lightweight brother of PEAR::SOAP geared more to prodecural servers. Last time I looked, the WSDL support wasn’t as good.

Web Services for PHP - presentaion by Shane Caraveo - I think this used an older version of PEAR::SOAP in the examples so be careful.

2) Heard of XUL yet? Mozilla have turned Gecko (the core) into a runtime just like Java and .NET have runtimes. You can use it to make complete GUI applications using markup like HTML along with CSS and JavaScript and you can also launch XUL apps straight from a website to anything running Gecko (e.g. Mozilla / Netscape) - i.e extemely fast download compared to Applets / Flash. And it’s library XPCom has support for SOAP and XML-RPC... check out http://www.xulplanet.com for more...
develop/pear_soap_server_quick_start.txt · Last modified: 2005/10/15 21:47

下载 直接可用

Twitter被墙,推荐翻墙工具

手机访问twitter请用 http://dabr.co.uk (PC也可以用)
找翻墙软件可以去 sesawe.net – 中文

因为dabr是开源的PHP软件,自己搭建dabr twitter web 客户端请参考 @scavinDabr 安装 – Twitter 手机版网页程序安装教程 | 小众软件 > 在线应用

被封了好长一段时间,终于喘过气来了。
FACK GFW!!!

jquery 记录分享

中文:http://blog.youmila.com/?p=491

英文:http://www.insideria.com/2009/09/jquery-ftw-september-6.html

一直以来我一直在收集一些好的jquery链接,但是一直没有机会发布出来

现在我post it(如果你感兴趣或者有好的分享的链接请回复下哈):

jQTouch – 一个为手机开发准备的插件,适用于iPhone, Android, Palm Pre以及其它手机。

jQuery – Manipulating Table Tow Background Colors -使用jquery去操纵表格淡雅u单元的东东,漂亮的表格会有效的帮助用户在大量数据中找到自己想要的信息。

Gritter -一个Growl类型(比如对话框,警示框等dialog)的jquery插件,我比较喜欢它的ui设计很好哈。

Link Nudge – 一个很好的链接闪动的jquery插件。效果很棒哦~

jQTransform – 一个完美的美化表单的一个jquery工具。由于我个人的ui不很好,所以很喜欢它。

Quick and Easy Image rollovers using jQuery – Dan Vega写的一个很好的用jquery实现图片的翻转的指南。

Getting selected text with jQuery -用jquery获取选择的文本,这是一片帖子,回复都溢出了哈大家一起分享。

Identifying and Locating Mouse Position with jQuery -一个讨论用jquery标志和定位鼠标位置的使用指南

jQuery UI 1.8 Preview – 我有点落后了,大家可能已经使用了jquery ui 1.8

劳驾看过的兄弟留了个脚印谢谢哦~

命令行下的Apache日志分析

1,查看apache进程:
ps aux | grep httpd | grep -v grep | wc -l

2,查看80端口的tcp连接:
netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l

3,通过日志查看当天ip连接数,过滤重复:

cat access_log | grep "24/Jul/2007" | awk '{print $2}' | sort | uniq -c | sort -nr

4,当天ip连接数最高的ip都在干些什么(原来是蜘蛛):
cat access_log | grep "24/Jul/2007:00" | grep "61.135.166.230" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10

5,当天访问页面排前10的url:

cat access_log | grep "24/Jul/2007:00" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10

6,用tcpdump嗅探80端口的访问看看谁最高
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr
接着从日志里查看该ip在干嘛:
cat access_log | grep 220.181.38.183| awk '{print $1"\t"$8}' | sort | uniq -c | sort -nr | less


7,查看某一时间段的ip连接数:
grep "2006:0[7-8]" www20060723.log | awk '{print $2}' | sort | uniq -c| sort -nr | wc -l

awstats 分析 Nginx 访问日志((linux,awstats))

Media_httpbbslinuxskyorgimagesyuyulogogif_yvfvvvdnjrzayda

配置 Nginx 自动切割日志

跟 Apache HTTP Server(以下称 Apache)不同的是,Apache可以将日志输出通过管道的方式进行重新定向,依此来进行自动的日志切割。Nginx 在现今版本上还没能跟 Apache一样,通过%YY等参数按日期分批创建日志,但是通过给 nginx 进程发送一个特定的信号,可以使 nginx重新生成日志文件。我们可以定期执行一个 Shell 脚本来切换日志,重新命名或转移,具体的脚本如下:

# mv /opt/nginx/logs/access.log /opt/nginx/logs/access_`date +%Y%m%d`.log
# killall –s USR1 nginx #使用USR1参数通知Nginx进程切换日志文件

全国电信网通DNS列表

电信 DNS 列表 (按拼音排序, 共32条)ITPUB个人空间|3]o ^NI�e
电信 A安徽 202.102.192.68 202.102.199.68ITPUB个人空间"Wu^,L.c"qh2n
电信 A澳门 202.175.3.8 202.175.3.3ITPUB个人空间 f8k x7i\6w4Ey
电信 B北京 202.96.199.133 202.96.0.133 202.106.0.20 202.106.148.1ITPUB个人空间f"r,m Tgq0d
电信 C重庆 61.128.128.68 61.128.192.68
,CF {ZM{%ruM0 电信 F福建 202.101.115.55 218.85.157.99
Hc*B g*^a;d0 电信 G甘肃 202.100.64.68 61.178.0.93ITPUB个人空间j.w xOC6mS
电信 G广东 202.96.128.86 202.96.128.166 202.96.134.133 202.96.128.68
W.E8p6]8?0 电信 G广西 202.103.224.68 202.103.225.68
._ M!\8Ha.LS+Ri0 电信 G贵州 202.98.192.67 202.98.198.167
/Q6@4ljPu6[D,e'O0 电信 H海南 202.100.192.68 202.100.199.8
8Op#w?0tQ0 电信 H河北 219.150.32.132
#UJ*[(g[6y/l0 电信 H黑龙江 219.150.32.132 219.146.0.130 219.147.198.230
tI:B I j3}$Sg0 电信 H河南 219.150.150.150 222.88.88.88 222.85.85.85
3D L I,P\B0 电信 H湖北 202.103.0.68 202.103.24.68 202.103.0.117 202.103.44.150
9S+F*? |b R0 电信 H湖南 220.170.0.18 202.103.96.68 61.187.91.18 220.170.64.68ITPUB个人空间H5~0l\;Z)p]mx
电信 J江苏 61.177.7.1 61.147.37.1 218.2.135.1 221.228.255.1ITPUB个人空间.veY3c;JAoM'@xO/P
电信 J江西 202.101.224.68 202.101.226.69
[+S4vm*mN0 电信 J吉林 219.149.194.55ITPUB个人空间n�|^o+[w*V8z5X
电信 L辽宁 219.150.32.132ITPUB个人空间.\wUq(Ew:I{8Le
电信 N内蒙古 219.150.32.132 219.146.0.130
-UN+x#z#wF,rT A0 电信 N宁夏 202.100.96.68 222.75.152.129
)mr(P+i-N7tT \0 电信 Q青海 202.100.128.68ITPUB个人空间4e5O!uUm?8z
电信 S山东 219.146.0.130ITPUB个人空间4])IM4j(YZz
电信 S上海 202.96.209.5 202.96.209.133 202.96.199.133ITPUB个人空间)E K$n"G3ZnV6Y
电信 S陕西 218.30.19.40 61.134.1.4ITPUB个人空间,j)W:m9e { S1CR s6_3n
电信 S四川 61.139.2.69 202.98.96.68 218.6.200.139 61.139.54.66
a,[X/Byx;_/uh;d0 电信 T台湾 168.95.1.1 168.95.192.1ITPUB个人空间-`Kdd"n!weS7D)X'X
电信 T天津 202.99.104.68ITPUB个人空间 V"j6Xz]z
电信 X香港 205.252.144.126 218.102.62.71ITPUB个人空间 }%|j b u
电信 X新疆 61.128.114.166 61.128.114.133 61.128.99.133 61.128.99.134ITPUB个人空间^~r]Q;VtA.}I
电信 Y云南 202.98.160.68 202.98.161.68 61.166.150.101ITPUB个人空间9T6[3WJs
电信 Z浙江 60.191.244.5 202.96.113.34 220.189.127.107 60.191.134.206
\ @-S:d1[[/d0 ITPUB个人空间p3y6bGv:mn `
网通 DNS 列表 (按拼音排序, 共23条)
7uh)lqQ0 网通 A安徽 218.104.78.2
8n Q_*w$f&S/|7PzQ0 网通 B北京 202.106.0.20 202.106.196.115
j;P"hP+^ Zj0 网通 G甘肃 221.7.34.10ITPUB个人空间)he}$o7?ks2`
网通 G广东 221.4.66.66 210.21.4.130 221.4.8.1
HH,{+}~y+\q#k{2A `0 网通 G广西 202.103.229.40 221.7.128.68ITPUB个人空间C!RR)YXNDJ+gO%E
网通 H海南 221.11.132.2ITPUB个人空间^6\s3X~:Q1L_
网通 H河北 202.99.160.68 202.99.166.4ITPUB个人空间 `'k0?0v x2}q
网通 H黑龙江 202.97.224.68 202.97.224.69
`b/n7_} sv0 网通 H河南 202.102.224.68 202.102.227.68
2P2t v'Q s,HZh,nBT0 网通 H湖北 218.104.111.112 218.104.111.114
:}T�^�uDc0 网通 H湖南 58.20.127.170 58.20.57.4ITPUB个人空间;Lsi-}XPBio
网通 J江苏 221.6.4.66 221.6.96.177 218.104.32.106
/id4v6O5WJtE$Z%O0 网通 J江西 220.248.192.12 220.248.192.13ITPUB个人空间2[Cj(A2PyK eUt5a
网通 J吉林 202.98.0.68 202.98.5.68
0^#H8x |d,eO0 网通 L辽宁 202.96.69.38 202.96.64.68
7X1H+iz;z8js/h/^0 网通 N内蒙古 202.99.224.8 202.99.224.67 202.99.224.68
|8iO v*Zx.v0 网通 S山东 202.102.152.3 202.102.134.68ITPUB个人空间,E#}6B TO0~`/U.o
网通 S上海 210.22.70.3 210.22.84.3 210.52.207.2
_ w:tA7w&s w0 网通 S山西 202.99.192.66 202.99.192.68
&D?MyV w0 网通 S四川 221.10.251.196
_eKlt}5j.t(RA0 网通 T天津 202.99.96.68 202.99.64.69
&g)_N/] u+g p^0 网通 Y云南 221.3.131.9 221.3.131.10
O-p1Wy:C-vH0 网通 Z浙江 221.12.1.228 221.12.33.228 221.12.65.228 218.108.248.200

TCPDUMP中文手册最详细的手册

TCPDUMP中文手册最详细的手册

tcpdump-转储网络上的数据流
总览(SYNOPSIS)
tcpdump[-adeflnNOpqStvx][-ccount][-Ffile]

[-iinterface][-rfile][-ssnaplen]

[-Ttype][-wfile][expression]

描述(DESCRIPTION)
Tcpdump打印出在某个网络界面上,匹配布尔表达式expression的报头.

对于SunOS的nit或bpf界面:要运行tcpdump,你必须有/dev/nit或/dev/bpf*的读访问权限.

对于Solaris的dlpi:你必须有网络仿真设备(networkpseudodevice),如/dev/le的读访问权限.

对于HP-UX的dlpi:你必须是root,或者把它安装成root的设置uid程序.对于IRIX的snoop:你必须是root,或者把它安装成root的设置uid程序.对于Linux:你必须是root,或者把它安装成root的设置uid程序.

对于Ultrix和DigitalUNIX:一旦超级用户使用pfconfig(8)开放了promiscuous操作模式(promiscuous-mode),任何用户都可以运行tcpdump.

对于BSD:你必须有/dev/bpf*的读访问权限.