今天解決一個調用微信永久素材上傳視頻的問題,在網上查了好久,上傳視頻的接口寫的都不同並且處處複製,感受很亂,因而在作完case以後作了一個整理,但願可以幫助到大家.php
涉及到如何調用微信永久素材上傳視頻以及上傳後如何經過mediaid和獲取永久素材接口獲得該視頻對應的url的,而後保存至本地,而後經過調用微信的url直接讀取視頻,拿默認回覆中的視頻來舉例。(YII1框架)前端
首先:數據庫
前端頁面中一個上傳按鈕,在上傳了以後經過js回調將視頻文件先上傳到服務器上對應的文件夾中,這個沒什麼好說的。json
jQuery('#video_upload').dmUploader({ url: '<?php echo Yii::app()->createAbsoluteUrl('Manager/DefaultMessages/videoUpload'); ?>', dataType : 'json', fileName : 'upload', allowedTypes : 'video/*', maxFileSize : 10485760, onUploadSuccess: function(id, data){ if(data.result == 'success'){ videoUpload.find('video').attr('src',data.url); }else{ alert(data.result); } }, onFileSizeError: function(file){ alert('<?php echo Yii::t("manager", "Unsupported file size!"); ?>') }, onFileTypeError: function(file){ alert('<?php echo Yii::t("manager", "Unsupported file type!"); ?>') } });
這個是Controller中上傳video的方法api
public function actionVideoUpload(){ $fileName = $_FILES['upload']['name']; $extensions = array('.mp4'); $extensionName = ''; foreach ($extensions as $extension) { if(stripos($fileName, $extension) !== false){ $extensionName = $extension; break; } } if($extensionName){ $filePath = $_FILES['upload']['tmp_name']; $des = dirname(__FILE__).'/../../../../upload/videos/defaultMessage/'; //目錄及權限 if(!is_dir($des)){ mkdir($des, 0777, true); } $timeStamp = time(); //成功則返回服務器所在視頻的地址 if(move_uploaded_file($filePath, $des.$timeStamp.$extensionName)){ echo json_encode(array('result'=>'success','url'=>'http://'.$_SERVER['HTTP_HOST'].'/upload/videos/defaultMessage/'.$timeStamp.$extensionName)); }else{ echo json_encode(array('result'=>'failed')); } }else{ echo json_encode(array('result'=>Yii::t('manager', 'Unsupported file type!'))); } }
而後是save之後的處理。服務器
public function actionModify() { //這裏是我項目裏面獲取defaultmessage的內容的,不用看 $mid = Helper::getManagerId(); $model = DefaultMessages::model()->find('mid = :mid', array(':mid' => $mid)); if (!$model) { $model = new DefaultMessages(); $model->mid = $mid; } if(isset($_POST['DefaultMessages'])) { //上傳或修改若是改變了視頻,則同步上傳到微信並保存微信的mediaId,這裏根據前端本身作相應的判斷,注意若是視頻沒有改變的話不要上傳視頻,不然素材空間很快就會滿掉。 if($_POST['DefaultMessages']['replyType']==5&&($model->mediaId !=$_POST['DefaultMessages']['mediaId'])){ $filePath = $_POST['DefaultMessages']['mediaId']; //微信須要的title $video_title = substr($filePath,-14); //視頻所在服務器上的路徑 $filePath= dirname(__FILE__).'/../../../../upload/videos/defaultMessage/'.$mid.$video_title; //微信須要的introduction描述 $introduction = 'default_message_video'; //上傳並獲取media_id $media_id = Media::addMaterial($mid, 'video', $filePath,true, $video_title, $introduction); //經過mediaid調用獲取永久素材接口獲取微信視頻存儲的url $wechat_url = Media::getMaterial($mid,$media_id); //保存mediaid爲微信視頻存放的url $_POST['DefaultMessages']['mediaId'] = $wechat_url; $model->attributes=$_POST['DefaultMessages']; } if ($model->save()) { $this->redirect(array('modify')); }else{ var_dump($model->getErrors());die(); } } $this->render('modify', array( 'model' => $model, )); }
上面調用的addmaterial函數微信
public static function addMaterial($mid, $type, $filePath, $is_video=false, $video_title='', $introduction='') { $json = WechatMedia::addMaterial(AccessToken::getAccessToken($mid), $type, $filePath, $is_video, $video_title, $introduction);//下面有函數,token的拼接就不貼出來了 $result = json_decode($json, TRUE); //成功則返回mediaid用於後面根據mediaid獲取視頻url if (!isset($result['media_id'])) { return false; } return $result['media_id']; }
上面調用的material函數app
public static function addMaterial($access_token, $type, $filePath, $is_video, $video_title, $introduction) { //$api_url 相似這個https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=....&type=video $api_url = self::getApiUrl('add_material'); if ($api_url) { $params = array(); if ($is_video) { $params['description'] = urldecode(json_encode(array( 'title' => $video_title, 'introduction' => $introduction ))); } //版本不一樣好像media有所不一樣 if (version_compare(PHP_VERSION, '5.5.0') < 0 ) { $params['media'] = '@'.$filePath; } else { $params['media'] = new CURLFile($filePath); } return HttpCurl::post($api_url, $params); } return false; }
post方法框架
public static function post($url, $keysArr, $header=array(), $flag = 0) { $ch = curl_init(); if (!$flag) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); } if(count($header) > 0) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr); curl_setopt($ch, CURLOPT_URL, $url); $ret = curl_exec($ch); $err = curl_error($ch); //yii記錄日誌用的不用看 if($err){ Yii::log("curl error ::".$err, 'error', 'Curl Error'); Yii::log("curl error url ::".$url, 'error', 'Curl Error'); } curl_close($ch); return $ret; }
經過mediaid調用微信獲取永久素材接口獲取微信視頻存儲的地址yii
public static function getMaterial($mid, $media_id) { $json = WechatMedia::getMaterial(AccessToken::getAccessToken($mid), $media_id); $result = json_decode($json, TRUE); //微信那裏的地址是down_url,存在則返回 if(isset($result['down_url'])){ return $result['down_url']; } return false; }
獲取永久素材的地址
public static function getMaterial($access_token, $media_id) { //Apiurl相似於https://api.weixin.qq.com/cgi-bin/material/get_material?access_token..... $apiUrl = self::getApiUrl('get_material'); if ($apiUrl) { $params = array( 'media_id' => $media_id ); //這裏注意要json一下,至於post方法上面有就不貼了 return HttpCurl::post($apiUrl, json_encode($params)); } else { return false; } }
這樣存在數據庫中的mediaid就是微信的視頻的地址了,應該算是比較完整的一段流程了0.0