緣由:因爲PHP5.6之前與以後的版本curl_setopt有差別。PHP5.6之後再也不支持」@文件路徑」的方式。json
$picPath= "public\public\upload\xxx.png" ;//絕對路徑圖片地址
完整代碼
//上傳臨時素材
$url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=".$type;
//二、同步到微信服務器
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 會出現的問題,代碼加一行api
if(class_exists('\CURLFile')){
$data = ['media' => new \CURLFile($media)];
}else{
$data = ['media' => "@{$media}"];
}
$data = ['form-data' => $data];