1. 程式人生 > >PHP7微信上傳永久圖片報錯errcode":41005,"errmsg":"media data missing

PHP7微信上傳永久圖片報錯errcode":41005,"errmsg":"media data missing

function https_request($url ='' , $path = '' ){
  $curl = curl_init();
  if (class_exists('\CURLFile')){
    curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    $data = array('media' => new \CURLFile($path));//
  }
  else
  {
    curl_setopt($curl,CURLOPT_SAFE_UPLOAD,false);
    $data = array('media'=>'@'.$path);
  }
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, 1 );
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
  $result = curl_exec($curl);
  if($result === false) {
     echo "error:".curl_errno($curl);
     exit;
  }
  $res=json_decode($result,true);
  return $res;
}

相容了PHP5.6以上和以下的所有PHP版本,

php>5.6CURLOPT_SAFE_UPLOAD =  true  and 要使用 new \CURLFILE($path) $path是檔案路徑( form-data 不用加)

php<5.6 CURLOPT_SAFE_UPLOAD = false

 

php === printf("%s",$a,$b) 打印出來 和print是一樣的功能

php ===sprintf("%s",$a,$b) 返回一個字串  $s是佔位符