PHP code example of techleeone / wechat-api
1. Go to this page and download the library: Download techleeone/wechat-api 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/ */
techleeone / wechat-api example snippets
// 如果是使用支持composer自动加载的框架(比如thinkphp,laravel),则无需> 'wxef3bbdf4ed9439c5', // 开发者ID(AppID)
'secret' => '7294422104de6147d818a09f5b4587fa', // 开发者密码(AppSecret)
'token' => '598T8YDF1N1AS5FF45H4F', // 令牌(Token)
'debug' => true, // 是否为调试模式,调试模式会记录日志
];
// 获取示例
$wechat = \TechOne\WechatApi\Wechat::init($config);
$wechat->access();
$message = $wechat->access();
$message = $wechat->access();
$message->content = '我收到你发的文本消息啦';
$message->response();
$message = $wechat->access();
$data = [
'ToUserName' => $message->toUserName,
'FromUserName' => $message->fromUserName,
'MsgType' => 'text',
'Content' => '这是回复的内容',
];
$messageResponse = \TechOne\WechatApi\Message::load($data);
$messageResponse->response();
$message = $wechat->access();
$data = [
'ToUserName' => $message->toUserName,
'FromUserName' => $message->fromUserName,
'MsgType' => 'news',
'Articles' => [
[
'title' => '快速上手——我用scrapy写爬虫(一)',
'description' => 'python的爬虫框架也很多,诸如pyspider 和 scrapy',
'pic_url' => 'http://www.tech1024.cn/uploads/images/scrapy354.png',
'url' => 'http://www.tech1024.cn/original/2951.html',
],
[
'title' => '保存数据到MySql数据库——我用scrapy写爬虫(二)',
'description' => '说了如何创建项目,并爬去网站内容,下面我们说一下如何保存爬去到的数据',
'pic_url' => 'http://www.tech1024.cn/uploads/images/scrapy354.png',
'url' => 'http://www.tech1024.cn/original/2959.html',
],
[
'title' => 'scrapy爬取慕课网全部免费课程——我用scrapy写爬虫(三)',
'description' => '简单用scrapy写了一个小demo,本篇文章主要目标是完整用scrapy爬取,慕课网所有免费的课程、标题、图片、地址、学习人数、难度、方向、分类、时长、评分、评论数等。',
'pic_url' => 'http://www.tech1024.cn/uploads/images/scrapy354.png',
'url' => 'http://www.tech1024.cn/original/2965.html',
],
[
'title' => '利用Flask和ECharts进行数据可视化——我用scrapy写爬虫(四)',
'description' => '我们对慕课网的课程进行了爬取,本文就对数据进行统计和可视化,让这些数据更直观的展现出来。',
'pic_url' => 'http://www.tech1024.cn/uploads/images/scrapy354.png',
'url' => 'http://www.tech1024.cn/original/2978.html',
],
],
];
$messageResponse = \TechOne\WechatApi\Message::load($data);
$messageResponse->response();
// 该接口已经实现:
// 第一步:用户同意授权,获取code
// 第二步:通过code换取网页授权access_token
// 可直接拉取用户信息
$userinfo = $wechat->oauth2()->userinfo();
$data = [
"button" => [
[
"type" => "view",
"name" => "百度一下",
"url" => "https://www.baidu.com/",
],
[
"type" => "view",
"name" => "导航",
"url" => "http://hao.tech1024.cn/",
],
[
"type" => "view",
"name" => "笔记",
"url" => "http://www.tech1024.cn/",
],
],
];
$wechat->menu()->create($data);
$wechat->menu()->get();
$wechat->menu()->delete();
$nextOpenid = 'oeuHrwSsNAUXvLemeusYduRMFGE0'; // 下一个openid,不传从第一个开始
$result = $wechat->user()->get($nextOpenid);
$openid = 'oeuHrwSsNAUXvLemeusYduRMFGE0';
$result = $wechat->user()->info($openid);
$openids = [
["openid" => "oeuHrwSsNAUXvLemeusYduRMFGE0", "lang" => "zh_CN"],
["openid" => "oeuHrwTwAOT5GN6K1oPxJXyygwUU"],
];
$result = $wechat->user()->infoBatchget($openids, 'zh_CN');
$openid = 'oeuHrwSsNAUXvLemeusYduRMFGE0';
$remark = '二傻子1'; // 备注名
$result = $wechat->user()->updateremark($openid, $remark);
$tagName = '北京'; // 标签名
$result = $wechat->tags()->create($tagName);
$result = $wechat->tags()->get();
$tagId = '101'; // 标签id
$tagName = '福建'; // 要修改为的标签名
$result = $wechat->tags()->update($tagId, $tagName);
$tagId = '101'; // 标签id
$result = $wechat->tags()->delete($tagId);
$tagId = '100'; // 标签id
$nextOpenid = 'oeuHrwSsNAUXvLemeusYduRMFGE0'; // 下一个openid 不传从第一个开始
$result = $wechat->tags()->user($tagId);
$result = $wechat->tags()->user($tagId, $nextOpenid);
$tagId = '102'; // 标签id
$openids = [
"oeuHrwSsNAUXvLemeusYduRMFGE0",
"oeuHrwTwAOT5GN6K1oPxJXyygwUU",
]; // 要修改标签的用户的openid
$result = $wechat->tags()->batchtagging($tagId, $openids);
$tagId = '102'; // 标签id
$openids = [
"oeuHrwSsNAUXvLemeusYduRMFGE0",
"oeuHrwTwAOT5GN6K1oPxJXyygwUU",
]; // 要取消标签的用户的openid
$result = $wechat->tags()->batchuntagging($tagId, $openids);
$openid = "oeuHrwSsNAUXvLemeusYduRMFGE0";
$result = $wechat->tags()->idlist($openid);