PHP code example of mejinke / wechat

1. Go to this page and download the library: Download mejinke/wechat 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/ */

    

mejinke / wechat example snippets




//配置
$options = [
	'appid' => 'xx',
	'appsecret' => 'xx',
	'mchid' => 'xx',
	'key' => 'xx'
];
//例实化
$wechat = new \Wechat\Wechat($options);

$event = $wechat->service('Event');

//关注事件
$event->subscribe(function(\Wechat\Component\Event $e){
    //...
});

//取消关注事件
$event->unsubscribe(function(\Wechat\Component\Event $e){
    //..
});
//扫描带参数二维码事件
$event->scan(function(\Wechat\Component\Event $e){
    //..
});
//上报地理位置事件
$event->location(function(\Wechat\Component\Event $e){
    //..
});

//执行事件派发
$event->dispatch();

//下单
$order = new \Wechat\Component\PayOrder();
$order->setBody("xxxxx"); //标题
$order->setAttach("xxxx"); //附件值
$order->setOutTradeNo("xxxxx"); //订单号
$order->setTotalFee(10 * 100); //总金额
$order->setTimeStart(date("YmdHis")); //时间戳
$order->setTimeExpire(date("YmdHis", time() + 600)); //过期时间
$order->setTradeType('JSAPI'); //支付方式
$order->setOpenid("xxxxx"); //用户openid

$pay = $wechat->service('Pay');
//提交订单
$result = $pay->createOrder($order);

//获取JSAPI参数
$pay->getJsApiParameters($result);

//获取回调
$pay->notifyProcess(function($data){
	//....
});


$menu = new \Wechat\Component\Menu();

//添加一个View Button
$id = $menu->addViewButton('官网', 'http://www.xxxx.com');
//添加子菜单
$menu->addViewButton('主页', 'http://www.xxxx.com', $id);

//添加一个Click Button
$menu->addClickButton('最新', 'news');

//提交创建
$wechat->service('Menu')->create($menu);