/fuel/app/classes/commons.php

https://bitbucket.org/kawadatetsuya/twilio · PHP · 143 lines · 89 code · 21 blank · 33 comment · 5 complexity · 39c7a862e2a137ecd7e1e0768b4eac3d MD5 · raw file

  1. <?php
  2. /*
  3. * common methods
  4. */
  5. class Commons{
  6. /*
  7. * validation
  8. * @param $json string // JSON Raw data
  9. * @param $exception bool // true: if data was not valid then throw exception
  10. * @return bool // is valid is true
  11. */
  12. public static function isValid( $json, $exception = false ){
  13. \Log::debug('start Common::isValid');
  14. $parsed = json_decode( $json );
  15. $message = false;
  16. $status = '000';
  17. if( isset( $parsed->status ) ){
  18. $status = $parsed->status;
  19. switch( $parsed->status ){
  20. case '200':
  21. return true;
  22. break;
  23. case '400':
  24. $message = 'API Request Parameter Wrong: '. $json;
  25. break;
  26. case '403':
  27. $message = 'API Access Denied: '. $json;
  28. break;
  29. case '500':
  30. $message = 'API Internal Server Error: '. $json;
  31. break;
  32. case '503':
  33. $message = 'API Salesforce Access Denied: '. $json;
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. else
  40. $message = "Unknown Status: ". $json;
  41. if( $message ){
  42. \Log::info('Throw Exception: '. $json);
  43. throw new Exception($message, $status);
  44. }
  45. else{
  46. $message = 'Invalid Parameter: '. $json;
  47. \Log::warning($message);
  48. }
  49. if( $exception )
  50. throw new Exception($message, $status);
  51. else
  52. return false;
  53. }
  54. /*
  55. * get NWF(CMS) data
  56. *
  57. * @param $kindOfApi string // kind of api ["search", "estimate"] reference: app/config/app.php
  58. * @param $queries array // query strings
  59. * @param $method string // kind of form method ["GET", "POST"]
  60. * @return $json string // json string
  61. * @exception Exception // API Access Error
  62. */
  63. public static function getNWFJson( $kindOfApi = 'search', $queries = array(), $method = "GET" ){
  64. // $requests = http_build_query( $queries, '', '&' );
  65. $ch = curl_init();
  66. switch( $method ){
  67. case "POST":
  68. case "post":
  69. curl_setopt($ch, CURLOPT_POST, $isPost);
  70. curl_setopt($ch, CURLOPT_POSTFIELDS, $queries);
  71. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  72. $uri = self::makeApiUri($kindOfApi);
  73. break;
  74. default:
  75. $uri = self::makeApiUri($kindOfApi, $queries);
  76. break;
  77. }
  78. \Log::info($method.": ".$uri);
  79. curl_setopt($ch, CURLOPT_URL, $uri);
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  81. curl_setopt($ch, CURLOPT_USERPWD, \Config::get('app.api.username') . ":" . \Config::get('app.api.password') );
  82. $json = curl_exec($ch);
  83. $infos = curl_getinfo ($ch);
  84. curl_close($ch);
  85. if( $infos['http_code'] !== 200 ){
  86. // var_dump( $infos );
  87. $message = 'Api Access denied: '. $uri;
  88. \Log::error($message);
  89. throw new Exception($message);
  90. }
  91. return $json;
  92. }
  93. /*
  94. * make request uri
  95. * @param $kindOfApi string // kind of api ["search", "estimate"] reference: app/config/app.php
  96. * @param $queries array // query strings
  97. * @param $uri
  98. */
  99. public static function makeApiUri( $kindOfApi = 'search', $queries = array() ){
  100. \Config::load('app');
  101. $return = null;
  102. // make api
  103. $base = \Config::get('app.api.baseUrl');
  104. $path = \Config::get('app.api.path.'. $kindOfApi);
  105. if( !$path ){
  106. $path = \Config::get('app.api.path.search');
  107. }
  108. /*
  109. $queries = array(
  110. "store_t_number" => "05011111111",
  111. );
  112. */
  113. $uri = Uri::create($base. $path, array(), $queries);
  114. return $uri;
  115. }
  116. public static function test(){
  117. echo "test";
  118. \Config::load('app');
  119. print_r( $api = \Config::get('app.api') );
  120. var_dump( \Config::get('app.api.baseUrl') );
  121. }
  122. }