Debugging a PHP XMLRPC Service with Xdebug and Curl

Standard

curl -i -X POST \
   -H "Content-Type:application/xml" \
   -d \
'<?xml version="1.0"?>
<methodCall>
<methodName>sample.sumAndDifference</methodName>
<params>
<param><value><int>1</int></value></param>
<param><value><int>2</int></value></param>
</params>
</methodCall>' \
 'http://localhost/xmlrpc_server.php?XDEBUG_SESSION_START=sublime.xdebug'

xmlrpc_server.php

<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><struct>
<member><name>sum</name>
<value><int>3</int></value>
</member>
<member><name>difference</name>
<value><int>-1</int></value>
</member>
</struct></value>
</param>
</params>
</methodResponse>

转XML-RPC范例

Standard

xmlrpc_client.php

<html>
<head>
<title>XML-RPC PHP Demo</title>
</head>
<body>
<h1>XML-RPC PHP Demo</h1>
<?php
include './xmlrpc.inc';
// Make an object to represent our server.
$server = new xmlrpc_client('/xmlrpc_server.php', 'www.test.com', 80);
 
// Send a message to the server.
$message = new xmlrpcmsg('sample.sumAndDifference',
                         array(new xmlrpcval(5, 'int'),
                               new xmlrpcval(3, 'int')));
$result = $server->send($message);
 
// Process the response.
if (!$result) {
    print "<p>Could not connect to HTTP server.</p>";
} elseif ($result->faultCode()) {
    print "<p>XML-RPC Fault #" . $result->faultCode() . ": " .
        $result->faultString();
} else {
    $struct = $result->value();
    $sumval = $struct->structmem('sum');
    $sum = $sumval->scalarval();
    $differenceval = $struct->structmem('difference');
    $difference = $differenceval->scalarval();
    print "<p>Sum: " . htmlentities($sum) .
        ", Difference: " . htmlentities($difference) . "</p>";
}
?>
</body></html>

xmlrpc_server.php

<?php
include './xmlrpc.inc';
include './xmlrpcs.inc';
 
function sumAndDifference ($params) {
    // Parse our parameters.
    $xval = $params->getParam(0);
    $x = $xval->scalarval();
    $yval = $params->getParam(1);
    $y = $yval->scalarval();
 
    // Build our response.
    $struct = array('sum' => new xmlrpcval($x + $y, 'int'),
                    'difference' => new xmlrpcval($x - $y, 'int'));
    return new xmlrpcresp(new xmlrpcval($struct, 'struct'));
}
 
// Declare our signature and provide some documentation.
// (The PHP server supports remote introspection. Nifty!)
$sumAndDifference_sig = array(array('struct', 'int', 'int'));
$sumAndDifference_doc = 'Add and subtract two numbers';
 
new xmlrpc_server(array('sample.sumAndDifference' =>
                        array('function' => 'sumAndDifference',
                              'signature' => $sumAndDifference_sig,
                              'docstring' => $sumAndDifference_doc)));
?>

PHP Msgpack安装报错处理

Standard

libtool: Version mismatch error…

phpize –clean;
phpize;
rm aclocal.m4;
aclocal;
autoconf;
./configure;
make;
make install;

done!

ps:
MessagePack是一种新的基于二进制的对象序列化类库。它可以像JSON那样,在许多种语言之间交换结构对象;但是它比JSON更快速也更轻巧。

以下转载自:http://hi.baidu.com/yzkuang/blog/item/557e4f24423d8136c9955908.html

1.autoscan (autoconf): 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是configure.ac的一个雏形。
2.aclocal (automake):根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到文件 aclocal.m4中。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”
 
user input files   optional input     process          output files
================   ==============     =======          ============
 
                    acinclude.m4 - - - - -.
                                          V
                                      .-------,
configure.ac ------------------------>|aclocal|
                 {user macro files} ->|       |------> aclocal.m4
                                      `-------'
3.autoheader(autoconf): 根据configure.ac中的某些宏,比如cpp宏定义,运行m4,声称config.h.in
 
user input files    optional input     process          output files
================    ==============     =======          ============
 
                    aclocal.m4 - - - - - - - .
                                             |
                                             V
                                     .----------,
configure.ac ----------------------->|autoheader|----> autoconfig.h.in
                                     `----------'
4.automake: automake将Makefile.am中定义的结构建立Makefile.in,然后configure脚本将生成的Makefile.in文件转换为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,否则它会自己产生config.guess和config.sub
 
user input files   optional input   processes          output files
================   ==============   =========          ============
 
                                     .--------,
                                     |        | - - -> COPYING
                                     |        | - - -> INSTALL
                                     |        |------> install-sh
                                     |        |------> missing
                                     |automake|------> mkinstalldirs
configure.ac ----------------------->|        |
Makefile.am  ----------------------->|        |------> Makefile.in
                                     |        |------> stamp-h.in
                                 .---+        | - - -> config.guess
                                 |   |        | - - -> config.sub
                                 |   `------+-'
                                 |          | - - - -> config.guess
                                 |libtoolize| - - - -> config.sub
                                 |          |--------> ltmain.sh
                                 |          |--------> ltconfig
                                 `----------'
5.autoconf:将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。
 
user input files   optional input   processes          output files
================   ==============   =========          ============
 
                   aclocal.m4 - - - - - -.
                                         V
                                     .--------,
configure.ac ----------------------->|autoconf|------> configure ----->autoconfig.h,Makefile

《大话设计模式》装饰模式-穿什么场景-PHP版代码

Standard

装饰模式
穿什么场景

<?php
/**
 * 装饰模式 穿什么场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
interface Component{
        public function Show();
}
 
class Person implements Component{
        protected $_name;
 
        public function __construct($name){
                $this->_name = $name;
        }
        public function Show(){
                echo "装扮的".$this->_name;
        }
}
 
 
abstract class Finery implements Component{
        protected $_component;
        public function Decorate(Component $component){
                $this->_component = $component;
        }
        public function Show(){
                $this->_component->Show();
        }
}
class Sneakers extends Finery{
        public function Show(){
                echo "旅游鞋 ";
                parent::Show();
        }
}
class TShirts extends Finery{
        public function Show(){
                echo "T恤 ";
                parent::Show();
        }
}
class BigTrouser extends Finery{
        public function Show(){
                echo "垮裤 ";
                parent::Show();
        }
}
class Tie extends Finery{
        public function Show(){
                echo "领带 ";
                parent::Show();
        }
}
class Suit extends Finery{
        public function Show(){
                echo "西装 ";
                parent::Show();
        }
}
class LeatherShoes extends Finery{
        public function Show(){
                echo "皮鞋 ";
                parent::Show();
        }
}
class Main{
        public static function run(){
                $xc = new Person("小菜");
 
                $sk = new Sneakers();
                $ts = new TShirts();
                $bt = new BigTrouser();
 
                echo "装扮1:\n";
                $sk->Decorate($xc);
                $ts->Decorate($sk);
                $bt->Decorate($ts);
                $bt->Show();
 
                $dn = new Person("大鸟");
 
                $ti = new Tie();
                $si = new Suit();
                $ls = new LeatherShoes();
 
                echo "\n装扮2:\n";
                $ti->Decorate($dn);
                $si->Decorate($ti);
                $ls->Decorate($si);
                $ls->Show();
 
        }
}
Main::run();
?>
//php Decorator.php 
//装扮1:
//垮裤 T恤 旅游鞋 装扮的小菜
//装扮2:
//皮鞋 西装 领带 装扮的大鸟

转载请注明出处 http://yemaosheng.com

《大话设计模式》策略模式-商场促销场景-PHP版代码

Standard

策略模式
商场促销场景

<?php
/**
 * 策略模式 商场促销场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
abstract class CashSuper{
        public abstract function acceptCash($money);
}
class CashNormal extends CashSuper{
        public function acceptCash($money){
                return $money;
        }
}
class CashRebate extends CashSuper{
        private $_moneyRebate;
        public function __construct($moneyRebate){
                $this->_moneyRebate = $moneyRebate;
        }
        public function acceptCash($money){
                return $money * $this->_moneyRebate;
        }
}
class CashReturn extends CashSuper{
        private $_moneyCondition;
        private $_moneyReturn;
        public function __construct($moneyCondition, $moneyReturn){
                $this->_moneyCondition = $moneyCondition;
                $this->_moneyReturn = $moneyReturn;
        }
        public function acceptCash($money){
                if( $money >= $this->_moneyCondition )
                        $result = $money - floor($money / $this->_moneyCondition) * $this->_moneyReturn;
 
                return $result;
        }
}
 
class CashContext{
        private $_cs;
 
        public function __construct(CashSuper $csuper){
                $this->_cs = $csuper;
        }
        public function GetResult($money){
                return $this->_cs->acceptCash($money);
        }
}
 
//简单工厂
class CashContextFactory{
        private $_cs;
 
        public function __construct($type){
                switch ($type) {
                case "a":
                    $this->_cs = new CashNormal();
                    break;
                case "b":
                    $this->_cs = new CashReturn("300", "100");
                    break;
                case "c":
                    $this->_cs = new CashRebate("0.8");
                    break;
                }
        }
        public function GetResult($money){
                return $this->_cs->acceptCash($money);
        }
}
 
class Main{
        public static function run_Strategy(){
                fwrite(STDOUT, "数量: ");
                $number = trim(fgets(STDIN));
 
                fwrite(STDOUT, "单价: ");
                $price = trim(fgets(STDIN));
 
                fwrite(STDOUT, "折扣: a = 原价 b = 返利 c = 8折 \nEnter your discount: ");
                $discount = trim(fgets(STDIN));
 
                switch ($discount) {
                case "a":
                    $cc = new CashContext( new CashNormal() );
                    break;
                case "b":
                    $cc = new CashContext( new CashReturn("300", "100") );
                    break;
                case "c":
                    $cc = new CashContext( new CashRebate("0.8") );
                    break;
                }
 
                echo "\n======帐单======\n数量:" .$number. "\n单价". $price. "\n折后总价:". $cc->GetResult($number*$price);
        }
        public static function run_StrategyFactory(){
                fwrite(STDOUT, "数量: ");
                $number = trim(fgets(STDIN));
 
                fwrite(STDOUT, "单价: ");
                $price = trim(fgets(STDIN));
 
                fwrite(STDOUT, "折扣: a = 原价 b = 返利 c = 8折 \nEnter your discount: ");
                $discount = trim(fgets(STDIN));
 
                $cc = new CashContextFactory($discount);
 
                echo "\n======帐单======\n数量:" .$number. "\n单价". $price. "\n折后总价:". $cc->GetResult($number*$price);
        }
}
Main::run_StrategyFactory();
?>
//php Strategy.php 
//数量: 10
//单价: 120
//折扣: a = 原价 b = 返利 c = 8折 
//Enter your discount: c
 
//======帐单======
//数量:10
//单价120
//折后总价:960

转载请注明出处 http://yemaosheng.com

《大话设计模式》访问者模式-男人和女人场景-PHP版代码

Standard

访问者模式
男人和女人场景

<?php
/**
 * 访问者模式 男人和女人场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
abstract class Action{
        public abstract function GetManConclusion(Man $concreteElementA);
        public abstract function GetWomanConclusion(Woman $concreteElementB);
}
abstract class Person{
        public abstract function Accept(Action $visitor);
}
 
class Success extends Action{
        public function GetManConclusion(Man $concreteElementA){
                echo "男人".__CLASS__."时 背后多半有一个伟大的女人\n";
        }
        public function GetWomanConclusion(Woman $concreteElementB){
                echo "女人".__CLASS__."时 背后多半有一个不太成功的男人\n";
        }
}
class Failing extends Action{
        public function GetManConclusion(Man $concreteElementA){
                echo "男人".__CLASS__."时 闷头喝酒,谁也不用劝\n";
        }
        public function GetWomanConclusion(Woman $concreteElementB){
                echo "女人".__CLASS__."时 眼泪汪汪,谁也劝不了\n";
        }
}
class Amativeness extends Action{
        public function GetManConclusion(Man $concreteElementA){
                echo "男人".__CLASS__."时 凡事不懂也要装懂\n";
        }
        public function GetWomanConclusion(Woman $concreteElementB){
                echo "女人".__CLASS__."时 遇事懂也装作不懂\n";
        }
}
 
class Man extends Person{
        public function Accept(Action $visitor){
                $visitor->GetManConclusion($this);
        }
}
class Woman extends Person{
        public function Accept(Action $visitor){
                $visitor->GetWomanConclusion($this);
        }
}
 
class ObjectStructure{
        private $_elements;
 
        public function __construct(){
                $this->_elements = array();
        }
        public function Attach(Person $element){
                $this->_elements[ count($this->_elements) ] = $element;
        }
        public function Display(Action $visitor){
                foreach($this->_elements as $element){
                        $element->Accept($visitor);
                }
        }
}
 
class Main{
        public static function run(){
                $o = new ObjectStructure;
                $o->Attach(new Man());
                $o->Attach(new Woman());
 
                $v1 = new Success;
                $o->Display($v1);
 
                $v2 = new Failing;
                $o->Display($v2);
 
                $v3 = new Amativeness;
                $o->Display($v3);
        }
}
Main::run();
?>
//php Visitor.php 
//男人Success时 背后多半有一个伟大的女人
//女人Success时 背后多半有一个不太成功的男人
//男人Failing时 闷头喝酒,谁也不用劝
//女人Failing时 眼泪汪汪,谁也劝不了
//男人Amativeness时 凡事不懂也要装懂
//女人Amativeness时 遇事懂也装作不懂

转载请注明出处 http://yemaosheng.com

《大话设计模式》解释器模式-音符场景-PHP版代码

Standard

解释器模式
音符场景

<?php
/**
 * 解释器模式 音符场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
class PlayContext{
	private $PlayText;
 
        public function __get($property_name){
                if(isset($this->$property_name)){
                        return($this->$property_name);
                }else{
                        return(NULL);
                }
        }
        public function __set($property_name, $value){
                $this->$property_name = $value;
        }
 
}
 
abstract class Expression{
	public function Interpret(PlayContext $context){
		if( strlen($context->PlayText)==0 ){
			return;
		}else{
			$PlayKey = substr($context->PlayText, 0, 1);
			$context->PlayText = substr($context->PlayText, 2);
			$PlayValue = substr( $context->PlayText, 0, strpos($context->PlayText, ' ') );
			$context->PlayText = substr($context->PlayText, strpos($context->PlayText, ' ') + 1 );
 
			$this->Excute($PlayKey, $PlayValue);
		}
	}
	public abstract function Excute($key, $value);
}
 
//音符类
class Note extends Expression{
	public function Excute($key, $value){
		$note = "";
		switch($key){
		case "C":
			$note = "1";
			break;
		case "D":
			$note = "2";
			break;
		case "E":
			$note = "3";
			break;
		case "F":
			$note = "4";
			break;
		case "G":
			$note = "5";
			break;
		case "A":
			$note = "6";
			break;
		case "B":
			$note = "7";
			break;
		}
		echo $note . " ";
	}
}
//音阶类
class Scale extends Expression{
	public function Excute($key, $value){
		$scale = "";
		switch($value){
		case "1":
			$scale = "低音";
			break;
		case "2":
			$scale = "中音";
			break;
		case "3":
			$scale = "高音";
			break;
		}
		echo $scale . " ";
	}
}
//速度类
class Speed extends Expression{
	public function Excute($key, $value){
		$speed = "";
		if ( $value < 500 ) 
			$speed = "快速";
		elseif( $value >= 1000 )
			$speed = "慢速";
		else
			$speed = "中速";
 
		echo $speed . " ";
	}
}
 
class Main{
	public static function run(){
		$context = new PlayContext();
		$context->PlayText = "T 500 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3";
		try{
			while ( strlen($context->PlayText) > 0 ){
				$str = substr($context->PlayText, 0, 1);
				switch($str){
				case "O":
					$expression = new Scale();
					break;
				case "T":
					$expression = new Speed();
					break;
				default:
					$expression = new Note();
					break;
				}
				$expression->Interpret($context);
			}
 
		} catch (Exception $e) {
			echo $e;
		}
	}
}
Main::run();
?>
//php Expression.php 
//中速 中音 3 5 6 3 5 2 3 5 6 高音 1 中音 6 5 1 3 2

转载请注明出处 http://yemaosheng.com

《大话设计模式》享元模式-网站场景-PHP版代码

Standard

享元模式
网站场景

<?php
/**
 * 享元模式 网站场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
class User{
	public $name;
	public function __construct($name){
		$this->name = $name;
	}
}
 
abstract class WebSite{
	abstract function __construct(User $user);
}
 
class ConcreteWebSite extends Website{
 
	private $_name;
 
	public function __construct($name){
		$this->_name = $name;
	}
	public function User(User $user){
		echo "网站分类:" . $this->_name . " 用户:" . $user->name . "\n";
	}
}
 
class WebSiteFactory{
 
	private $_websites;
	private $_ConcreteWetSite;
 
	public function __construct(){
		$this->_ConcreteWetSite = array();
	}
 
	public function GetWebSiteCategory($key){
		if( isset($websites[$key]) ){
			return $this->_websites[$key];
		}else{
			return $this->_websites[$key] = new ConcreteWebSite($key);
		}
	}
	public function GetWebSiteCount(){
		return count( $this->_websites );
	}
}
 
class Main{
	public static function run(){
		$WebSiteFactory = new WebSiteFactory;
		$f = new WebSiteFactory();
		$fx = $f->GetWebSiteCategory("产品展示");
		$fx->User(new User("小菜"));
 
		$fy = $f->GetWebSiteCategory("产品展示");
		$fy->User(new User("大鸟"));
 
		$fy = $f->GetWebSiteCategory("产品展示");
		$fy->User(new User("娇娇"));
 
		$fl = $f->GetWebSiteCategory("博客");
		$fl->User(new User("老顽童"));
 
		$fm = $f->GetWebSiteCategory("博客");
		$fm->User(new User("六仙"));
 
		$fn = $f->GetWebSiteCategory("博客");
		$fn->User(new User("南海"));
 
		echo "得到网站分类总数为:" .$f->GetWebSiteCount(). "\n";
	}
}
Main::run();
?>
//php Flyweight.php 
//网站分类:产品展示 用户:小菜
//网站分类:产品展示 用户:大鸟
//网站分类:产品展示 用户:娇娇
//网站分类:博客 用户:老顽童
//网站分类:博客 用户:六仙
//网站分类:博客 用户:南海
//得到网站分类总数为:2

转载请注明出处 http://yemaosheng.com