加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php微信高级接口群发 多客服

发布时间:2020-05-23 12:52:00 所属栏目:PHP 来源:互联网
导读:这篇文章主要为大家详细介绍了php微信高级接口群发、多客服的相关资料,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了php微信高级接口群发、多客服源码,供大家参考,具体内容如下

'tokenaccesskey',//填写你设定的key * 'appid'=>'wxdk1234567890',//填写高级调用功能的app id * 'appsecret'=>'xxxxxxxxxxxxxxxxxxx',//填写高级调用功能的密钥 * ); */ class WeiXinTool {

private $appid;
private $appsecret;
private $access_token;
private $mediaType = array('image' => array("jpg"),'voice' => array('amr','MP3'),'video' => array('mp4'),'thumb' => array("jpg"));
private $mediaMaxSize = array('image' => 131072,'voice' => 262144,'video' => 1048576,'thumb' => 65536);
private $tem_file_path = "";

// 授权地址
const AUTH_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
// 素材上传
const UPLOAD_MEDIA_URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";
const GET_MEDIA_URL = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s";
const UPLOAD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=%s";

public function __construct($options) {
$this->appid = isset($options['appid']) ? $options['appid'] : '';
$this->appsecret = isset($options['appsecret']) ? $options['appsecret'] : '';
//需修改
//上传图片临时文件目录自定义
$this->tem_file_path = YYUC_FRAME_PATH . YYUC_PUB . '/' . Session::get('upath');
}

/**

  • 需修改
  • 具体业务需求,图文信息上传至素材库
  • 微站文章上传至微信素材
  • @param type $wid
  • @param type $aid
  • @return int
    */
    public function uploadArticlesToWeiXinServer($wid,$aid) {
//具体图文组装过程,需修改
$m = new Model('website_article');
$m_pubs = new Model('pubs');
$m_pubs->find(array("id" => $wid));
$m->find(array("wid" => $wid,'id' => $aid));
$res = array();
if ($m->has_id() && $m_pubs->has_id()) {
  $res[] = $m->get_model_array();

// var_dump($res);
$m->votetouser = json_decode($m->votetouser,TRUE);
$articles = $m->votetouser[0];
$m_article = new Model('website_article');
$ress = $m_article->where(array('wid' => $wid,'id' => $articles))->list_all_array();
$res = array_merge($res,$ress);
} else {
$errarr = array();
$errarr['errcode'] = 44003;
$errarr['errmsg'] = self::$errorno[$errarr['errcode']];
return $errarr;
}
$items = array();
foreach ($res as $k => $v) {
$mediaid = $this->uploadMedia($v['picurl']);
if ($mediaid['media_id']) {
$thumb_media_id = $mediaid['media_id'];
} else {
return $mediaid;
}
$item = array(
"thumb_media_id" => $thumb_media_id,"author" => $m_pubs->pubun,"title" => $v['title'],"content_source_url" => WeiXinTool::complateUrl(WeiSite::parseArticleLinkData($v)),"content" => $v['reply_content'],//内容 富文本
"digest" => $v['description']//描述
);
$items[] = $item;
}
//以上具体图文组装过程,需修改

$postData['articles'] = $items;
$error = $this->uploadNews($postData);
return $error;

}

/**

  • 需修改
  • 微站文章关键字匹配数据解析
    */
    public static function getArticleData($keyword) {
    global $wid;
    $m = new Model('website_article');
    $m->find(array('wid' => $wid,'keyword@~' => " " . $keyword . " "));
    $res = array();
    if ($m->has_id()) {
    $res[] = array("tit" => $m->title,"pic" => $m->picurl,"dec" => $m->description,"url" => WeiSite::parseArticleLinkData($m->get_model_array()));
    $m->votetouser = json_decode($m->votetouser,TRUE);
    $articles = $m->votetouser[0];
    if (!empty($articles)) {
    foreach ($articles as $v) {
    $m_article = new Model('website_article');
    $m_article->find(array('wid' => $wid,'id' => $v));
    $res[] = array("tit" => $m_article->title,"pic" => $m_article->picurl,"dec" => $m_article->description,"url" => WeiSite::parseArticleLinkData($m_article->get_model_array()));
    }
    }
    return $res;
    }
    }

/**

  • 获取accesstoken
  • @param type $flag 强制刷新accesstoken 开关
  • @return type
    */
    public function getAccessToken($flag = FALSE) {
    $url = sprintf(self::AUTH_URL,$this->appid,$this->appsecret);
    $result = Cache::get(md5($url));
    if ($flag || empty($result)) {
    $result = $this->http_get($url);
    $result = json_decode($result,TRUE);
    if ($result['errcode']) {
    return $result['errcode'];
    }
    Cache::set(md5($url),$result,1);
    }
    $this->access_token = $result['access_token'];
    return true;
    }

/**

  • 上传媒体
  • @param type $file 媒体文件 $url或者物理路径地址
  • @param type $type 类型
  • @return int
  • return
    array (size=3)
    'type' => string 'image' (length=5)
    'media_id' => string '-0Lr3rX9mDYBB7i5bDydvwFHHm3zW2Uxt0OoDFBPmGRfYiwckiALqHHDlP9jCm' (length=64)
    'created_at' => int 1400477181
    */
    public function uploadMedia($file,$type = "image") {
    $file = self::complateUrl($file);
    $urlarr = parse_url($file);
    $filetype = explode(".",$urlarr['path']);
    $filetype = strtolower($filetype[count($filetype) - 1]);
    $resizeSize = 100; //图片处理后另存质量
    if (!key_exists($type,$this->mediaType) || !in_array($filetype,$this->mediaType[$type])) {
    // return 40005; //格式错误
    $errarr = array();
    $errarr['errcode'] = 40005;
    $errarr['errmsg'] = self::$errorno[$errarr['errcode']];
    return $errarr;
    }
$temp_file = $this->tem_file_path . 'uploadMedia.' . $filetype;
$temp_file_resize = $this->tem_file_path . 'uploadMediaResize.' . $filetype;
file_put_contents($temp_file,self::http_get($file));
$handle = fopen($temp_file,"r");
$fstat = fstat($handle);

if ($fstat['size'] > $this->mediaMaxSize[$type]) {
  $resizeSize = intval($this->mediaMaxSize[$type] / $fstat['size'] * 100);
  ImageTool::resizeImage($temp_file_resize,$temp_file,400,$resizeSize); //图片太大再处理压缩
  $temp_file = $temp_file_resize;

// return 40006; //大小错误
}
$filePath = realpath($temp_file);
$uploadUrl = sprintf(self::UPLOAD_MEDIA_URL,$this->access_token,$type);
$postData = array("r" => time(),'media' => "@{$filePath}");
$result = self::http_post($uploadUrl,$postData);
$result = json_decode($result,TRUE);
return $result;
}

/**

  • 群发图文信息
  • @param type $touser 粉丝数组/粉丝组id
  • @param type $media_id
  • @return type
    */
    public function sendArticles($touser,$media_id) {
    $errarr = array();
    $postData = array();
    $postData['mpnews'] = array("media_id" => $media_id);
    $postData['msgtype'] = "mpnews";
    if (is_array($touser)) {
    //用户列表群发
    $postData['touser'] = $touser;
    $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=%s';
    } else {
    $group = intval($touser);
    $groups = $this->getGroups(true);
    if (key_exists($group,$groups['list'])) {
    $postData['filter'] = array("group_id" => $group);
    } else {
    $errarr['errcode'] = 40050; //无效分组id
    $errarr['errmsg'] = self::$errorno[$errarr['errcode']];
    return $errarr;
    }
    $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=%s';
    }
    $url = sprintf($url,$this->access_token);
    $result = self::http_post($url,self::json_encode($postData));
    $result = json_decode($result,TRUE);
    return $result;
    }

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读