前瞻
之前寫了個通用的微信管理,領導說實用性太差凿菩,要加上群發(fā)管理机杜,哎真是站著說話不腰疼,難為人衅谷,沒辦法椒拗,自己是打工的,干吧获黔;不過個人覺得還是直接上微信后臺方便的多蚀苛。先看下我的目錄
框架:tp3.2
思路
我這里是模擬的微信后臺添加的方式,首先有個群發(fā)主題(一個欄目)玷氏,然后在群發(fā)內容里在對應主題添加內容堵未,一個主題可以對應著多個內容(多圖文)≌荡ィ可以看下我的頁面:
我的群發(fā)內容頁面:
數(shù)據(jù)庫字段:
-
群發(fā)主題
-
群發(fā)內容
實現(xiàn)群發(fā)功能需要以下幾步:
- 上傳群發(fā)內容的標題圖到微信服務器渗蟹,拿到media_id(即thumb_media_id),這是上傳主題素材用的赞辩;調用接口地址http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html (我也不知道微信公眾平臺有幾個開發(fā)文檔雌芽,我看到了3個不同版本,這個是我點擊客戶接口進去的)
- 上傳群發(fā)內容編輯器所編輯內容所有圖片辨嗽,拿到對應服務器的url并替換碍脏;這個也是必須的卦方,否則王悍,微信不會顯示圖片沦零;調用地址:http://mp.weixin.qq.com/wiki/14/0c53fac3bdec3906aaa36987b91d64ea.html
- 上傳主題素材来破,拿到media_id篮灼,用于群發(fā),這個和上面2的地址一樣
- 利用素材的media_id徘禁,上傳或者預覽诅诱,地址也是2
代碼:(因為我的是測試號,沒權限好像是送朱,但是我的預覽接口成功了娘荡,預覽和素材只是最后一步不一樣)
namespace Admin\Controller;
class massController extends AuthController{
protected $MassObj;
protected $MassReturnObj;
protected $conf;
public function __construct(){
parent::__construct();
$this->MassObj = D('Mass');//主題表
$this->MassReturnObj = D('MassReturn');//主題內容表
}
/**
* [delmass 發(fā)送群發(fā)主題]
* @return [JSON] [返回成功與失敗]
*/
public function sendmass(){
//這里我是異步post請求的,可以是get驶沼,看個人
if( !empty( $_POST ) ){
$id = I('post.id');
$data = $this->MassReturnObj->order('mass_order')->where(array('mass_id'=>$id))->select();//拿到主題內容數(shù)據(jù)
$access_token = $this->getWxAccessToken();//下面有函數(shù)
//循環(huán)遍歷每個圖文
foreach( $data as $k=>$v){
//調用基礎支持的上傳多媒體文件炮沐,上傳標題圖
$url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image";
$arr = array('media'=>'@'.$_SERVER['DOCUMENT_ROOT'].$v['pic_url'],'form-data'=>$v['pic_url']);
$res = $this->http_curl($url,'post','',$arr);
if( !$res ){
$this->ajaxReturn( array( 'code'=>1,'msg'=>'id為'.$v['id'].'的數(shù)據(jù)標題圖圖片上傳失敗回怜!' ) );
}
$v['media_id'] = $res['media_id'];//標題圖的thumb_media_id
$v['wxpic'] = $res['url'];
$content = $v['content'];
preg_match_all('/<img.*?src="(.*?)".*?>/is',$content,$arr);//提取編輯內容的所有img的src
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$access_token}&type=image";
//將內容里面的所有img圖片上傳到微信服務器大年,并替換新的圖片地址
foreach( $arr[1] as $k1=>$vo ){
$arr = array('media'=>'@'.$_SERVER['DOCUMENT_ROOT'].$vo,'form-data'=>$vo);
//調用圖片上傳到微信服務器接口
$res = $this->http_curl($url,'post','',$arr);
if( !$res['url'] ){
$this->ajaxReturn( array( 'code'=>1,'msg'=>'id為'.$v['id'].'的消息內容頁面的第'.($k1+1).'張圖片上傳失敗!' ) );
}
$content = str_replace($vo,$res['url'],$content);//替換新的圖片地址
}
//拼接主題素材數(shù)組翔试,這塊漢字需要用urlencode轉一下轻要,轉成json再用urldecode轉回,否則消息頁面是亂碼
$postArr['articles'][] = array(
"thumb_media_id"=>$v['media_id'],
"author"=>urlencode($v['author']),
"title"=>urlencode($v['title']),
"content_source_url"=>urlencode($v['url']),
"content"=>urlencode(htmlspecialchars(str_replace("\"","'",$content))),//需要str_replace將上引號換成單引號
"digest"=>urlencode($v['description']),
"show_cover_pic"=>"0"
);
}
//將數(shù)組轉為json
$postArr = htmlspecialchars_decode(urldecode(json_encode($postArr)));
//調用上傳素材接口
$url = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token='.$access_token;
//上傳整個素材post
$res = $this->http_curl($url,'post','json',$postArr);
$media_id = $res['media_id'];//群發(fā)素材的media_id
$url= "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=".$access_token;
$postArr = array(
"filter"=>[
"is_to_all"=>true,
],
"mpnews"=>[
"media_id"=>$media_id
],
"msgtype"=>"mpnews"
);
$postArr = json_encode( $postArr );
$res = $this->http_curl($url,'post','json',$postArr);
if( $res['errcode']==0 && $res['msg_data_id'] ){
$this->MassObj->setField('put_time',time());
$this->ajaxReturn( array( 'code'=>0,'msg'=>'發(fā)送成功垦缅!' ) );
}else{
$this->ajaxReturn( array( 'code'=>1,'msg'=>$res['errcode'].':'.$res['errmsg'] ) );
}
}else{
$this->ajaxReturn( array( 'code'=>1,'msg'=>'無效操作' ) );
}
}
}
預覽接口
public function send(){
//Gl8DWQUSQHXlB7IBKXcyqF79PSS9Mm_rTvwJsIHx_rPs3oMgWR8tB6l5Z4CNStXo
if( !empty( $_POST ) ){
$id = I('post.id');
$data = $this->MassReturnObj->order('mass_order')->where(array('mass_id'=>$id))->select();
$access_token = $this->getWxAccessToken();
foreach( $data as $k=>$v){
//調用基礎支持的上傳多媒體文件
$url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image";
$arr = array('media'=>'@'.$_SERVER['DOCUMENT_ROOT'].$v['pic_url'],'form-data'=>$v['pic_url']);
$res = $this->http_curl($url,'post','',$arr);
if( !$res ){
$this->ajaxReturn( array( 'code'=>1,'msg'=>'id為'.$v['id'].'的數(shù)據(jù)標題圖圖片上傳失敵迥唷!' ) );
}
echo "1111<br />";var_dump($res);echo "<br />";
$v['media_id'] = $res['media_id'];//標題圖的thumb_media_id
$v['wxpic'] = $res['url'];
$content = $v['content'];
preg_match_all('/<img.*?src="(.*?)".*?>/is',$content,$arr);//提取所有img的src
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$access_token}&type=image";
//將內容里面的所有img圖片上傳到微信服務器壁涎,并替換新的圖片地址
foreach( $arr[1] as $k1=>$vo ){
$arr = array('media'=>'@'.$_SERVER['DOCUMENT_ROOT'].$vo,'form-data'=>$vo);
//調用圖片上傳到微信服務器接口
$res = $this->http_curl($url,'post','',$arr);
if( !$res['url'] ){
$this->ajaxReturn( array( 'code'=>1,'msg'=>'id為'.$v['id'].'的消息內容頁面的第'.($k1+1).'張圖片上傳失敺不小!' ) );
}
$content = str_replace($vo,$res['url'],$content);
}
//拼接數(shù)組
$postArr['articles'][] = array(
"thumb_media_id"=>$v['media_id'],
"author"=>urlencode($v['author']),
"title"=>urlencode($v['title']),
"content_source_url"=>urlencode($v['url']),
"content"=>urlencode(htmlspecialchars(str_replace("\"","'",$content))),
"digest"=>urlencode($v['description']),
"show_cover_pic"=>"0"
);
}
//將數(shù)組轉為json
$postArr = htmlspecialchars_decode(urldecode(json_encode($postArr)));
//調用上傳素材接口
$url = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token='.$access_token;
//上傳整個素材post
$res = $this->http_curl($url,'post','json',$postArr);
echo "1111<br />";var_dump($res);echo "<br />";
$media_id = $res['media_id'];//群發(fā)素材的media_id
$url= "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=".$access_token;
$postArr = array(
"touser"=>"oV3xFwbYr3X4sYFepGUgDNgn6cGg",
"mpnews"=>[
"media_id"=>$media_id
],
"msgtype"=>"mpnews"
);
$postArr = json_encode( $postArr );
$res = $this->http_curl($url,'post','json',$postArr);
echo "1111<br />";var_dump($res);
if( $res['errcode']==0 && $res['errmsg'] ){
// $this->MassObj->setField('is_send',1);
$this->ajaxReturn( array( 'code'=>0,'msg'=>'發(fā)送成功粹庞!' ) );
}else{
$this->ajaxReturn( array( 'code'=>1,'msg'=>$res['errcode'].':'.$res['errmsg'] ) );
}
}else{
$this->ajaxReturn( array( 'code'=>1,'msg'=>'無效操作' ) );
}
}
可以將上面的echo去掉咳焚,不去的話可以看返回值,調試用
用到的函數(shù)
/**
*
*獲取全局access_token
* @return $access_token string
*/
function getWxAccessToken(){
//將access_token存在session中
if( $_SESSION['access_token'] && $_SESSION['expire_time']>time() ){
return $_SESSION['access_token'];
}else{
$this->conf = M('Wechat')->find(1);
//如果access_token不存在或者過期庞溜,重新獲取
$appid = $this->conf['appid'];//公眾號的appid
$appsecret = $this->conf['appsecret'];//公眾號的appsecret
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res = $this->http_curl($url);
$access_token = $res['access_token'];
//將獲取到的access_token存到session
$_SESSION['access_token'] = $access_token;
$_SESSION['expire_time'] = time()+7000;//access_token過期時間為2小時革半,所以把session過期時間也修改過來
return $access_token;
}
}
/**
*
*$url 接口url string
*$type 請求類型 string
*$res 返回數(shù)據(jù)類型 string
*$arr post請求參數(shù) array
*
*/
public function http_curl($url,$type='get',$res='json',$arr=''){
//1.初始化curl
$ch = curl_init();
//2.設置curl的參數(shù)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if($type == 'post'){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
}
//3.采集
$output = curl_exec($ch);
//4.關閉
curl_close($ch);
if($res == 'json'){
if( curl_error($ch) ){
return curl_error($ch);//請求失敗返回錯誤信息
}else{
return json_decode($output,true);//請求成功返回json
}
}else{
if( curl_error($ch) ){
return curl_error($ch);//請求失敗返回錯誤信息
}else{
return json_decode($output,true);//請求成功返回json
}
}
}