PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/textyle/libs/me2day.api.php

http://xe-textyle.googlecode.com/
PHP | 63 lines | 53 code | 10 blank | 0 comment | 6 complexity | 9c4c0a9779c292809981e54a97ef2dd5 MD5 | raw file
  1. <?php
  2. class me2api {
  3. var $user_id = null;
  4. var $user_key = null;
  5. var $api_url = 'http://me2day.net:80';
  6. var $application_key = '537a368d9049d9e86b2b169d75a2a4c3';
  7. function me2api($user_id, $user_key) {
  8. $this->user_id = $user_id;
  9. $this->user_key = $user_key;
  10. }
  11. function _getNonce() {
  12. for($i=0;$i<8;$i++) $nonce .= dechex(rand(0, 15));
  13. return $nonce;
  14. }
  15. function _getAuthKey() {
  16. $nonce = $this->_getNonce();
  17. return $nonce.md5($nonce.$this->user_key);
  18. }
  19. function _getPath($method, $user_id = null) {
  20. if(!$user_id) return sprintf('/api/%s.xml', $method);
  21. return sprintf('/api/%s/%s.xml',$method, $user_id);
  22. }
  23. function _getContent($method, $user_id = null, $params = null) {
  24. $url = $this->api_url.$this->_getPath($method, $user_id);
  25. $auth = base64_encode($this->user_id.':'.$this->_getAuthKey());
  26. $arr_content = array();
  27. if(is_array($params) && count($params)) {
  28. foreach($params as $key => $val) {
  29. $arr_content[] = sprintf('%s=%s', $key, urlencode($val));
  30. }
  31. $body = implode('&',$arr_content);
  32. }
  33. $buff = FileHandler::getRemoteResource($url, $body, 3, 'GET', 'application/x-www-form-urlencoded',
  34. array(
  35. 'me2_application_key'=>$this->application_key,
  36. 'Authorization'=>'Basic '.$auth,
  37. )
  38. );
  39. return $buff;
  40. }
  41. function chkNoop() {
  42. $buff = $this->_getContent('noop');
  43. if(strpos($buff, '<code>0</code>')!==false) return new Object();
  44. return new Object(-1, $buff);
  45. }
  46. function doPost($body, $tags, $content_type = 'document') {
  47. $params = array('post[body]'=>$body, 'post[tags]'=>str_replace(',',' ',$tags), 'content_type'=>$content_type);
  48. $buff = $this->_getContent('create_post',$this->user_id,$params);
  49. if(preg_match('/<code>[0-9]+</',$buff)) return new Object(-1,$buff);
  50. return new Object();
  51. }
  52. }
  53. ?>