Zend Framework+smartyÓ÷¨ÊµÀýÏê½â

5年以前  |  阅读数:871 次  |  编程语言:PHP 

±¾ÎÄʵÀý½²ÊoÁËZend Framework+smartyÓè¡£ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º

Ò»¡¢Zend Framework¼o½e

Zend FrameworkʹÓÃÄ£ÐÍ-ÊÓͼ-¿ØÖÆÆ÷£¨Model-View-Controller£¨MVC£(C)£(C)½a¹¹¡£Õa¸oÓÃÀ´°ÑÄaµÄ³ÌÐoÖÀe³É²»Í¬²¿ÖʹµÃ¿ª*¢ºÍά»¤±aµÃÈÝÒס£

ÔËÐÐZend FrameworkÐeÒª£ºPHP 5.1.4 (»o¸u¸ß) ¡¢Web *þÎñÆ÷Ö§³Ö mod_rewrite¹¦ÄÜ£¬±¾ÊµÀý²ÉÓÃApache¡£ ´ÓÕaÀihttp://framework.zend.com/downloadÏÂÔØ Zend Framework,ÓÐÁ½ÖÖ¸ñʽ.zip»oÕß.tar.gz¡£

¶þ¡¢Zend FrameworkµÄÅaÖÃ

1¡¢Ä¿Â¼½a¹¹

ËaÈ»Zend Framework ûÓÐÇ¿ÇoʹÓÃÒ»¸o±e×¼µÄĿ¼½a¹¹£¬µ«ÊÇ»¹ÊÇÓÐÒ»Ð(C)ͨÓõÄĿ¼½a¹¹¡£Õa¸oĿ¼½a¹¹¼ÙÉeÄaÍeÈ«¿ØÖÆApache µÄÅaÖᣣ¨ÏÂÃaeÒÔ±¾»u×oÀý×Ó£¬¸÷λÐe¸u¾Ý×Ô¼ºµÄÇe¿o½øÐиu¸Ä£¬ÎÒµÄ*þÎñÆ÷µÄ¸uĿ¼ָÏoµÄÊÇWebÎļþ¼Ð£(C)

ÒýÓÃ:

Web/
test/
/webapp
/controllers
/models
/templates
/templates_c
/library
/webroot
/images
/js
/css

ÎÒÃÇÒѾ­°Ñ³ÌÐoÖеÄÄ£ÐÍ¡¢ÊÓͼºÍ¿ØÖÆÆ÷µÄÎļþÖÀeµ½²»Í¬µÄ×ÓĿ¼ÖС£Ö§³ÖµÄͼÏñ£¬½Å±¾ºÍCSS Îļþ±»´aeÅÔÚwebroot Ŀ¼ÏµIJ»Í¬×ÓĿ¼ÖС£ÏÂÔصÄZend Framework ÎļþÅÔÚ library Ŀ¼Ï¡£Èç¹uÎÒÃÇ»¹ÐeÒªÆaËu¿aÎļþ£¬¶¼¿ÉÒÔÅÔÚÕaÀi¡£ÔÚÕa¸oʵÀýÖУ¬ÎÒÃÇʹÓõ½ÁËSmartyÄ£°ae¼¼Êo£¬ËuÒÔSmarty µÄ¿aÎļþÎÒÃÇÒ²Ó¦¸Ã*ÅÔÚlibraryÎļþÏ£¡

2¡¢Æo¶¯Îļþ

  1. ÅaÖÃ.htaccess

ÎÒÃÇʹÓõ¥Ò»µÄÈe¿ÚÎļþindex.phpÀ´¶ÔÎÒÃǵijÌÐo½øÐÐÃÎÊ£¬Õa¸øÎÒÃÇÌa¹(C)Á˳ÌÐoÖÐËuÓÐÒ³ÃaeµÄÖÐÐĵa²¢È±£ÔËÐл¾³ÅaÖÃÕýÈ¡£ÎÒÃÇÓÃ.htaccess ÎļþÀ´ÊµÏÖÕa¸oÄ¿µÄ£¬Ìi¼Ó.htaccess ÎļþÔÚtest µÄ¸uĿ¼ÖУ¬ÄÚÈÝÈçÏ£º


    RewriteEngine on
    RewriteRule !".(js|ico|gif|jpg|png|css)$ index.php

2£(C)ÅaÖÃApache
Óe´Ëͬʱ£¬ÎÒÃÇ»¹ÐeÒª¶Ôapache½øÐÐÒ»Ð(C)ÉeÖ㬴o¿ªapacheµÄÅaÖÃÎļþ httpd.conf¡£

1¡¢ÕÒµ½¡°#LoadModule rewrite_module modules/mod_rewrite.so Õa¾a»°£¬°ÑÇ°ÃaeµÄ#È¥µo£¡

2¡¢È»ºoÔÙÕÒµ½¡°AllowOverride None ¸Ä³ÉAllowOverride All£¬ÖØÐÂÆo¶¯apache¼´¿É¡£

3¡¢Æo¶¯Îļþindex.php
index.php*ÅÔÚtestµÄ¸uĿ¼Ï£¬ÏÂÃaeÊÇindex.phpµÄÄÚÈÝ£º:


    <?php 
    //´o¿ª´iÎoÌaʾ
    error_reporting(E_ALL|E_STRICT); 
    //Ée¶¨Ê±Çø
    date_default_timezone_set('Asia/Shanghai'); 
    //Ö¸Ã÷ÒýÓÃÎļþµÄÂ*¾¶
    set_include_path('.' .
    PATH_SEPARATOR . './library/'.
    PATH_SEPARATOR . './webapp/models/'.
    PATH_SEPARATOR . get_include_path());
    //±ØÐeÊÖ¶¯¼ÓÔØLoader.php
    include "Zend/Loader.php"; 
    //×Ô¶¯¼ÓÔØÀa£¬Ê¹ÓÃʱ£¬Ö±½ÓʵÀý»¯Ê¹ÓÃ
    function __autoload($class){
    Zend_Loader::loadClass($class);
    }
    //getInstance()*½*¨ÓÃÀ´»ñÈ¡Ç°¶Ë¿ØÖÆÆ÷ʵÀý
    $frontController = Zend_Controller_Front::getInstance(); 
    //Ée¶¨Ç°¶ËÂ*ÓÉÆ÷µÄ¹¤×÷Ŀ¼
    $frontController->setControllerDirectory(array("default"=>'./webapp/controllers')); 
    //Å׳oÒi³£
    $frontController->throwExceptions(true); 
    //ÉeÖûuµØÖ*,*½±aÒÔºourlµÄÌøתÓû§,.×¢Òa£¬Çø*Ö´oСд£¡
    $frontController->setBaseUrl('/test'); 
    //ʹÓÃsmartyÄ£°aeÐe¹Ø±Õ±¾ÉiµÄÊÓͼÖuÊÖ. 
    $frontController->setParam('noViewRenderer', true); 
    // ¹Ø±Õ´iÎoÌaʾ£¬*¢ÉuÇeÇo´iÎoʱºo£¬×ªµ½ErrorControllerµÄerrorAction¿ØÖÆÆ÷
    //$frontController->throwExceptions(false);
    //¶Ô¡£¡£½øÐÐ×¢²a
    Zend_Registry::set('font', $frontController);
    //------------ÅaÖÃSmartyÄ£°ae ----------------
    include 'Smarty/Smarty.class.php'; 
    /**
    * ¶ÔsmartyÄ£°ae½øÐгoʼ»¯
    **/
    $views = new Smarty();
    //$views->left_delimiter = "{{";
    //$views->right_delimiter = "}}";
    $views->compile_dir = './webapp/templates_c';
    $views->cache_dir = './webapp/templates_c/cache_c';
    $views->template_dir = "./webapp/templates";
    function smarty_block_dynamic($param,$content,&$views)
    {
    return $content;
    } 
    $views->register_block('dynamic','smarty_block_dynamic',false);
    Zend_Registry::set('views', $views);
    //¿ªÊ¼ÔËÐгÌÐo
    $frontController->dispatch(); 
    ?>

4£(C)Æo¶¯Îļþ˵Ã÷

Zend Framework ÊÇÕaÑuÉe¼ÆµÄ£¬ËuÓеÄÎļþ±ØÐe°uº¬ÔÚ include_path ÖС£ÎÒÃÇÒ²°ÑÎÒÃǵÄÄ£ÐÍĿ¼°uº¬ÔÚ include path ÖУ¬ÕaÑuÎÒÃÇÒÔºo¾ÍÄܺÜÈÝÒ×¼ÓÔØÎÒÃǵÄÄ£ÐÍÀa¡£Ò»¿ªÊ¼£¬ÎÒÃDZØÐe include Zend/Loader.php£¬ÕaÑuÎÒÃǾÍÄÜÃÎÊZend_Loader Àa,ÔÚZend_Loader ÀaÖÐÓо²Ì¬½*¨Ê¹ÎÒÃÇÄܹ»¼ÓÔØÆaËuZend Framework Àa£¬ÀýÈ磺


    Zend_Loader::loadClass('Zend_Controller_Front');

Zend_Loader::loadClass ¼ÓÔØÒѾ­ÃuÃuµÄÀa¡£ËuÊÇ°ÑÏ»®Ïßת»»³É¾¶¸oÀeuÀ´ÊµÏֵģ¬²¢ÔÚ×iºo¼ÓÉÏ.php ºo׺¡£ÕaÑu£¬ÀaZend_Controller_Front ½«´ÓZend/Controller/font.php ¼ÓÔØ¡£Èç¹uÄaÔÚÄaµÄÀa¿aÀiʹÓÃÏaͬµÄÃuÃu¹aeÔo£¬¾Í¿ÉÒÔÓÃZend_Loader::loadCass()À´¼ÓÔØËuÃÇ¡£ÎÒÃÇÐeÒª¼ÓÔØ¿ØÖÆÆ÷ÀaºÍÂ*ÓÉÀa¡£

Ç°¶Ë¿ØÖÆÆ÷ÓÃÂÓÉÀaÀ´Ó³ÉaÇeÇoµÄ URL µ½Õý鵀 PHP º¯Êý£¬È»ºoÏÔʾҳÃae¡£ÎªÁËÄÜʹÂÓɹ¤×÷£¬ÐeÒª½a¾o URL µÄÄÄÒ»²¿ÖÊÇÖ¸Ïoindex.php µÄÂ*¾¶£¬ÕaÑuËu¾Í¿ÉÒÔÔÚÄǸoµaºoÃaeÑ°ÕÒurl ÔªËØ¡£

ÎÒÃÇÐeÒªÅaÖÃÇ°¶ËÂ*ÓÉÆ÷£¬ÕaÑuËu¾ÍÖªµÀ´ÓÄĸoĿ¼ÀiÕÒ³oÎÒÃǵĿØÖÆÆ÷¡£


    $frontController = Zend_Controller_Front::getInstance(); 
    $frontController->setControllerDirectory('./application/controllers');

ÉeÖÃÅ׳oÒi³££¬µ«ÊÇÔÚ*þÎñÆ÷ÕaeÕý¹¤×÷ÒÔºo£¬ÎÒÃDz»Ó¦¸ÃÏÔʾ´iÎoÐÅÏ¢¸øÓû§¿´¡£


    $frontController->throwExceptions(true);

ÓÉÓÚÔÚ±¾ÊµÀýÖÐÎÒÃÇʹÓÃSmartyÄ£°ae¼¼Êo¡£ËuÒÔÎÒÃǹرÕZF±¾Éi×Ô´øµÄview¡£$frontController->setParam('noViewRenderer', true);ÉeÖûuµØÖ,½±aÒÔºoÉeÖÃurlÀ´½øÐÐÌøת¡£$frontController->setBaseUrl('/test');Zend_Registry::set('font', $frontController);½ÓÏÂÀ´£¬ÎÒÃǶÔSmarty½øÐÐÉeÖá£Ê×ÏÈÎÒÃÇÔÚÀa¿aÖÐÒýÓÃÁËSmarty.class.phpÕa¸oÀa¡£²¢ÇÒ¶ÔËuµÄÂ*¾¶½øÐÐÁËÉeÖã¬ÒÔ±aÈÃZFÖªµÀËuµÄλÖá£:


    include 'Smarty/Smarty.class.php'; 
    /**
    * ¶ÔsmartyÄ£°ae½øÐгoʼ»¯
    **/
    $views = new Smarty();
    //$views->left_delimiter = "{{";
    //$views->right_delimiter = "}}";
    $views->compile_dir = './webapp/templates_c';
    $views->cache_dir = './webapp/templates_c/cache_c';
    $views->template_dir = "./webapp/templates";
    function smarty_block_dynamic($param,$content,&$views)
    {
    return $content;
    } 
    $views->register_block('dynamic','smarty_block_dynamic',false);

ÔÚÕaÀi£¬ÎÒÃÇÓÃZFµÄ¶ÔÏo×¢²a±i(Registry)À´¶Ô$viewÀ´½øÐдaeÖu£¬ÕaÑu£¬ÔÚ³ÌÐoµÄÈκζÔ*½£¬ÎÒÃǶ¼¿ÉÒÔµ÷ÓÃËuÀ´½øÐвÙ×÷¡£Zend_Registry::set('views', $views);ÉeÖúÃÁËÒÔºo£¬ÔËÐгÌÐo¡£$frontController->dispatch();

Õa¸oʱºo£¬ÄaÈç¹uÔËÐÐhttp://127.0.0.1/test À´½øÐвaÊÔ¡£»a*¢ÏÖÓиo´iÎoÀaËÆ Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index)' in¡­Õa¸oÊÇÒoΪÎÒÃÇ»¹Ã»ÓÐÉeÖúÃÎÒÃdzÌÐo¡£

3¡¢ÉeÖóÌÐo

ÔÚÉeÖÃÎļþÒÔÇ°£¬Ài½aZend Framework ÈçºÎ×eÖ¯Ò³ÃaeºÜÖØÒª¡£Ã¿¸oÓ¦ÓóÌÐoµÄÒ³Ãae½Ð×o action £¬Ði¶a action ×e³É¿ØÖÆÆ÷¡£ÀýÈ磬¶ÔÓÚÕaÑuÒ»¸o¸ñʽµÄ URL http://localhost/test/news/view/id/1 À´Ëµ£¬¿ØÖÆÆ÷ÊÇnews, action ÊÇview,ºoÃaeµÄidºÍ1,*Ö±ðÊÇÍuÕa¸oactionView´«µÝµÄ²ÎÊýºÍÖµ¡£

Zend Framework ¿ØÖÆÆ÷°Ñ index ×÷Ϊһ¸oȱʡµÄaction ¶ø±£ÁoΪÌرðµÄaction¡£ÕaÑu£¬¶ÔÓÚhttp://localhost/test/news/ ÕaÑuµÄurl£¬ÔÚnews¿ØÖÆÆ÷ÀiµÄ index action½«±»Ö´ÐС£Zend Framework Ò²±£ÁoÁËÒ»¸oȱʡµÄ¿ØÖÆÆ÷£¬Ò²½Ð×oindex¡£ÕaÑu£¬http://localhost/test/ ½«Ö´ÐÐ index¿ØÖÆÆ÷Ï嵀 action index¡£

4¡¢ÉeÖÿØÖÆÆ÷

ÏÖÔÚ¿ÉÒÔÉeÖÿØÖÆÆ÷ÁË¡£ÔÚZend Framework Ài£¬¿ØÖÆÆ÷ÊÇÒ»¸o±ØÐe±»½Ð×o{Controller name}Controller µÄÀa¡£×¢Òa{Controller name}±ØÐeÒÔ´oд×Öĸ¿ªÍ¡£²¢ÇÒ£¬Õa¸oÀa±ØÐeÔÚ½Ð×o{Controller name}Controller.phpÕaÑuµÄÎļþÖУ¬Õa¸oÎļþ»¹±ØÐeÔÚÌض¨µÄ¿ØÖÆÆ÷Ŀ¼ÖС£Ç¿µ÷һϣ¬{Controller name}±ØÐeÒÔ´oд×Öĸ¿ªÍ²¢ÆaËu×Öĸһ¶¨ÊÇСд¡£Ã¿¸oactionÊÇÔÚ¿ØÖÆÆ÷ÀaÀiµÄpublic º¯Êý£¬Ãu×Ö±ØÐeÊÇ{action name}Action¡£ÔÚÕaÀi£¬{action name}Ó¦¸ÃÒÔСд×Öĸ¿ªÍ*¡£ÕaÑuÔÚÎļþ test/webapp/controllers/IndexController.php ÀiÎÒÃǵĿØÖÆÆ÷Àa½Ð×o IndexController£¬Î»Öãºtest/webapp/controllers/IndexController.php:


    <?php 
    class IndexController extends Zend_Controller_Action 
    { 
    function init()
    {
    } 
    function indexAction() 
    { 
    } 
    function addAction() 
    { 
    } 
    } 
    ?>

ÎÒÃÇÏÖÔÚÓÐÈý¸oÎÒÃÇÏeʹÓõÄaction£¬Ö±µ½ÎÒÃÇÉeÖúÃÊÓͼ£¬ËuÃDzŹ¤×÷¡£ÆaÖÐfunction initÊǸoÌØÊaµÄº¯Êý£¬¼oµ¥µÄ˵£¬Ëu¾ÍÊÇÔÚcontrollerÖеĹ¹Ôiº¯Êýʱµ÷Óõĺ¯Êý¡£

ÿ¸oactionµÄ URL ÈçÏ£º

http://localhost/test/ in IndexController::indexAction()
http://localhost/test/index/add in IndexController::addAction()

ÏÖÔÚ£¬ÎÒÃÇÔÚ³ÌÐoÀiÓиoÄܹ¤×÷µÄÂ*ÓÉÆ÷ºÍÿ¸oÒ³ÃaeµÄ action¡£

5¡¢ÉeÖÃÊÓͼ

ÒoΪ±¾ÊµÀýʹÓõĵÄÊÇSmartyÄ£°ae£¬ËuÒÔºÍZF±¾ÉiµÄViewÊÓͼÔÚʵÏÖ¹ý³ÌÖУ¬ÉÔ΢ÓеaÇø±ð£¡ÏÂÃaeÎÒÖ±½Ó½eÉÜÔÚZFÀiÊÇÈκÎʹÓÃSmartyµÄ¡£ÔÚʹÓÃSmarty֮ǰ£¬ÎÒÃÇÓ¦¸ÃÏÈÈ¡³oÔÚindex.phpÀi¶¨ÒaµÄ$view,²¢ÇÒ¶¨ÒaºÃ£¬ÐeÒªÔÚÄ£°aeÏÔʾµÄ±aÁ¿¡£:


    class IndexController extends Zend_Controller_Action 
    { 
    var $views; /*Ä£°a¶ÔÏo*/
    var $data; /*´«µÝÄ£°ae±aÁ¿µÄ¶ÔÏo*/
    function init()
    {
    //ÄûØ×¢²a¹ýµÄ¶ÔÏo
    $this->views = Zend_Registry::get('views'); 
    } 
    function indexAction() 
    { 
    //¶¨ÒaÄ£°aeÏÔʾµÄ±aÁ¿ 
    $data[£atitle¡a]=¨•hello world¨•£»
    //´«µÝ±aÁ¿µ½Ä£°ae
    $this->views->assign($data);
    //ÏÔʾģ°ae
    $this->views->display('index/index.tpl'); 
    } 
    function addAction() 
    { 
    } 
    }

ÏÂÃaeÎÒÃÇ¿ªÊ¼×oÊÓͼÎļþ£¬ËuÃǵÄλÖÃÊÇtest/webapp/templates/index/index.tpl:

´uÂe:


    {$title}

Õa¸oʱºo£¬ÊaÈehttp://127.0.0.1/test¿´¿´¡£Ó¦¸Ã»a³oÏÖ¡°hello world ÁË¡£

ÕaÑu£¬Ò»¸o¼oµ¥µÄʵÀý¾ÍÍe³ÉÁË¡£ÏÂÃaeÎÒÃǽaºÏXmlrpc¼¼ÊoÀ´ÊµÏÖÒ»¸oÉÔ΢¸´ÔÓÒ»µaµÄʵÀý£¡

Èý¡¢XMLRPC

1¡¢Ê²Ã´ÊÇXMLRPC

XMLRPC£¬¹ËÃu˼Òa,¾ÍÊÇÓ¦ÓÃÁËXML¼¼ÊoµÄRPC¡£ÄÇôʲôÊÇXMLºÍRPCÁË£¿

RPCÊÇRemote Procedure CallµÄËoд£¬­Òe³ÉÖÐÎľÍÊÇÔ¶³Ì¹ý³Ìµ÷Óã¬ÊÇÒ»ÖÖÔÚ±¾µØµÄ»uÆ÷Éϵ÷ÓÃÔ¶¶Ë»uÆ÷ÉϵÄÒ»¸o¹ý³Ì£¨½¨£(C)µÄ¼¼Êo£¬Õa¸o¹ý³ÌÒ²±»´o¼Ò³ÆΪ¡°Ö²¼Ê½¼ÆËa £¬ÊÇΪÁËÌa¸ß¸÷¸oÖÁ¢»uÆ÷µÄ¡°»¥²Ù×÷ÐÔ ¶ø¢Ã÷³oÀ´µÄ¼¼Êo¡£

XMLºÍRPCÒ»ÑuÒ²ÊÇÒ»¸o¶«Î÷µÄËoд£¬Õa¸o¶«Î÷¾ÍÊÇeXtensible Markup Language£¬ÖÐÎÄÒa˼¾ÍÊÇ¿ÉÀ(C)Õ¹±e¼ÇÓiÑÔ£¬±e¼ÇÓiÑÔ¾ÍÊÇÄÇÖÖÓüaÀ¨ºÅ£¨<>)À¨À´À¨È¥µÄÄÇÖÖÓiÑÔ£¬±ÈÈç˵HTML¡£XMLµÄ¿ÉÀ(C)Õ¹ÐÔÒ²ÌaÏÖÔÚËuÖ»¶¨ÒaÁËÓiÑԵĸñʽ£¬¶ø²¢Ã»Óж¨Òa¹ý¶aµÄ¹Ø¼u×Ö£¬Ò²¾ÍÊÇͨ³£Ëu˵µÄ±e¼Ç£¨Tag£(C)£¬ËuÒÔÓû§¿ÉÒÔ×ÔÓɵØÑ¡Ôñ¶¨Òa±e¼Ç¡£ËuµÄÕaÖÖ×ÔÓɺͼoµ¥µÄÓi*¨¹aeÔoҲʹµÃËu¹aΪÁ÷´«£¬ÓÃÀ´±iʾ¸÷ÖÖÊý¾Ý¡£

2¡¢ÔÚZFÖÐʹÓÃXMLRPC

1)´´½¨IndexController.php

ÏÂÃaeÎÒÃÇÀ´Íe³ÉÒ»¸oʵÀý£¬ÎªÁ˽±aÆð¼u£¬¾Í²»½¨Á¢ÐµÄController£¬°Ñ¸Õ²ÅÎÒÃǽ¨Á¢µÄIndexControllerÐÞ¸Äһϣ¬¾ÍÄÜʹÓÃÁË£¡ÁiÍaÎÒÃÇ»¹ÐeÒª½¨Á¢Ò»¸oXMLRPCµÄþÎñ¶Ë³ÌÐo¡£Î»ÖÃÔÚWEBþÎñÆ÷µÄ¸uĿ¼ÉÏ£¨ÔÚ±¾»uÖУ¬Ò²¾ÍÊÇÔÚtestÎļþµÄÉϼ¶Ä¿Â¼ÖУ¬È¡ÃuΪ1.php£(C)£¬ÓÉÓÚXMLRPCʹÓõ½ÁËÀa¿a£¬ÎÒÃÇ»¹ÐeÒªÏÂÔØlibphpxmlrpcÅÔÚlibraryÎļþ¼ÐÏ£¡

ÎļþλÖãºtest/webapp/controller/IndexController.php:


    class IndexController extends Zend_Controller_Action 
    {
    var $views; /*Ä£°a¶ÔÏo*/
    var $data; /*´«µÝÄ£°ae±aÁ¿µÄ¶ÔÏo*/
    public function init()
    {
    //ÄûØ×¢²a¹ýµÄ¶ÔÏo
    $this->views = Zend_Registry::get('views'); 
    $this->font = Zend_Registry::get('font'); 
    //µÃµ½»uµØÖ* 
    $this->baseurl=$this->font->getBaseUrl();
    }
    function indexAction() 
    { 
    @include "libphpxmlrpc/xmlrpc.inc";
    @include "libphpxmlrpc/xmlrpcs.inc"; 
    if (isset($_POST['var1']) && isset($_POST['var2'])) 
    { 
    //´´½¨¿Í»§¶Ë
    $client = new xmlrpc_client('http://127.0.0.1/1.php');
    //´´½¨Ò»¸oʵÀý
    @ $msg = new xmlrpcmsg("add", array(
    new xmlrpcval($_POST['var1'], "int"),
    new xmlrpcval($_POST['var2'], "int")));
    //*¢ËÍÐÅÏ¢£¬
    $response=$client->send($xmlrpc_message);£¬*þÎñÆ÷*µ»ØxmlrpcrespµÄÒ»¸oʵÀý
    $retval = $client->send($msg);
    if ($retval->faultCode()) 
    {
    print_r("*¢ÉuÒ»¸o´iÎo: "); 
    print_r("Ô­Òo: " . htmlspecialchars($retval->faultString())); 
    } 
    else 
    {
    //$retval->value()»ñÈ¡Ó¦´ðµÄxmlrpcval£¨Ò²¾ÍÊÇ*þÎñÆ÷¶Ë*µ»ØµÄ½a¹u£(C)£¬
    $retval->value()->scalarval();µÃµ½ÃeÊoÓ¦´ð½a¹uµÄPHP±aÁ¿
    $sum = $retval->value()->scalarval();
    }
    }
    @$data['var1']=$_POST['var1'];
    @$data['var2']=$_POST['var2'];
    @$data['sum']=$sum; 
    @$data[£aaction¡a]= "$this->baseurl/index/"; 
    //¹¹ÔiÍeÕuµÄurl¸øÄ£°ae 
    $time=date("Y-m-d H:i:s")
    @$data['url']="$this->baseurl/index/add/id/$sum/time/$time"; 
    /´«µÝ±aÁ¿µ½Ä£°ae
    $this->views->assign($data);
    //ÏÔʾģ°ae
    $this->views->display('index/index.tpl'); 
    } 
    function addAction() 
    { 
    $data['title']="ʵÑeÒ»ÏÂ"; 
    //µÃµ½´«µÝµÄÖµ 
    $id=$this->_request->getParam("id");
    $time=$this->_request->getParam("time"); 
    $data['id']="$id";
    $data['time']="$time";
    $this->views->assign($data);
    $this->views->display('index/add.tpl'); 
    }
    }

2£(C)´´½¨ÏÔʾģ°aeÎļþ

λÖãºtest/webapp/templates/index/index.tpl:

hello,ÏÂÃaeÑÝʾµÄÊÇÀuÓÃXmlrpcµ÷ÓÃÔ¶³ÌþÎñÆ÷½*¨µÄʵÀý£¡²¢ÇÒÎÒÃǰѵõ½µÄ½a¹u´«µÝµ½ÁiÍaµÄÒ»¸oº¯ÊýÖÐÈ¥£¡

´uÂe:


    {if $sum}
    µaһϿ´¿´£¡
    {/if}

λÖ㺠test/webapp/templates/index/add.tpl:


    ÏÖÔÚÊÇ{$time}
    {$title}Äa¸Õ²Å´«µÝµÄÊÇ {$id}

3£(C)´´½¨XMLRPC*þÎñÆ÷¶Ë³ÌÐo

λÖãºweb/1.php:


    <?php
    @include ("libphpxmlrpc/xmlrpc.inc");
    @include ("libphpxmlrpc/xmlrpcs.inc");
    if ($_SERVER['REQUEST_METHOD'] != 'POST')
    {
    exit(0);
    }
    $add_sig = array(array($xmlrpcString, $xmlrpcInt, $xmlrpcInt));
    $add_doc = "Add the two integer together";
    function add($params)
    {
    //ÒýÈeÓû§´iÎo´uÂeÖµ
    global $xmlrpcerruser;
    //*µ»ØÒ»¸oPHPÊý×e
    $val = php_xmlrpc_decode($params);
    $ret = $val[0] + $val[1];
    return new xmlrpcresp(new xmlrpcval($ret, "int"));
    }
    //´´½¨Ò»¸oxmlrpc_serverµÄʵÀý£º
    $server = new xmlrpc_server(array(
    "add" => array(
    "function" => "add",
    "signature" => $add_sig,
    "docstring" => $add_doc
    )));
    ?>

OK£¬ÏÖÔÚ´o¿ªhttp;//127.0.0.1/test/¿´¿´¡£¸Õ²Å½¨Á¢µÄÄǸoXMLRPCÓ¦¸ÃÒѾ­½¨Á¢ÆðÀ´ÁË£¬ÊaÈeÊý×Ö£¬²aÊÔһϰɣ¡

¸u¶a¹ØÓÚzendÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶Zend FrameWork¿o¼ÜÈeÃŽ̡̳¡¢¡¶[phpÓÅÐa¿ª¢¿o¼Ü×ܽa](http://www.jb51.net/Special/155.htm)¡¡¢¡¶Yii¿o¼ÜÈeÃż°³£Óü¼ÇÉ×ܽa¡¡¢¡¶ThinkPHPÈeÃŽ̡̳¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡

Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Ò»uÓÚzend framework¿o¼ÜµÄPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例