/** * 上传永久图文素材(认证后的订阅号可用) * 新增的永久素材也可以在公众平台官网素材管理模块中看到 * @param array $data 消息结构{"articles":[{...}]} * @return boolean|array */ public function uploadForeverArticles($access_token,$data){ error_log('post data is=======> '.self::json_encode($data)); $url = self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$access_token; $result = HTTP::request($url, 'POST', self::json_encode($data)); error_log('weixin return result is =====>'.$result); if ($result) { $json = json_decode($result,true); if (!$json || !empty($json['errcode'])) { $this->errCode = $json['errcode']; $this->errMsg = $json['errmsg']; return false; } return $json; } return false; }
upload_meterial方法
说明:该方法为调用微信上传永久素材接口方法
/** * 请注意该方法必须保证php的版本在5.6以下,否则会爆40015错误 */ function upload_meterial($file_info,$access_token){ $url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image"; $ch1 = curl_init (); $timeout = 5; $real_path="{$file_info['filename']}"; //$real_path=str_replace("https://www.jb51.net/", "\\", $real_path); $data= array("media"=>"@{$real_path}",'form-data'=>$file_info); curl_setopt ( $ch1, CURLOPT_URL, $url ); curl_setopt ( $ch1, CURLOPT_POST, 1 ); curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout ); curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false ); curl_setopt ( $ch1, CURLOPT_POSTFIELDS, $data ); $result = curl_exec ( $ch1 ); echo '<br/>'; echo 'reulst is ==========>'.$result; curl_close ( $ch1 ); if(curl_errno()==0){ $result=json_decode($result,true); //var_dump($result); return $result['media_id']; }else { return false; } }
http_post方法
说明:该方法为调http post请求方法
/** * POST 请求 * @param string $url * @param array $param * @param boolean $post_file 是否文件上传 * @return string content */ private function http_post($url,$param,$post_file=false){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } if (is_string($param) || $post_file) { $strPOST = $param; } else { $aPOST = array(); foreach($param as $key=>$val){ $aPOST[] = $key."=".urlencode($val); } $strPOST = join("&", $aPOST); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($oCurl, CURLOPT_POST,true); curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ return false; } }
遇到的问题
在开发的过程中,在调用微信上传媒体文件时候始终得到的返回数据为
{"errcode":41005,"errmsg":"media data missing hint: [3fSt_0048e297]"}
原因:php版本的问题,我本机的版本5.6,而带有@识别的php方法必须是5.5以下才能识别,5.5以上的版本将这个特性去除了。
解决方法:更换php的版本到5.5或者5.5以下,不更换php的版本的方法暂时没有找到