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

访问webdav中的php文件时被解析

Standard
<VirtualHost *:80>
 ServerAdmin webmaster@yemaosheng.com
 DocumentRoot /web/dev/
 ServerName webdav.yemaosheng.com
 <Location />
  Dav On
 
  #加下面两行
  SetHandler default-handler
  ForceType text/plain
 
  Options Indexes FollowSymlinks Includes
 
  Order Allow,Deny
  Allow from all
 
  AuthType Basic
  AuthName "webdav"
  AuthUserFile "/etc/apache2/davuser.passwd"
  AuthBasicProvider file
  <LimitExcept PUT POST DELETE MOVE OPTIONS>
    Require user username
  </LimitExcept>
 </Location>
</VirtualHost>
 
htpasswd /etc/apache2/davuser.passwd username;

《大话设计模式》装饰模式-穿什么场景-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

《大话设计模式》中介者模式-联合国场景-PHP版代码

Standard

中介者模式
联合国场景

<?php
/**
 * 中介者模式 联合国场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
abstract class UnitedNations{
        abstract function Declares($message, Country $colleague);
}
 
abstract class Country{
        private $mediator;
 
        public function __construct(UnitedNations $mediator){
                $this->mediator = $mediator;
        }
        public function mediator(){
                return $this->mediator;
        }
}
 
class USA extends Country{
        public function __construct(UnitedNations $mediator){
                parent::__construct($mediator);
        }
        public function Declares($message){
                parent::mediator()->Declares($message, $this);
        }
        public function GetMessage($message){
                echo "美国获得对方信息:" . $message ."\n";
        }
}
class Iraq extends Country{
        public function __construct(UnitedNations $mediator){
                parent::__construct($mediator);
        }
        public function Declares($message){
                parent::mediator()->Declares($message, $this);
        }
        public function GetMessage($message){
                echo "伊拉克获得对方信息:" . $message ."\n";
        }
}
class UnitedNationsSecurityCouncil extends UnitedNations{
        private $colleague1;
        private $colleague2;
 
        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;
        }
 
        public function Declares($message, Country $colleague){
                if($colleague == $this->colleague1){
                        $this->colleague2->GetMessage($message);
                }else{
                        $this->colleague1->GetMessage($message);
                }
        }
}
class Main{
        public static function run(){
                $UNSC = new UnitedNationsSecurityCouncil();
 
                $c1 = new USA($UNSC);
                $c2 = new Iraq($UNSC);
 
                $UNSC->colleague1 = $c1;
                $UNSC->colleague2 = $c2;
 
                $c1->Declares("不准研制核武器,否则就打你!");
                $c2->Declares("我们没有研制核武器,但也不怕你打!");
        }
}
Main::run();
?>
//php Mediator.php  
//伊拉克获得对方信息:不准研制核武器,否则就打你!
//美国获得对方信息:我们没有研制核武器,但也不怕你打!

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