功能需求
公司新開的公眾號需要將公司平臺現在的所有精品文章都導入,手動導入會有很多的工作量,所以采用自動化同步文章的方式來達到效果
開發說明
微信open api提供了新增永久素材的接口,本次功能是基于這個接口進行數據同步的
使用到的接口
- 獲取永久素材列表接口:material/batchget_material
- 新增永久素材接口:material/add_news
- 新增媒體文件接口:material/add_material
- 圖文類型
- 單圖文(要求有默認的封面,需要提前上傳到微信公眾號后臺)
環境要求
php版本:5.5以下(因為下面代碼中的上傳媒體文件必須要求在此環境,否則會調用微信接口失敗)
開發流程
1、從公司平臺獲取所有的文章列表
2、遍歷文章列表,查看文章是否有圖片附件,若有進行第三步,否則進行第四步
3、檢測所有的附件,取出第一個圖片附件,并調用新增媒體文件接口上傳圖片獲得返回后的media_id
4、調用素材列表接口獲取默認的封面圖片,并從中得到的數據中獲取media_id
5、根據返回獲取到的media_id開始調用上傳圖文接口上傳素材
6、記錄返回信息
接口設計
獲取微信素材列表接口
此接口是用于獲取默認的圖片media_id同步平臺數據接口
此接口是用戶同步我們自己的文章數據到微信功能實現
接口常量
private $app_id = 'wx189ae9fa8816b131'; private $app_secret = '36f5f430c591acbae3505fe877733283'; const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin'; const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?'; const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?'; const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?'; const MEDIA_FOREVER_GET_URL = '/material/get_material?'; const MEDIA_FOREVER_DEL_URL = '/material/del_material?'; const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?'; const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
獲取微信素材列表接口
action接口方法
說明:該方法為此接口的入口方法
調用方式:http://${domain}/weixin/get_articles/
/** * 獲取圖片素材接口 */ public function get_articles_action(){ $token = $this->get_access_token(); $list = $this->getForeverList($token,'image',0,20); echo json_encode($list); } get_access_token方法 private function get_access_token() { $access_token = AWS_APP::cache()->get('access_token'); if(!$access_token){ error_log('get access_token from weixin '); $appId = $this->app_id; $appSecret = $this->app_secret; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret"; $res = json_decode($this -> httpGet($url)); $access_token = $res -> access_token; AWS_APP::cache()->set('access_token',$access_token,time()+3600); }else{ error_log('get access_token from cache '); } error_log('access_token is :'.$access_token); return $access_token; }
調用微信素材接口方法
說明:該方法為調用微信獲取永久素材列表接口方法
/** * 獲取永久素材列表 * @param $token * @param $type 類型有image,vedio和audio * @param $offset 起始位置,0表示從第一個 * @param $count 個數,區間為0~20 */ public function getForeverList($token,$type,$offset,$count){ $data = array( 'type' => $type, 'offset' => $offset, 'count' => $count, ); $result = $this->http_post( self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$token, self::json_encode($data) ); error_log('forever list is :'.$result); if ($result) { $json = json_decode($result,true); if (isset($json['errcode'])) { $this->errCode = $json['errcode']; $this->errMsg = $json['errmsg']; return false; } return $json; } return false; }
同步文章到微信接口
action方法
說明:該方法為此接口的入口方法
調用方式:http://${domain}/weixin/upload_article/
/** * 同步問答的文章到訂閱號上接口 */ public function index_action(){ $article_list = $this->model('article')->get_articles_list(null, 1, 18, 'add_time DESC'); $access_token = $this->get_access_token(); $base_url = 'http://wenda.qiezilife.com/article/'; foreach ($article_list as $key => $article){ if($article['has_attach']){ $attaches = $this->model('publish')->get_attach('article', $article['id'], 'max'); foreach ($attaches as $i => $a){ //過濾獲取第一張圖片 if($a['is_image']){ $attache = $a; break; } } $img = $attache['path']; $size = filesize($img); echo $img.',size is :'.$size; echo '<br/>'; $file_info = array( 'filename' => $img, 'content-type' => 'image/jpg', //文件類型 'filelength' => $size ); $upload_img_result = $this->upload_meterial($file_info,$access_token); $media_id = $upload_img_result; error_log('media_id is ===============>'.$media_id); }else{ $media_id = '1PoTp0SqruwWu_HX0HR_jUp4STX5HSpYkibb1Ca8ZQA'; } $articles =array(); //上傳圖片成功了就開始上傳圖文 $upload_article_data = array( 'title' => $article['title'], 'thumb_media_id' => $media_id, 'author' => '茄子營養師', 'digest' => '茄子生活,你的品質生活指南', 'show_cover_pic' => 1, 'content' => $article['message'], 'content_source_url' => $base_url.$article['id'] ); $articles[] = $upload_article_data; $data = array( 'articles' => $articles ); $result= $this->uploadForeverArticles($access_token,$data); echo self::json_encode($result); error_log('upload_article result is : '.json_encode($result)); error_log('============================upload end============================'); } }
uploadForeverArticles方法
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。