MediaController.class.php主要代码:
* 上传永久素材*/public function addMedia($accessToken,$type,$filename){$wechatObj = new \Common\Library\Wechat();$url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=$type";$filePath=$_SERVER['DOCUMENT_ROOT'].'/wechat1011/'.$filename;dump($filePath);//dump($url);clearstatcache();$formData=array('filename'=>$_SERVER['DOCUMENT_ROOT'].$filename,'filelengh'=>filesize($filePath),'content-type'=>'image/jpeg'); dump($formData);$result = $wechatObj->addMedia($url,$filePath,$formData);return $result;}/***获取图文消息素材*/ public function getTuwen($accessToken,$media){ $wechatObj = new \Common\Library\Wechat(); $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=$accessToken"; $data = "{'mediaId':$media}"; //dump($mediaId); $result = $wechatObj->getTuwen($url,$data);return $result; }/*** 获取素材列表*/public function getMediaList($accessToken,$type,$offset,$count){$url="https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=$accessToken";$data = '{"type":"'.$type.'","offset":"'.$offset.'","count":"'.$count.'"}';//dump($data);$result=\Common\Library\Wechat::getMediaList($url,$data);return $result;}/*** 获取素材*/public function getMedia($media){//直接从浏览器访问即可}/*** 上传图文消息*/public function addNews($accessToken){$wechatObj = new \Common\Library\Wechat();$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$accessToken";/*$imageObj = M('images');$condition=array();$condition['id'] = array(array('gt',1),array('lt',5));$results = $imageObj->where($condition);*/ $data = '{ "articles": [{ "title": "百度", "thumb_media_id":"IkmaAbKUeMBbn3mp5S37FaZ-wRQI5yabMUO3zZxLNy8", "author": "刘远", "digest": "链接到百度网站", "show_cover_pic": 1, "content": "<h1>腾讯</h1>", "content_source_url": "https://www.baidu.com" }, ] "articles": [{ "title": "新浪", "thumb_media_id": "IkmaAbKUeMBbn3mp5S37FcEsbx2NE_dntl7Ca0OCc4U", "author": "刘远", "digest": "链接到百度网站", "show_cover_pic": 1, "content": "<h1>爱奇艺</h1>", "content_source_url": "https://www.baidu.com" }, ] "articles": [{ "title": "搜狐", "thumb_media_id":"IkmaAbKUeMBbn3mp5S37FWBNpDwdu0xUoavaHUO6Vt4", "author": "刘远", "digest": "链接到百度网站", "show_cover_pic": 1, "content": "<h1>乐视</h1>", "content_source_url": "https://www.baidu.com" }, ]}';$news = $wechatObj->addNews($url,$data);return $news;
WeChat.class.php主要代码:
public function addsuoluetu($url,$filePath,$formData){ $curl = curl_init ($url); $timeout = 5; $data= array("media"=>"@{$filePath}",'form-data'=>$formData); curl_setopt ( $curl, CURLOPT_POST, 1 ); curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $curl, CURLOPT_CONNECTTIMEOUT, $timeout ); curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false ); curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); $result = curl_exec ($curl); curl_close ( $curl ); if(curl_errno()==0){ $result=json_decode($result,true); return $result; }else { return curl_error($curl); }}public function addMedia($url,$filePath,$formData){ $curl = curl_init ($url); $timeout = 5; $data= array("media"=>"@{$filePath}",'form-data'=>$formData); curl_setopt ( $curl, CURLOPT_POST, 1 ); curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $curl, CURLOPT_CONNECTTIMEOUT, $timeout ); curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false ); curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); $result = curl_exec ($curl); curl_close ( $curl ); if(curl_errno()==0){ $result=json_decode($result,true); return $result; }else { return false; }}public function getTuwen($url,$data){ $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, 0);//过滤头部 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt($curl,CURLOPT_POST,true); // post传输数据 curl_setopt($curl,CURLOPT_POSTFIELDS,$data);// post传输数据 $responseText = curl_exec($curl); dump( $responseText); $res = json_decode($responseText, true); curl_close($curl); return $res; }public function getMediaList($url,$data){ $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, 0);//过滤头部 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt($curl,CURLOPT_POST,true); // post传输数据 curl_setopt($curl,CURLOPT_POSTFIELDS,$data);// post传输数据 $responseText = curl_exec($curl); //dump($responseText); $res = json_decode($responseText, true); curl_close($curl); return $res; }public function addNews($url,$data){ $curl = curl_init ($url); curl_setopt ( $curl, CURLOPT_POST, 1 ); curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $curl, CURLOPT_CONNECTTIMEOUT, $timeout ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false ); curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); $result = curl_exec ($curl); curl_close ($curl); if(curl_errno()==0){ return $result; }else { dump(curl_errno($curl))