原因:由于PHP5.6以前与之后的版本curl_setopt有差异。PHP5.6以后不再支持”@文件路径”的方式。
$picPath= "public\public\upload\xxx.png" ;//绝对路径图片地址
完整代码
//上传临时素材
$url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=".$type;
//2、同步到微信服务器
if(class_exists('\CURLFile')){ //7.0
$data = ['media' => new \CURLFile($picPath)];
}else{ //5.6以及5.6以下
$data = array('media' => '@'.$picPath);
} $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
curl_close($ch);
$wxrs_id_card=json_decode($res,true);
$id_card =$wxrs_id_card['media_id']; //得到media_id
还有一个是FuelPHP 会出现的问题,代码加一行
if(class_exists('\CURLFile')){
$data = ['media' => new \CURLFile($media)];
}else{
$data = ['media' => "@{$media}"];
} $data = ['form-data' => $data];