Return to Snippet

Revision: 8217
at September 9, 2008 03:37 by xuanyan


Initial Code
<?php

/* EasyBots<http://www.easybots.cn> class by xuanyan <[email protected]> */

// example:
// receive user message:
// $token = 'xxxxx';
// $param = EasyBots::getParam();
// if ($param['token'] == $token)
// {
//     // var_dump($param);
// }
// //exit;
// ----------------------------
// send message to user:
// $oapi_id = 'xxxxx';
// $ack = 'xxxxx';
// $bot = new EasyBots($oapi_id, $ack, 'gtalk:[email protected]');
// $bot->send('palapla');
// // or
// $bot2 = new EasyBots($oapi_id, $ack,
//                     array(
//                           'gtalk:[email protected]',
//                           'msn:[email protected]'
//                     ));
// $bot2->send('palapla');

class EasyBots
{
    const URL = 'http://www.easybots.cn/oapi_mo.net';
    private $oapi_id = null;
    private $ack = null;
    private $im = '';

    public function __construct($oapi_id, $ack, $im)
    {
        $this->oapi_id = intval($oapi_id);
        $this->ack = trim($ack);
        $this->im = implode(',', (array)$im);
    }

    public function send($msg)
    {
        $data = array(
                'oapi_id' => $this->oapi_id,
                'ack'     => $this->ack,
                'im'      => $this->im,
                'msg'     => trim($msg)
        );
        $data = http_build_query($data);
        $opts = array (
            'http' => array (
                'method' => 'POST',
                'header'=> "Content-type: application/x-www-form-urlencoded
" .
                           "Content-Length: " . strlen($data) . "
",
                'content' => $data
            )
        );

        $context = stream_context_create($opts);
        $html = @file_get_contents(self::URL, false, $context);

        return (100 == intval($html));
    }

    public static function getParam()
    {
        return array(
            'e'     => isset($_POST['e']) ? trim($_POST['e']) : '',
            'msg'   => isset($_POST['msg']) ? trim($_POST['msg']) : '',
            'im'    => isset($_POST['im']) ? trim($_POST['im']) : '',
            'token' => isset($_POST['token']) ? trim($_POST['token']) : ''
        );
    }
}

?>

Initial URL
http://www.easybots.cn

Initial Description

                                

Initial Title
php5 class for easybots

Initial Tags
class

Initial Language
PHP