/libs_server/server_common/api.php

http://na-agent.googlecode.com/ · PHP · 307 lines · 232 code · 53 blank · 22 comment · 22 complexity · 7cd9f8f1848ffe8e85255bcc5f7421f6 MD5 · raw file

  1. <?php
  2. /*
  3. *
  4. * This class implements all the features of VKontakte Server API
  5. */
  6. class Api {
  7. // api_id
  8. public $api_id = null;
  9. // secret key
  10. public $api_secret = null;
  11. // version
  12. public $v = "2.0";
  13. // format
  14. public $format = "JSON";
  15. // server
  16. public $server = "http://api.vkontakte.ru/api.php";
  17. // last error here
  18. public $error;
  19. function Api($api_id, $api_secret) {
  20. $this->api_id = $api_id;
  21. $this->api_secret = $api_secret;
  22. }
  23. private function makeSiganture(&$params) {
  24. $sorted_array = array();
  25. foreach ($params as $k => $v) {
  26. $sorted_array[] = $k . "=" . $v;
  27. }
  28. sort($sorted_array, SORT_STRING);
  29. return md5(join("", $sorted_array) . $this->api_secret);
  30. }
  31. private function fillCommonParams(&$params){
  32. $params['v'] = $this->v;
  33. $params['format'] = $this->format;
  34. $params['api_id'] = $this->api_id;
  35. $params['timestamp'] = time();
  36. $params['random'] = rand();
  37. $params['sig'] = $this->makeSiganture($params);
  38. }
  39. public function isAuthorizedCall($auth_key, $viewer_id) {
  40. return $auth_key == md5($this->api_id . "_" . $viewer_id . "_" . $this->api_secret);
  41. }
  42. public function addRating($uid, $rate, $message = null) {
  43. $params = array();
  44. $params['method'] = "secure.addRating";
  45. $params['uid'] = $uid;
  46. $params['rate'] = $rate;
  47. if (!empty($message)) {
  48. $params['message'] = $message;
  49. }
  50. $this->fillCommonParams($params);
  51. $response = json_decode($this->request($params));
  52. if (!empty($response->error)) {
  53. $this->error = $response->error->error_msg;
  54. return null;
  55. }
  56. return true;
  57. }
  58. public function setCounter($uid, $counter) {
  59. $params['method'] = "secure.setCounter";
  60. $params['uid'] = $uid;
  61. $params['counter'] = $counter;
  62. $this->fillCommonParams($params);
  63. $response = json_decode($this->request($params));
  64. if (!empty($response->error)) {
  65. $this->error = $response->error->error_msg;
  66. return null;
  67. }
  68. return true;
  69. }
  70. public function getAppBalanse() {
  71. $params = array();
  72. $params['method'] = "secure.getAppBalance";
  73. $this->fillCommonParams($params);
  74. $response = json_decode($this->request($params));
  75. if (!empty($response->error)) {
  76. $this->error = $response->error->error_msg;
  77. return null;
  78. }
  79. return (int)$response->response;
  80. }
  81. public function sendNotification($uids, $message) {
  82. $params = array();
  83. $params['method'] = "secure.sendNotification";
  84. $params['uids'] = join(",", $uids);
  85. $params['message'] = $message;
  86. $this->fillCommonParams($params);
  87. $json = $this->request($params);
  88. $response = json_decode($json);
  89. if (!empty($response->error)) {
  90. $this->error = $response->error->error_msg;
  91. return null;
  92. }
  93. return split(",", $response->response);
  94. }
  95. public function saveAppStatus($uid, $status) {
  96. $params = array();
  97. $params['method'] = "secure.saveAppStatus";
  98. $params['uid'] = $uid;
  99. $params['status'] = $status;
  100. $this->fillCommonParams($params);
  101. $json = $this->request($params);
  102. $response = json_decode($json);
  103. if (!empty($response->error)) {
  104. $this->error = $response->error->error_msg;
  105. return null;
  106. }
  107. return $response->response;
  108. }
  109. public function getAppStatus($uid) {
  110. $params = array();
  111. $params['method'] = "secure.getAppStatus";
  112. $params['uid'] = $uid;
  113. $this->fillCommonParams($params);
  114. $json = $this->request($params);
  115. $response = json_decode($json);
  116. if (!empty($response->error)) {
  117. $this->error = $response->error->error_msg;
  118. return null;
  119. }
  120. return $response->response;
  121. }
  122. public function getBalanse($uid) {
  123. $params = array();
  124. $params['method'] = "secure.getBalance";
  125. $params['uid'] = $uid;
  126. $this->fillCommonParams($params);
  127. $json = $this->request($params);
  128. $response = json_decode($json);
  129. if (!empty($response->error)) {
  130. $this->error = $response->error->error_msg;
  131. return null;
  132. }
  133. return (int)$response->response;
  134. }
  135. public function addVotes($uid, $count) {
  136. $params = array();
  137. $params['method'] = "secure.addVotes";
  138. $params['uid'] = $uid;
  139. $params['votes'] = $count;
  140. $this->fillCommonParams($params);
  141. $json = $this->request($params);
  142. $response = json_decode($json);
  143. if (!empty($response->error)) {
  144. $this->error = $response->error->error_msg;
  145. return null;
  146. }
  147. return (int)$response->response;
  148. }
  149. public function withdrawVotes($uid, $count) {
  150. $params = array();
  151. $params['method'] = "secure.withdrawVotes";
  152. $params['uid'] = $uid;
  153. $params['votes'] = $count;
  154. $this->fillCommonParams($params);
  155. $json = $this->request($params);
  156. $response = json_decode($json);
  157. if (!empty($response->error)) {
  158. $this->error = $response->error->error_msg;
  159. return null;
  160. }
  161. return (int)$response->response;
  162. }
  163. public function transferVotes($from, $to, $count) {
  164. $params = array();
  165. $params['method'] = "secure.transferVotes";
  166. $params['uid_from'] = $from;
  167. $params['uid_to'] = $to;
  168. $params['votes'] = $count;
  169. $this->fillCommonParams($params);
  170. $json = $this->request($params);
  171. $response = json_decode($json);
  172. if (!empty($response->error)) {
  173. $this->error = $response->error->error_msg;
  174. return null;
  175. }
  176. return (int)$response->response;
  177. }
  178. public function getTransactionsHistory($type, $from, $to, $from_date, $to_date, $limit = 1000) {
  179. $params = array();
  180. $params['method'] = "secure.getTransactionsHistory";
  181. $params['uid_from'] = $from;
  182. $params['uid_to'] = $to;
  183. $params['type'] = $type;
  184. $params['date_from'] = $from_date;
  185. $params['date_to'] = $to_date;
  186. $params['limit'] = $limit;
  187. $this->fillCommonParams($params);
  188. $json = $this->request($params);
  189. $response = json_decode($json);
  190. if (!empty($response->error)) {
  191. $this->error = $response->error->error_msg;
  192. return null;
  193. }
  194. // TODO: make a transaction info
  195. $response->response;
  196. }
  197. private function request(&$params) {
  198. if (empty($this->server)) {
  199. return false;
  200. }
  201. $url_parsed = parse_url($this->server);
  202. $host = $url_parsed["host"];
  203. $port = 80;
  204. $path = $url_parsed["path"];
  205. if ( empty( $path ) ) {
  206. $path="/";
  207. }
  208. /*if ( $url_parsed['query'] != "" ) {
  209. $path .= "?";
  210. // URL-encode the query (encode '@', ' ', etc)
  211. $query_str = $url_parsed["query"];
  212. $query_params = explode('&', $query_str);
  213. foreach ($query_params as $param) {
  214. list($name, $value) = explode('=', $param, 2);
  215. $params[$name] = $value;
  216. }
  217. }*/
  218. $path .= ("?" . http_build_query($params));
  219. $out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
  220. $fp = fsockopen( $host, $port, $errno, $errstr, 30 );
  221. if ( $fp ) {
  222. fwrite( $fp, $out );
  223. $return = '';
  224. while ( !feof( $fp ) ) {
  225. $return .= fgets($fp, 1024);
  226. }
  227. fclose( $fp );
  228. return $this->getXmlResponse($return);
  229. } else {
  230. return null;
  231. }
  232. }
  233. private function getXmlResponse($response) {
  234. $content = '';
  235. $str = strtok($response, "\n");
  236. $h = null;
  237. while ($str !== false) {
  238. if ($h and trim($str) === '') {
  239. $h = false;
  240. continue;
  241. }
  242. if ($h !== false and false !== strpos($str, ':')) {
  243. // skip headers
  244. $h = true;
  245. }
  246. if ($h === false) {
  247. $content .= $str."\n";
  248. }
  249. $str = strtok("\n");
  250. }
  251. return trim($content);
  252. }
  253. }
  254. ?>