Posted By

xuanyan on 09/09/08


Tagged

class php5 kukufun xuanyan easybots


Versions (?)

php5 class for easybots


 / Published in: PHP
 

URL: http://www.easybots.cn

  1. <?php
  2.  
  3. /* EasyBots<http://www.easybots.cn> class by xuanyan <[email protected]> */
  4.  
  5. // example:
  6. // receive user message:
  7. // $token = 'xxxxx';
  8. // $param = EasyBots::getParam();
  9. // if ($param['token'] == $token)
  10. // {
  11. // // var_dump($param);
  12. // }
  13. // //exit;
  14. // ----------------------------
  15. // send message to user:
  16. // $oapi_id = 'xxxxx';
  17. // $ack = 'xxxxx';
  18. // $bot = new EasyBots($oapi_id, $ack, 'gtalk:[email protected]');
  19. // $bot->send('palapla');
  20. // // or
  21. // $bot2 = new EasyBots($oapi_id, $ack,
  22. // array(
  23. // 'gtalk:[email protected]',
  24. // ));
  25. // $bot2->send('palapla');
  26.  
  27. class EasyBots
  28. {
  29. const URL = 'http://www.easybots.cn/oapi_mo.net';
  30. private $oapi_id = null;
  31. private $ack = null;
  32. private $im = '';
  33.  
  34. public function __construct($oapi_id, $ack, $im)
  35. {
  36. $this->oapi_id = intval($oapi_id);
  37. $this->ack = trim($ack);
  38. $this->im = implode(',', (array)$im);
  39. }
  40.  
  41. public function send($msg)
  42. {
  43. $data = array(
  44. 'oapi_id' => $this->oapi_id,
  45. 'ack' => $this->ack,
  46. 'im' => $this->im,
  47. 'msg' => trim($msg)
  48. );
  49. $data = http_build_query($data);
  50. $opts = array (
  51. 'http' => array (
  52. 'method' => 'POST',
  53. 'header'=> "Content-type: application/x-www-form-urlencoded
  54. " .
  55. "Content-Length: " . strlen($data) . "
  56. ",
  57. 'content' => $data
  58. )
  59. );
  60.  
  61. $context = stream_context_create($opts);
  62. $html = @file_get_contents(self::URL, false, $context);
  63.  
  64. return (100 == intval($html));
  65. }
  66.  
  67. public static function getParam()
  68. {
  69. return array(
  70. 'e' => isset($_POST['e']) ? trim($_POST['e']) : '',
  71. 'msg' => isset($_POST['msg']) ? trim($_POST['msg']) : '',
  72. 'im' => isset($_POST['im']) ? trim($_POST['im']) : '',
  73. 'token' => isset($_POST['token']) ? trim($_POST['token']) : ''
  74. );
  75. }
  76. }
  77.  
  78. ?>

Report this snippet  

You need to login to post a comment.