php5 class for easybots


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /* EasyBots<http://www.easybots.cn> class by xuanyan <xunayan1983@gmail.com> */
  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:easybots@gmail.com');
  19. // $bot->send('palapla');
  20. // // or
  21. // $bot2 = new EasyBots($oapi_id, $ack,
  22. // array(
  23. // 'gtalk:easybots@gmail.com',
  24. // 'msn:easybots@hotmail.com'
  25. // ));
  26. // $bot2->send('palapla');
  27.  
  28. class EasyBots
  29. {
  30. const URL = 'http://www.easybots.cn/oapi_mo.net';
  31. private $oapi_id = null;
  32. private $ack = null;
  33. private $im = '';
  34.  
  35. public function __construct($oapi_id, $ack, $im)
  36. {
  37. $this->oapi_id = intval($oapi_id);
  38. $this->ack = trim($ack);
  39. $this->im = implode(',', (array)$im);
  40. }
  41.  
  42. public function send($msg)
  43. {
  44. $data = array(
  45. 'oapi_id' => $this->oapi_id,
  46. 'ack' => $this->ack,
  47. 'im' => $this->im,
  48. 'msg' => trim($msg)
  49. );
  50. $data = http_build_query($data);
  51. $opts = array (
  52. 'http' => array (
  53. 'method' => 'POST',
  54. 'header'=> "Content-type: application/x-www-form-urlencoded
  55. " .
  56. "Content-Length: " . strlen($data) . "
  57. ",
  58. 'content' => $data
  59. )
  60. );
  61.  
  62. $context = stream_context_create($opts);
  63. $html = @file_get_contents(self::URL, false, $context);
  64.  
  65. return (100 == intval($html));
  66. }
  67.  
  68. public static function getParam()
  69. {
  70. return array(
  71. 'e' => isset($_POST['e']) ? trim($_POST['e']) : '',
  72. 'msg' => isset($_POST['msg']) ? trim($_POST['msg']) : '',
  73. 'im' => isset($_POST['im']) ? trim($_POST['im']) : '',
  74. 'token' => isset($_POST['token']) ? trim($_POST['token']) : ''
  75. );
  76. }
  77. }
  78.  
  79. ?>

URL: http://www.easybots.cn

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.