微信公众号开发---上传临时素材到公众号遇到的问题:"errcode":41005,"errmsg":"media data missing

1、上传临时素材到公众号遇到的问题:"errcode":41005,"errmsg":"media data missing

解决办法:因为php版本的原因,上传素材一直保错。 php 的curl的 curl_setopt 函数存在版本差异

php5.5已经把通过@加文件路径上传文件的方式给放入到Deprecated中了。php5.6默认是不支持这种方式了

特殊处理1:curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD, false);

特殊处理2:$post_data ['media']  = '@'.$file;

具体实现代码如下

/* 上传多媒体文件 */
	public function uploadFile($file, $type = 'image', $acctoken = '') {
	//	$post_data ['type'] = $type; // 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
		$post_data ['media']  = '@'.$file;
     	//$post_data ['media']  =$file;
    
		$url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=$acctoken&type=image";
    
        $output=$this->http_post_media($url, $post_data);

        return $output;
}
 private function http_post_media($url,$strPOST)
    {
        $oCurl = curl_init ();
        curl_setopt ( $oCurl, CURLOPT_SAFE_UPLOAD, false);
        if (stripos ( $url, "https://" ) !== FALSE) {
            curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
            curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
        }

        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;
        }

    }


 
   


你可能感兴趣的:(php编程)