增加了新的代码文件,以完善软件功能

src
fanbo 2 months ago
parent 95742fda2f
commit a83b65e7f2

@ -1,69 +0,0 @@
<?php
namespace LaneWeChat\Core;
/**
* 微信Access_Token的获取与过期检查
* Created by Lane.
* User: lane
* Date: 13-12-29
* Time: 下午5:54
* Mail: lixuan868686@163.com
* Website: http://www.lanecn.com
*/
class AccessToken{
/**
* 获取微信Access_Token
*/
public static function getAccessToken(){
//检测本地是否已经拥有access_token并且检测access_token是否过期
$accessToken = self::_checkAccessToken();
if($accessToken === false){
$accessToken = self::_getAccessToken();
}
return $accessToken['access_token'];
}
/**
* @descrpition 从微信服务器获取微信ACCESS_TOKEN
* @return Ambigous|bool
*/
private static function _getAccessToken(){
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.WECHAT_APPID.'&secret='.WECHAT_APPSECRET;
$accessToken = Curl::callWebServer($url, '', 'GET');
if(!isset($accessToken['access_token'])){
return Msg::returnErrMsg(MsgConstant::ERROR_GET_ACCESS_TOKEN, '获取ACCESS_TOKEN失败');
}
$accessToken['time'] = time();
$accessTokenJson = json_encode($accessToken);
//存入数据库
/**
* 这里通常我会把access_token存起来然后用的时候读取判断是否过期如果过期就重新调用此方法获取存取操作请自行完成
*
* 请将变量$accessTokenJson给存起来这个变量是一个字符串
*/
$f = fopen('access_token', 'w+');
fwrite($f, $accessTokenJson);
fclose($f);
return $accessToken;
}
/**
* @descrpition 检测微信ACCESS_TOKEN是否过期
* -10是预留的网络延迟时间
* @return bool
*/
private static function _checkAccessToken(){
//获取access_token。是上面的获取方法获取到后存起来的。
// $accessToken = YourDatabase::get('access_token');
$data = file_get_contents('access_token');
$accessToken['value'] = $data;
if(!empty($accessToken['value'])){
$accessToken = json_decode($accessToken['value'], true);
if(time() - $accessToken['time'] < $accessToken['expires_in']-10){
return $accessToken;
}
}
return false;
}
}
?>

@ -1,400 +0,0 @@
<?php
namespace LaneWeChat\Core;
/**
* 高级群发接口
* User: lane
* Date: 14-10-27
* Time: 下午7:24
* E-mail: lixuan868686@163.com
* WebSite: http://www.lanecn.com
*/
class AdvancedBroadcast{
/**
* 上传图文消息素材 - 创建一个图文消息保存到微信服务器可以得到一个id代表这个图文消息发送的时候根据这个id发送就可以了
*
* @param $articles = array(
array('thumb_media_id'=>'多媒体ID由多媒体上传接口获得' , 'author'=>'作者', 'title'=>'标题', 'content_source_url'=>'www.lanecn.com', content=>'图文消息页面的内容支持HTML标签', 'digest'=>'摘要', 'show_cover_pic'=>'是否设置为封面0或者1'),
array('thumb_media_id'=>'多媒体ID由多媒体上传接口获得' , 'author'=>'作者', 'title'=>'标题', 'content_source_url'=>'www.lanecn.com', content=>'图文消息页面的内容支持HTML标签', 'digest'=>'摘要', 'show_cover_pic'=>'是否设置为封面0或者1'),
* )
*
* return mediaId 上传的图文消息的ID
*/
public static function uploadNews($articles){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
foreach($articles as &$article){
$article['author'] = urlencode($article['author']);
$article['title'] = urlencode($article['title']);
$article['content'] = urlencode($article['content']);
$article['digest'] = urlencode($article['digest']);
}
$template = array();
$template['articles'] = $articles;
$template = json_encode($template);
$template = urldecode($template);
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
return empty($result['media_id']) ? false : $result['media_id'];
}
/**
* 根据分组进行群发 - 发送图文消息
*
* @param $groupId Int 要发送的分组ID
* @param $mediaId String 必须通过self::uploadNews获得的多媒体资源ID
* @param $isToAll Bool 使用is_to_all为true且成功群发会使得此次群发进入历史消息列表。
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentNewsByGroup($groupId, $mediaId, $isToAll=false){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['filter']['group_id'] = $groupId;
$template['filter']['is_to_all'] = $isToAll;
$template['mpnews']['media_id'] = $mediaId;
$template['msgtype'] = 'mpnews';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据分组进行群发 - 发送文本消息
*
* @param $groupId 要发送的分组ID
* @param $content 文本消息的内容
* @param $isToAll Bool 使用is_to_all为true且成功群发会使得此次群发进入历史消息列表。
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentTextByGroup($groupId, $content, $isToAll=false){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['filter']['group_id'] = $groupId;
$template['filter']['is_to_all'] = $isToAll;
$template['text']['content'] = $content;
$template['msgtype'] = 'text';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据分组进行群发 - 发送语音消息
*
* @param $groupId 要发送的分组ID
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @param $isToAll Bool 使用is_to_all为true且成功群发会使得此次群发进入历史消息列表。
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentVoiceByGroup($groupId, $mediaId, $isToAll=false){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['filter']['group_id'] = $groupId;
$template['filter']['is_to_all'] = $isToAll;
$template['voice']['media_id'] = $mediaId;
$template['msgtype'] = 'voice';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据分组进行群发 - 发送图片消息
*
* @param $groupId 要发送的分组ID
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @param $isToAll Bool 使用is_to_all为true且成功群发会使得此次群发进入历史消息列表。
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentImageByGroup($groupId, $mediaId, $isToAll=false){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['filter']['group_id'] = $groupId;
$template['filter']['is_to_all'] = $isToAll;
$template['image']['media_id'] = $mediaId;
$template['msgtype'] = 'image';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据分组进行群发 - 发送视频消息
*
* @param $groupId 要发送的分组ID
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @param $isToAll Bool 使用is_to_all为true且成功群发会使得此次群发进入历史消息列表。
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentVideoByGroup($mediaId, $title, $description, $groupId, $isToAll=false){
//将根据基础支持中上传多媒体得到的mediaId转化为群发视频消息所需要的mediaId。
$queryUrl = 'https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['media_id'] = $mediaId;
$template['title'] = $title;
$template['description'] = $description;
$template = json_encode($template);
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
if(empty($result['type']) || $result['type'] != 'video' || empty($result['media_id'])){
return $result;
}
$mediaId = $result['media_id'];
//群发视频
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['filter']['group_id'] = $groupId;
$template['filter']['is_to_all'] = $isToAll;
$template['mpvideo']['media_id'] = $mediaId;
$template['msgtype'] = 'mpvideo';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据OpenID列表群发 - 发送图文消息
*
* @param $toUserList array(openId1, openId2, openId3)
* @param $mediaId String 必须通过self::uploadNews获得的多媒体资源ID
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentNewsByOpenId($toUserList, $mediaId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $toUserList;
$template['mpnews']['media_id'] = $mediaId;
$template['msgtype'] = 'mpnews';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据OpenID列表群发 - 发送文本消息
*
* @param $toUserList array(openId1, openId2, openId3)
* @param $content 文本消息的内容
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentTextByOpenId($toUserList, $content){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $toUserList;
$template['text']['content'] = $content;
$template['msgtype'] = 'text';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据OpenID列表群发 - 发送语音消息
*
* @param $toUserList array(openId1, openId2, openId3)
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentVoiceByOpenId($toUserList, $mediaId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $toUserList;
$template['voice']['media_id'] = $mediaId;
$template['msgtype'] = 'voice';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据OpenID列表群发 - 发送图片消息
*
* @param $toUserList array(openId1, openId2, openId3)
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentImageByOpenId($toUserList, $mediaId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $toUserList;
$template['image']['media_id'] = $mediaId;
$template['msgtype'] = 'image';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 根据OpenID列表群发 - 发送视频消息
*
* @param $toUserList array(openId1, openId2, openId3)
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function sentVideoByOpenId($toUserList, $mediaId, $title, $description){
//将根据基础支持中上传多媒体得到的mediaId转化为群发视频消息所需要的mediaId。
$queryUrl = 'https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['media_id'] = $mediaId;
$template['title'] = $title;
$template['description'] = $description;
$template = json_encode($template);
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
if(empty($result['type']) || $result['type'] != 'video' || empty($result['media_id'])){
return $result;
}
$mediaId = $result['media_id'];
//群发视频
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $toUserList;
$template['video']['media_id'] = $mediaId;
$template['video']['title'] = $title;
$template['video']['description'] = $description;
$template['msgtype'] = 'video';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 删除群发
* 请注意,只有已经发送成功的消息才能删除删除消息只是将消息的图文详情页失效,已经收到的用户,还是能在其本地看到消息卡片。 另外,删除群发消息只能删除图文消息和视频消息,其他类型的消息一经发送,无法删除。
*
* @param $msgId 发送出去的消息ID
* @return mixed array("errcode"=>0, "errmsg"=>"ok"} 正常是errcode为0
*/
public static function delete($msgId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['msg_id'] = $msgId;
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 预览 - 预览图文消息
*
* @param $openId String 发送消息给指定用户,该用户的OpenId
* @param $mediaId String 必须通过self::uploadNews获得的多媒体资源ID
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function previewNewsByGroup($openId, $mediaId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $openId;
$template['mpnews']['media_id'] = $mediaId;
$template['msgtype'] = 'mpnews';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 预览 - 预览文本消息
*
* @param $openId String 发送消息给指定用户,该用户的OpenId
* @param $content 文本消息的内容
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function previewTextByGroup($openId, $content){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $openId;
$template['text']['content'] = $content;
$template['msgtype'] = 'text';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 预览 - 预览语音消息
*
* @param $openId String 发送消息给指定用户,该用户的OpenId
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function previewVoiceByGroup($openId, $mediaId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $openId;
$template['voice']['media_id'] = $mediaId;
$template['msgtype'] = 'voice';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 预览 - 预览图片消息
*
* @param $openId String 发送消息给指定用户,该用户的OpenId
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function previewImageByGroup($openId, $mediaId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $openId;
$template['image']['media_id'] = $mediaId;
$template['msgtype'] = 'image';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 预览 - 预览视频消息
*
* @param $openId String 发送消息给指定用户,该用户的OpenId
* @param $mediaId 需通过基础支持中的上传下载多媒体文件来得到。Media::upload()中返回的media_id字段的值
* @return mixed array("errcode"=>0, "errmsg"=>"success","msg_id"=>34182} 正常是errcode为0
*/
public static function previewVideoByGroup($mediaId, $title, $description, $openId){
//将根据基础支持中上传多媒体得到的mediaId转化为群发视频消息所需要的mediaId。
$queryUrl = 'https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['media_id'] = $mediaId;
$template['title'] = $title;
$template['description'] = $description;
$template = json_encode($template);
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
if(empty($result['type']) || $result['type'] != 'video' || empty($result['media_id'])){
return $result;
}
$mediaId = $result['media_id'];
//群发视频
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $openId;
$template['mpvideo']['media_id'] = $mediaId;
$template['msgtype'] = 'mpvideo';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 查询群发消息发送状态【订阅号与服务号认证后均可用】
*
* @param $msgId String 群发消息后返回的消息id
* @return mixed array("msg_status":"SEND_SUCCESS","msg_id"=>34182)
*/
public static function getStatus($openId, $mediaId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $openId;
$template['image']['media_id'] = $mediaId;
$template['msgtype'] = 'image';
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
}

@ -1,21 +0,0 @@
<?php
namespace LaneWeChat\Core;
/**
* Created by lixuan-it@360.cn
* User: lane
* Date: 15/4/29
* Time: 上午10:51
* E-mail: lixuan868686@163.com
* WebSite: http://www.lanecn.com
*/
class Auth {
/**
* 获取微信服务器IP列表
*/
public static function getWeChatIPList(){
//获取ACCESS_TOKEN
$accessToken = AccessToken::getAccessToken();
$url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token='.$accessToken;
return Curl::callWebServer($url, '', 'GET');
}
}

@ -1,8 +1,9 @@
<?php <?php
namespace LaneWeChat\Core; namespace LaneWeChat\Core;
class AutoReply{ class AutoReply{
public static function getRole($industryId1, $industryId2){ public static function getRole($industryId1, $industryId2){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token='.AccessToken::getAccessToken(); $queryUrl = 'https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST'; $queryAction = 'POST';
$template = array(); $template = array();
$template['industry_id1'] = "$industryId1"; $template['industry_id1'] = "$industryId1";

@ -0,0 +1,137 @@
<?php
namespace LaneWeChat\Core;
class Curl {
private static $_ch;
private static $_header;
private static $_body;
private static $_cookie = array();
private static $_options = array();
private static $_url = array ();
private static $_referer = array ();
public static function callWebServer($queryUrl, $param='', $method='get', $is_json=true, $is_urlcode=true) {
if (empty($queryUrl)) {
return false;
}
$method = strtolower($method);
$ret = '';
$param = empty($param) ? array() : $param;
self::_init();
if ($method == 'get') {
$ret = self::_httpGet($queryUrl, $param);
} elseif($method == 'post') {
$ret = self::_httpPost($queryUrl, $param, $is_urlcode);
}
if(!empty($ret)){
if($is_json){
return json_decode($ret, true);
}else{
return $ret;
}
}
return true;
}
private static function _init() {
self::$_ch = curl_init();
curl_setopt(self::$_ch, CURLOPT_HEADER, true);
curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, true);
}
public static function setOption($optArray=array()) {
foreach($optArray as $opt) {
curl_setopt(self::$_ch, $opt['key'], $opt['value']);
}
}
private static function _close() {
if (is_resource(self::$_ch)) {
curl_close(self::$_ch);
}
return true;
}
private static function _httpGet($url, $query=array()) {
if (!empty($query)) {
$url .= (strpos($url, '?') === false) ? '?' : '&';
$url .= is_array($query) ? http_build_query($query) : $query;
}
curl_setopt(self::$_ch, CURLOPT_URL, $url);
curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);
$ret = self::_execute();
self::_close();
return $ret;
}
private static function _httpPost($url, $query=array(), $is_urlcode=true) {
if (is_array($query)) {
foreach ($query as $key => $val) {
if($is_urlcode){
$encode_key = urlencode($key);
}else{
$encode_key = $key;
}
if ($encode_key != $key) {
unset($query[$key]);
}
if($is_urlcode){
$query[$encode_key] = urlencode($val);
}else{
$query[$encode_key] = $val;
}
}
}
curl_setopt(self::$_ch, CURLOPT_URL, $url);
curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
curl_setopt(self::$_ch, CURLOPT_POST, true );
curl_setopt(self::$_ch, CURLOPT_POSTFIELDS, $query);
curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);
$ret = self::_execute();
self::_close();
return $ret;
}
private static function _put($url, $query = array()) {
curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'PUT');
return self::_httpPost($url, $query);
}
private static function _delete($url, $query = array()) {
curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
return self::_httpPost($url, $query);
}
private static function _head($url, $query = array()) {
curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
return self::_httpPost($url, $query);
}
private static function _execute() {
$response = curl_exec(self::$_ch);
$errno = curl_errno(self::$_ch);
if ($errno > 0) {
throw new \Exception(curl_error(self::$_ch), $errno);
}
return $response;
}
}
?>

@ -0,0 +1,65 @@
<?php
namespace LaneWeChat\Core;
Class CustomService{
public function addAccount($kfAccount, $nickname, $password){
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['kf_account'] = $kfAccount;
$template['nickname'] = $nickname;
$template['password'] = $password;
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
public function editAccount($kfAccount, $nickname, $password){
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/update?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['kf_account'] = $kfAccount;
$template['nickname'] = $nickname;
$template['password'] = $password;
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
public function delAccount($kfAccount){
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/del?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['kf_account'] = $kfAccount;
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
public function getAccountList(){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=' . AccessToken::getAccessToken();
$queryAction = 'GET';
return Curl::callWebServer($queryUrl, '', $queryAction);
}
public function setAccountImage($kfAccount, $imagePath){
if(!file_exists($imagePath)){
return false;
}
$queryUrl = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=' . AccessToken::getAccessToken() . '&kf_account=' . $kfAccount;
$data = array();
$data['media'] = '@' . $imagePath;
return Curl::callWebServer($queryUrl, $data, 'POST', 1 , 0);
}
public function getRecord($startTime, $endTime, $pageIndex=1, $pageSize=1000, $openId=''){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/customservice/getrecord?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['starttime'] = $startTime;
$template['endtime'] = $endTime;
$template['openid'] = $openId;
$template['pagesize'] = $pageSize;
$template['pageindex'] = $pageIndex;
$template = json_encode($template);
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
return isset($result['recordlist']) ? $result['recordlist'] : array();
}
}

@ -0,0 +1,20 @@
<?php
namespace LaneWeChat\Core;
class IntelligentInterface{
public static function semanticSemproxy($query, $category, $openId, $latitude='', $longitude='', $region='', $city=''){
$queryUrl = 'https://api.weixin.qq.com/semantic/semproxy/search?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['query'] = $query;
$template['category'] = $category;
$template['appid'] = WECHAT_APPID;
$template['uid'] = $openId;
if(!empty($latitude)) $template['latitude'] = $latitude;
if(!empty($longitude)) $template['longitude'] = $longitude;
if(!empty($region)) $template['region'] = $region;
if(!empty($city)) $template['city'] = $city;
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction, 0, 0);
}
}

@ -1,17 +1,61 @@
<?php <?php
namespace LaneWeChat\Core; namespace LaneWeChat\Core;
/**
* 多媒体文件上传与下载管理类
* 该类提供将图片、语音、视频等文件上传到微信服务器的功能,以及从微信服务器下载多媒体文件的功能。
*/
class Media { class Media {
/**
* 上传多媒体文件到微信服务器
* 此方法允许上传图片、语音、视频等文件到微信服务器并返回对应的media_id用于后续获取多媒体文件。
* 上传的文件有格式和大小限制,具体如下:
* 图片image: 1M支持JPG格式
* 语音voice2M播放长度不超过60秒支持AMR/MP3格式
* 视频video10MB支持MP4格式
* 缩略图thumb64KB支持JPG格式
* 媒体文件在微信服务器的保存时间为3天之后media_id将失效。
*
* @param string $filename 要上传的文件的绝对路径
* @param string $type 媒体文件类型可以是图片image、语音voice、视频video和缩略图thumb
* @return array 上传成功后返回包含media_id和其他信息的数组
*/
public static function upload($filename, $type) { public static function upload($filename, $type) {
// 获取微信公众号的ACCESS_TOKEN
$accessToken = AccessToken::getAccessToken(); $accessToken = AccessToken::getAccessToken();
// 构造多媒体文件上传的URL
$queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $accessToken . '&type=' . $type; $queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $accessToken . '&type=' . $type;
// 准备上传的数据,使用'@'符号指定文件路径
$data = array('media' => '@' . $filename); $data = array('media' => '@' . $filename);
// 发起POST请求上传文件
return Curl::callWebServer($queryUrl, $data, 'POST', 1, 0); return Curl::callWebServer($queryUrl, $data, 'POST', 1, 0);
} }
/**
* 从微信服务器下载多媒体文件
* 此方法根据提供的media_id从微信服务器下载对应的多媒体文件。
*
* @param string $mediaId 微信服务器上的多媒体文件ID
* @return mixed 下载的文件内容或下载失败的错误信息
*
* 下载多媒体文件时的HTTP头信息示例
* HTTP/1.1 200 OK
* Connection: close
* Content-Type: image/jpeg
* Content-disposition: attachment; filename="MEDIA_ID.jpg"
* Date: Sun, 06 Jan 2013 10:20:18 GMT
* Cache-Control: no-cache, must-revalidate
* Content-Length: 339721
*
* 使用curl命令下载多媒体文件的示例
* curl -G "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID"
*/
public static function download($mediaId) { public static function download($mediaId) {
// 获取微信公众号的ACCESS_TOKEN
$accessToken = AccessToken::getAccessToken(); $accessToken = AccessToken::getAccessToken();
// 构造多媒体文件下载的URL
$queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $accessToken . '&media_id=' . $mediaId; $queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $accessToken . '&media_id=' . $mediaId;
// 发起GET请求下载文件
return Curl::callWebServer($queryUrl, '', 'GET', 0); return Curl::callWebServer($queryUrl, '', 'GET', 0);
} }
} }

@ -0,0 +1,36 @@
<?php
namespace LaneWeChat\Core;
class TemplateMessage{
public static function setIndustry($industryId1, $industryId2){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['industry_id1'] = "$industryId1";
$template['industry_id2'] = "$industryId2";
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
public static function getTemplateId($templateIdShort){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['template_id_short'] = "$templateIdShort";
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
public static function sendTemplateMessage($data, $touser, $templateId, $url, $topcolor='#FF0000'){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['touser'] = $touser;
$template['template_id'] = $templateId;
$template['url'] = $url;
$template['topcolor'] = $topcolor;
$template['data'] = $data;
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
}

@ -0,0 +1,66 @@
<?php
namespace LaneWeChat\Core;
class UserManage{
public static function createGroup($groupName){
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/groups/create?access_token=' . $accessToken;
$data = '{"group":{"name":"' . $groupName . '"}}';
return Curl::callWebServer($queryUrl, $data, 'POST');
}
public static function getGroupList(){
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/groups/get?access_token=' . $accessToken;
$data = '';
return Curl::callWebServer($queryUrl, $data, 'GET');
}
public static function getGroupByOpenId($openId){
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=' . $accessToken;
$data = '{"openid":"' . $openId . '"}';
return Curl::callWebServer($queryUrl, $data, 'POST');
}
public static function editGroupName($groupId, $groupName){
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/groups/update?access_token=' . $accessToken;
$data = '{"group":{"id":' . $groupId . ',"name":"' . $groupName . '"}}';
return Curl::callWebServer($queryUrl, $data, 'POST');
}
public static function editUserGroup($openid, $to_groupid){
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=' . $accessToken;
$data = '{"openid":"' . $openid . '","to_groupid":' . $to_groupid . '}';
return Curl::callWebServer($queryUrl, $data, 'POST');
}
public static function getUserInfo($openId){
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $accessToken . '&openid=' . $openId;
return Curl::callWebServer($queryUrl, '', 'GET');
}
public static function getFansList($next_openid=''){
$accessToken = AccessToken::getAccessToken();
if(empty($next_openid)){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' . $accessToken;
}else{
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' . $accessToken . '&next_openid=' . $next_openid;
}
return Curl::callWebServer($queryUrl, '', 'GET');
}
public static function setRemark($openId, $remark){
$accessToken = AccessToken::getAccessToken();
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=' . $accessToken;
$data = json_encode(array('openid'=>$openId, 'remark'=>$remark));
return Curl::callWebServer($queryUrl, $data, 'POST');
}
public static function getNetworkState(){
echo "WeixinJSBridge.invoke('getNetworkType',{},function(e){WeixinJSBridge.log(e.err_msg);});";
}
}

@ -1,78 +0,0 @@
<?php
namespace LaneWeChat\Core;
/**
* 推广支持
* User: lane
* Date: 14-10-31
* Time: 下午4:15
* E-mail: lixuan868686@163.com
* WebSite: http://www.lanecn.com
*/
class Popularize{
/**
* 生成带参数的二维码 - 第一步 创建二维码ticket
*
* 获取带参数的二维码的过程包括两步首先创建二维码ticket然后凭借ticket到指定URL换取二维码。
*
* 目前有2种类型的二维码分别是临时二维码和永久二维码
* 前者有过期时间最大为1800秒但能够生成较多数量后者无过期时间数量较少目前参数只支持1--100000
* 两种二维码分别适用于帐号绑定、用户来源统计等场景。
*
* @param $type Int 临时二维码类型为1永久二维码类型为2
* @param $expireSeconds Int 过期时间只在类型为临时二维码时有效。最大为1800单位秒
* @param $sceneId Int 场景值ID临时二维码时为32位非0整型永久二维码时最大值为100000目前参数只支持1--100000
* @return Array(
* //获取的二维码ticket凭借此ticket可以在有效时间内换取二维码。
* "ticket"=>"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm3sUw==",
* //二维码的有效时间以秒为单位。最大不超过1800。
* "expire_seconds"=>60,
* //二维码图片解析后的地址,开发者可根据该地址自行生成需要的二维码图片
* "url"=>"http://weixin.qq.com/q/kZgfwMTm72WWPkovabbI"
* )
*/
public static function createTicket($type, $expireSeconds, $sceneId){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
if($type == 1){
$template['expire_seconds'] = $expireSeconds;
$template['action_name'] = 'QR_SCENE';
}else{
$template['action_name'] = 'QR_LIMIT_SCENE';
}
$template['action_info']['scene']['scene_id'] = $sceneId;
$template = json_encode($template);
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
/**
* 生成带参数的二维码 - 第二步 通过ticket换取二维码
* @param $ticket Popularize::createTicket()获得的
* @param $filename String 文件路径如果不为空则会创建一个图片文件二维码文件为jpg格式保存到指定的路径
* @return 直接echo本函数的返回值并在调用页面添加header('Content-type: image/jpg');,将会展示出一个二维码的图片。
*/
public static function getQrcode($ticket, $filename=''){
$queryUrl = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
$queryAction = 'GET';
$result = Curl::callWebServer($queryUrl, '', $queryAction, 0);
if(!empty($filename)){
file_put_contents($filename, $result);
}
return $result;
}
/**
* 将一条长链接转成短链接。
* 主要使用场景:开发者用于生成二维码的原链接(商品、支付二维码等)太长导致扫码速度和成功率下降,将原长链接通过此接口转成短链接再生成二维码将大大提升扫码速度和成功率。
* @param $longUrl String 需要转换的长链接支持http://、https://、weixin://wxpay 格式的url
* @return array('errcode'=>0, 'errmsg'=>'错误信息', 'short_url'=>'http://t.cn/asdasd')错误码为0表示正常
*/
public static function long2short($longUrl){
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token='.AccessToken::getAccessToken();
$queryAction = 'POST';
$template = array();
$template['long_url'] = $longUrl;
$template['action'] = 'long2short';
return Curl::callWebServer($queryUrl, '', $queryAction);
}
}
Loading…
Cancel
Save