PHP code example of dongdavid / notify

1. Go to this page and download the library: Download dongdavid/notify library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dongdavid / notify example snippets


function offical($account)
{
    $data = [
        'access_token'=>$account['wechatoffical']['token'],
        'openid'=>$account['wechatoffical']['openid'],
        'template_id'=>$account['wechatoffical']['template_id'],
        'data'=>[
            'first'=>['value'=>'first is me'],
            'keyword1'=>['value'=>'哈哈哈'],
            'keyword2'=>['value'=>number_format(23.32,2)],
            'remark'=>['value'=>'备注信息'],
        ],

    ];


    $d = QuickSend::offical($data['access_token'],$data['openid'],$data['template_id'],$data['data']);
    var_dump($d);
}
function mini($account)
{
    $data = [
        'access_token'=>$account['miniprogram']['token'],
        'openid'=>$account['miniprogram']['openid'],
        'template_id'=>$account['miniprogram']['template_id'],
        'data'=>[
            'thing1'=>['value'=>'first is me'],
            'thing3'=>['value'=>'哈哈哈'],
            'time2'=>['value'=>23.32],
        ],

    ];

    $d = QuickSend::miniProgram($data['access_token'],$data['openid'],$data['template_id'],$data['data']);
    var_dump($d);
}
function email($account)
{
    // to、cc、bcc 可以支持字符串、数组,如果是二维数组, 则子数组必须要有键[email]保存邮箱地址
    // attachments 支持字符串、数组,如果是二维数组,则子数组必须要有键[filepath]来保存文件路径,会自动转换成绝对路径
    // config参考phpmailer,
    $data = [
        'config'=>$account['mail'],
        'subject'=>'测试邮件',
        'body'=>'邮件正文',
        'to'=>[
            '[email protected]',
        ],
        'cc'=>[
            ['email'=>'[email protected]','name'=>'收件人昵称'],
        ],
        'bcc'=>'[email protected]',
        'attachments'=>[
            './aa.md',
            ['filepath'=>'./bb.md','filename'=>'说明文件.txt']
        ],
        //'attachments'=>'./aa.md',
    ];
    $d = QuickSend::mail($data['config'],$data['subject'],$data['body'],$data['to'],$data['attachments'],$data['cc'],$data['bcc']);
    var_dump($d);
}
function alisms($account)
{
    $data = [
        'config'=>[
            'accessKeyId'=>$account['alisms']['accessKeyId'],
            'accessKeySecret'=>$account['alisms']['accessKeySecret'],
            'SignName'=>$account['alisms']['SignName'],
            'template_code'=>$account['alisms']['template_code'],
        ],
        'phone'=>$account['alisms']['phone'],
        'template_param'=>[
            'name'=>'皮皮虾',
            'course_name'=>'如何上天',
        ],
    ];
    $d = QuickSend::alisms($data['config'],$data['phone'],$data['template_param']);
    var_dump($d);
}


# 修改配置
\Dongdavid\Notify\cache(
    // 默认配置
    'host'     => '127.0.0.1',
    'port'     => '6379',
    'password' => '',
    'select'   => 6,
    'timeout'  => 3,
])

$mail = [
    'type'   => 'email',
    'config' => [
        'type'       => 'email',
        'signature'  => 'email_tx',
        'host'       => 'smtp.exmail.qq.com',
        'port'       => '465',
        'username'   => '[email protected]',
        'password'   => '', //腾讯的要用专用密码登陆
        'SMTPSecure' => 'ssl',
        'fromEmail'  => '[email protected]',
        'fromName'   => 'name',
    ],
    'msg'    => [
        'subject'     => '邮件主旨',
        'body'        => '邮件内容',
        'touser'      => [
            [
                'emailAddress' => '[email protected]',
                'name'         => '收件人名字',
            ],
        ],
        'cc'          => [],
        'attachments' => [
            './public/robots.txt',
        ],
    ],
];

Notify::send($mail);

$qy = [
    'type'   => 'wechatwork',
    'config' => [
        'type'      => 'wechatwork',
        'signature' => 'work_xxx_1000002',
        'appid'     => 'xxx',
        'appsecret' => 'xxxxx',
        'agentid'   => '1000002',
    ],
    'msg'    => [
        'touser'   => '001',
        'msgtype'  => 'textcard',
        'textcard' => [
            'title'       => '测试通知',
            'description' => "我是通知内容\n第二行开始了\n加上一个链接<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>",
            'url'         => 'https://www.dongdavid.com',
        ],
    ],
];

Notify::send($qy);

$wechat = [
    'type'   => 'wechatoffical',
    'config' => [
        'type'      => 'wechatoffical',
        'signature' => 'offical_xxx',
        'appid'     => 'xxx',
        'appsecret' => 'xxxxx',
    ],
    'msg'    => [
        'touser'      => 'openid',
        'template_id' => 'template_id',
        'url'         => 'https://www.dongdavid.com',
        'miniprogram' => [
            'appid'    => '',
            'pagepath' => '',
        ],
        'data'        => [
            'first'    => [
                'value' => '哈喽,我是first one',
            ],
            'keyword1' => [
                'value' => '我是关键词一号',
                'color' => '#17317',
            ],
            'keyword2' => [
                'value' => '我是关键词二号',
            ],
            'remark'   => [
                'value' => '我是备注了啊',
            ],
        ],
    ],
];
Notify::send($notify);

$wechat = [
    'type'   => 'miniprogram',
    'config' => [
        'type'      => 'miniprogram',
        'signature' => 'mini_xxx',
        'appid'     => 'xxx',
        'appsecret' => 'xxxx',
    ],
    'msg'    => [
        'touser'            => 'oBEIa0cr36R6FTCppAvgKLoKG9FY',
        'template_id'       => '0OHAj375XtCEVwJaASmRv79c4KhlqzN_mtsmNn6qHGg',
        'page'              => '',
        'miniprogram_state' => 'developer', //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
        'data'              => [
            'thing1' => [
                'value' => '哈喽,我是first one',
            ],
            'time2'  => [
                'value' => date('Y-m-d H:i'),
            ],
        ],
    ],
];
Notify::send($notify);

if (!function_exists('config')) {
    function config($name, $value = null)
    {
        if (null !== $value) {
            \Dongdavid\Notify\cache('notify-config', $value);
        } else {
            return \Dongdavid\Notify\cache('notify-config');
        }
    }
} else {
    function config($name, $value = null)
    {
        return \config($name, $value);
    }
}

# 预定义一个config方法用于获取配置
function config($config_name)
{
    $config = [
        'notify_config'=>[
            'work_xxx_1000002' => [
                'type'      => 'wechatwork',
                'signature' => 'work_xxx_1000002',
                'appid'     => 'xxx',
                'appsecret' => 'xxxxx',
                'agentid'   => '1000002',
            ],
            'offical_xxx'      => [
                'type'      => 'wechatoffical',
                'signature' => 'offical_xxx',
                'appid'     => 'xxx',
                'appsecret' => 'xxx',
            ],
            'email_tx'                        => [
                'type'       => 'email',
                'signature'  => 'email_tx',
                'host'       => 'smtp.exmail.qq.com',
                'port'       => '465',
                'username'   => '[email protected]',
                'password'   => '', //腾讯的要用专用密码登陆
                'SMTPSecure' => 'ssl',
                'fromEmail'  => '[email protected]',
                'fromName'   => 'name',

            ],
            'mini_xxx'         => [
                'type'      => 'miniprogram',
                'signature' => 'mini_xxx',
                'appid'     => 'xxx',
                'appsecret' => 'xxxxx',
            ],
        ]
    ];
    return $config[$config_name];
}
# 动态注入配置 
\Dongdavid\Notify\config('notify_config',$configs);

$data = [
    'type'   => 'email',
    'config' => 'email_tx',
    'msg'    => [
        .
        .
        .
    ],
];

$data = [
    'type'   => 'wechatwork',
    'config' => 'work_xxx_1000002',
    'msg'    => [
        .
        .
        .
    ],
];
$data = [
    'type'   => 'wechatoffical',
    'config' => 'offical_xxx',
    'msg'    => [
        .
        .
        .
    ],
];
$data = [
    'type'   => 'miniprogram',
    'config' => 'mini_xxx',
    'msg'    => [
        .
        .
        .
    ],
];

Notify::send($data);