Base.class.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * Desc : Action base class
  4. *
  5. * Author: xuchang.zhang
  6. */
  7. class BaseAction{
  8. public function __construct(){}
  9. protected function getDbEntity(){
  10. global $_ONU_CONFIG;
  11. $db = new MysqlHandler($_ONU_CONFIG['_db_conf']);
  12. return $db;
  13. }
  14. public static function getResponse($status,$value){
  15. $rsp = array();
  16. if($status==0){
  17. $rsp['status']='ok';
  18. $rsp['val']=$value;
  19. }else{
  20. $rsp['status']= 'fail';
  21. $rsp['err_code']= $status;
  22. $rsp['err_msg'] = $value;
  23. }
  24. $jsonRes = json_encode($rsp);
  25. $cbPara = $_REQUEST['callback'];
  26. if(!empty($cbPara)){
  27. return "$cbPara($jsonRes)";
  28. }
  29. else{
  30. return $jsonRes;
  31. }
  32. }
  33. public static function makeRequest($url, $params,$timeout=3,$post=true) {
  34. $ch = curl_init();
  35. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  36. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  37. curl_setopt($ch, CURLOPT_URL, $url);
  38. curl_setopt($ch, CURLOPT_POST, $post);
  39. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  41. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  42. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  43. $result = curl_exec($ch);
  44. curl_close($ch);
  45. return $result;
  46. }
  47. }