自己写的jsonServer jsonClient


  1. <?php
  2. /**
  3.  * 调用TEA加密算法,对所有Service目录下的类,都实例化之后调用。
  4.  * @author 肖江
  5.  * @filesource  service/0
  6.  * @classname Json Server调用
  7.  * @copyright 2010-2-4
  8.  */
  9. class SjsonServer {
  10.  static $jsonDecode; //jsonDecode类
  11.  static $serverInfo=array(); //解密段密码
  12.  static $domain; //域名
  13.  static $myClass; //要执行的className
  14.  function __construct() {
  15.   self::$jsonDecode=new Btea();
  16.  }
  17.  function setKeyPasswd($domain) {
  18.   self::$serverInfo[‘keyPasswd’]=l(‘FmyRpc’)->$domain;
  19.   self::$domain=$domain;
  20.  }
  21.  /**
  22.   * 设置class 供外部使用
  23.   * @author 肖江
  24.   * @param $class
  25.   * @return unknown_type
  26.   */
  27.  function setClass($class) {
  28.   self::$myClass=$class;
  29.   return $this;
  30.  }
  31.  /**
  32.   * 主函数,由index调用。
  33.   * @author 肖江
  34.   * @param $className
  35.   * @return unknown_type
  36.   */
  37.  function run($className) {
  38.   try {
  39.   $this->setClass($className);
  40.   $this->parseData($_REQUEST);
  41.   $GLOBALS[‘domain’]=self::$domain;
  42.   $classObject=new $className; //load
  43.   $param=isset($_REQUEST[‘Data’])?json_decode($_REQUEST[‘Data’],true):;
  44.   if(!is_array($param)) $param=array();
  45.   <a href="mailto:$result=@call_user_func_array(array($classObject,self::$serverInfo['method'">$result=@call_user_func_array(array($classObject,self::$serverInfo['method'</a>]), $param);
  46. //  $debug=new BdebugE();
  47. //  $debug->showErr('JsonServer 执行了.',$className."\r\n结果:\r\n".print_r($result,true),1);
  48.   echo json_encode(array('Return'=>$result)); //输出执行结果
  49.   } catch (dherror $err) {
  50.    echo json_encode(array('Error'=>$err->getMessage()))//忽略showMode 因为json 服务器不能用debugE,errCode也无法让client对照。
  51.   }
  52.  }
  53.  /**
  54.   * 检查Data
  55.   * @author 肖江
  56.   * @param $data
  57.   * @return unknown_type
  58.   */
  59.  function parseData($data) {
  60.   if(!isset($data['Domain'])) $this->error('未找到预先定义的domain字段',10055);
  61.   else $this->setKeyPasswd($data['Domain']);
  62.   if(!isset($data['Method'])) $this->error('未找到预先定义的method字段',10056);
  63.   else {
  64.    if(!isset($data['SecureData'])) $this->error('未找到预先定义的SecureData字段',10061);
  65.    else self::$serverInfo['method']=$this->getMethod($data['Method'],$data['SecureData']);
  66.   }
  67.  }
  68.  /**
  69.   * 获取方法名
  70.   * @author 肖江
  71.   * @param $methodStr
  72.   * @return unknown_type
  73.   */
  74.  function getMethod($methodStr,$secureCode) {
  75.   //$method=self::$jsonDecode->decrypt($methodStr,self::$serverInfo['keyPasswd']);
  76.   if(md5($methodStr.‘|’.self::$serverInfo[‘keyPasswd’])!=$secureCode)
  77.    $this->error(‘数据校验不通过’,10060);
  78.   $method=explode(‘.’,$methodStr);
  79.   if((time()-$method[0])>3600) $this->error(‘过期协议’,10057);
  80.   if(!isset($method[1])) $this->error(‘method定义出错’,10058);
  81.   return $method[1];
  82.  }
  83.  /**
  84.   * 错误抛出
  85.   * @author 肖江
  86.   * @param $errStr
  87.   * @param $errCode
  88.   * @param $className
  89.   * @return unknown_type
  90.   */
  91.  function error($errStr,$errCode) {
  92.   throw new dhError ( $errStr,$errCode,‘接口 :’.self::$myClass,1 );
  93.  }
  94. }
  1.  
  2. <?php
  3. /**
  4.  *
  5.  * @author 肖江
  6.  * @test
  7.  */
  8. class SjsonClient {
  9.  static $baseInfo=array(); //基础信息
  10.  static $jsonDecode; //加密解密模块
  11.  static $curl; //请求类
  12.  /**
  13.   * 构造函数
  14.   * @author 肖江
  15.   * @param $url
  16.   * @param $domain
  17.   * @return unknown_type
  18.   */
  19.  function __construct($url,$domain) {
  20.   self::$jsonDecode=new Btea();
  21.   self::$curl=new Bcurl();
  22.   self::$baseInfo[‘url’]=$url;
  23.   self::$baseInfo[‘domain’]=$domain;
  24.   self::$baseInfo[‘keyPasswd’]=l(‘FmyRpc’)->$domain;
  25.   self::setArrMode();
  26.   if(strlen(self::$baseInfo[‘keyPasswd’])<1)
  27.    $this->error(‘未能找到相关网站密码’,10035,$url);
  28.  }
  29.  /**
  30.   * 设置client返回的是obj还是array
  31.   * @author 肖江
  32.   * @param boolean true是array false是object
  33.   * @return unknown_type
  34.   */
  35.  static function setArrMode($mode=true) {
  36.   self::$baseInfo[‘arrMode’]=$mode;
  37.   return $this
  38.  }
  39.  /**
  40.   * 内部函数
  41.   * @param string 函数名
  42.   * @param array 参数
  43.   */
  44.  function __call($funcName,$param) {
  45.   try {
  46.    $callRequest=$this->makeCall($funcName,$param);
  47.  
  48.    $result=self::$curl->init(array(‘url’ => self::$baseInfo[‘url’],
  49.                  ‘method’=>‘POST’,
  50.            ‘post_fields’=>$callRequest
  51.    ))->exec();
  52.           if(isset($result[‘body’])) return  json_decode($result[‘body’],self::$baseInfo[‘arrMode’]);
  53.   } catch (dhError $e) {
  54.    $e->showDebugE(); //处理showMode=1
  55.    $e->getMsg(); //处理showMode=0/2
  56.   }
  57.  }
  58.     /**
  59.   * 创建一次请求信息
  60.   * @author 肖江
  61.   * @param $funcName
  62.   * @param $param
  63.   * @return unknown_type
  64.   */
  65.  function makeCall($funcName,$param) {
  66.   $callRequest=array();
  67.   $callRequest[‘Domain’]=self::$baseInfo[‘domain’];
  68.   $callRequest[‘Method’]=time().‘.’.$funcName;
  69.   $callRequest[‘SecureData’]=md5($callRequest[‘Method’].‘|’.self::$baseInfo[‘keyPasswd’]);
  70.   $callRequest[‘Data’]=json_encode($param);
  71.   return $callRequest;
  72.  }
  73.  /**
  74.   * 错误抛出
  75.   * @author 肖江
  76.   * @param $errStr
  77.   * @param $errCode
  78.   * @param $className
  79.   * @return unknown_type
  80.   */
  81.  function error($errStr,$errCode,$className) {
  82.   throw new dhError ( $errStr,$errCode,$className,1 );
  83.  }
  84. }
  85.  
  1. No comments yet.
(will not be published)
  1. No trackbacks yet.