“errcode“:41005,“errmsg“:“media data missing hint

5.6之前这样调微信上传素材

$url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={
    
    $access_token}&type=image";
//$filepath图片绝对路径D:\phpstudy_pro\WWW\yh_member\yh_member\public\uploads\activity\20210303\c9aa05bc27cd5d9a17f5d2cd32ca64bb.jpg
$real_path="{
    
    $_SERVER['DOCUMENT_ROOT']}{
    
    $file_info['filename']}";
$filepath =  '@'.$real_path;
$data= [
    "media"=>$filepath,
];
$result = request_post($url,$data);
$result = json_decode($result,true);
if(!isset($result['media_id'])){
    
    
	//日志
    addLog('addMedia','error',$result);
}
return $result;

php5.6之后这样调微信上传素材

$url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={
    
    $access_token}&type=image";
//$filepath图片绝对路径D:\phpstudy_pro\WWW\yh_member\yh_member\public\uploads\activity\20210303\c9aa05bc27cd5d9a17f5d2cd32ca64bb.jpg
$real_path="{
    
    $_SERVER['DOCUMENT_ROOT']}{
    
    $file_info['filename']}";
$filepath = new \CURLFile(realpath($real_path));
$data= [
    "media"=>$filepath,
];
$result = request_post($url,$data);
$result = json_decode($result,true);
if(!isset($result['media_id'])){
    
    
	//日志
    addLog('addMedia','error',$result);
}
return $result;

request_post 请求

function request_post($url = '', $param = '',$headers = false){
    
    
    if (empty($url) || empty($param)) {
    
    
        return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init(); //初始化curl
    curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
    curl_setopt($ch, CURLOPT_POST, true); //post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_TIMEOUT, 8);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $data = curl_exec($ch); //运行curl
    curl_close($ch);
    return $data;
}

猜你喜欢

转载自blog.csdn.net/cheers_bin/article/details/114318142