php 通過curl上傳圖片(php7)


通過curl上傳圖片

  • PHP < 5.5:
    使用 目前使用的php版本 7.1 測試無法使用 前面加@ 的方法上傳文件 ,查找資料 使用 curl_setopt($ch,CURLOPT_SAFE_UPLOAD,FALSE) 可以解決,但是經測試 這種方式不行,顯示的CURLOPT_SAFE_UPLOAD這個選項在該版本php中已經被廢棄

  • 可以通過檢測 有沒有 curl_file_create 這個函數 也可以檢測 有沒有類\CURLFile class_exists('\CURLFile')

$filename = new \CURLFile(realpath($filepath),$minetype,$basename);
或者
$filename = curl_file_create(realpath($filepath),$minetype,$basename);

 
if (!function_exists('curl_file_create')) {
     function curl_file_create($filename, $mimetype = '', $postname = '') {
         return "@$filename;filename="
             . ($postname ?: basename($filename))
             . ($mimetype ? ";type=$mimetype" : '');
     }
 }
 
$ch = curl_init();
$filename = 'C:/Users/shanghai/Pictures/Camera Roll/23.jpg';
$minetype = 'image/jpeg';
$curl_file = curl_file_create($filename,$minetype);
$postData = [
    'file' => '111',
    'text' => '666',
    'file_name'=>$curl_file ,
];
 
 
curl_setopt($ch, CURLOPT_URL, 'xxx.com/test/curl');
//curl結果不直接輸出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
//發送post  請求
curl_setopt($ch, CURLOPT_POST, 1);
// urlencoded 后的字符串(及使用了http_build_query方法),類似'para1=val1&para2=val2&...',也可以使用一個以字段名為鍵值,字段數據為值的數組 ,測試當值為數組時候  Content-Type頭將會被設置成multipart/form-data 否則Content-Type 頭會設置為 application/x-www-form-urlencoded 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
 
//允許 cURL 函數執行的最長秒數
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
//不輸出header 頭信息
curl_setopt($ch, CURLOPT_HEADER,0);
//不驗證證書 信任任何證書
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 檢查證書中是否設置域名,0不驗證 0:不檢查通用名稱(CN)屬性
1:檢查通用名稱屬性是否存在
2:檢查通用名稱是否存在,是否與服務器的主機名稱匹配
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//設置 在HTTP請求中包含一個"User-Agent: "頭的字符串
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
 
 
$res = curl_exec($ch);
$error_no = curl_errno($ch);
$info = curl_getinfo($ch);
$err_msg = '';
if ($error_no) {
    $err_msg = curl_error($ch);
} else {
    print_r($res);
    dump($info);
}
curl_close($ch);

 

注意:

  • curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); $postData為數組時Content-Type 頭部會被設置為 multipart/form-data ,通過file_get_contents('php://input')獲取不到post提交的到數據,通過$_POST 可以,$_FILES 可以獲取數據
$ch = curl_init();
$curl_file1 = curl_file_create('C:/Users/shanghai/Pictures/Camera Roll/23.jpg', 'image/png', pathinfo('C:/Users/shanghai/Pictures/Camera Roll/23.jpg',PATHINFO_BASENAME));
$curl_file2 = curl_file_create('C:/Users/shanghai/Pictures/Camera Roll/66tyr.jpg', 'image/png', pathinfo('C:/Users/shanghai/Pictures/Camera Roll/66tyr.jpg',PATHINFO_BASENAME));
 
$postData = [
    'file' => '111',
    'text' => '666',
    'file_name[0]'=>$curl_file1,
    'file_name[1]'=>$curl_file2,
    // 'file_name'=>$curl_file1 ,
];
curl_setopt($ch, CURLOPT_URL, 'xxx.com/test/curl');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($postData));
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
 
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
 
 
$res = curl_exec($ch);
$error_no = curl_errno($ch);
$info = curl_getinfo($ch);
$err_msg = '';
if ($error_no) {
    $err_msg = curl_error($ch);
} else {
    print_r($res);
    // dump($info);
}
curl_close($ch);
 
 
public function actionCurl()
{
    $request = Yii::$app->request;
    dump(file_get_contents('php://input'),$_FILES,$request->post(),$request->getContentType(),$request->getMethod());
}
  • curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); 這個參數值為urlencoded 后的字符串,類似'para1=val1&para2=val2&...'Content-Type 頭部會被設置為 application/x-www-form-urlencoded ,通過file_get_contents('php://input')可以獲取到post提交的到數據,通過$_POST 也可以,$_FILES 獲取不到數據
  • 傳遞json參數
$jsonData = '{"name":"xp","age":"18","sex":"男"}';
$postData = [
'file' => '111',
'text' => '666',
   'file_name[0]'=>$curl_file1,
   'file_name[1]'=>$curl_file2,
// 'file_name'=>$curl_file1 ,
];
$header = [
   'Content-Type: application/json',
   'Content-Length:'.strlen($jsonData),
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
// 設置Content-Type: application/json,傳遞數組數據時, 都獲取不到數據,文件也獲取不到
//curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
//當不設置header 時 
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    1. 設置Content-Type: application/json,傳遞json數據參數時,file_get_contents('php://input')可以獲取到數據,其他獲取不到

    2. 不設置Content-Type: application/json,傳遞json數據參數時,傳遞file_get_contents('php://input')可以獲取到數據,$_FILES獲取不到 ,$_POST 獲取到的是["{"name":"xp","age":"18","sex":"男"}" => ""]

    3. 設置Content-Type: application/json,傳遞數組參數時,獲取不到參數

  • curl 多圖上傳時 ,傳遞的數組要加key
  • //像這樣寫可以獲取到多圖
    $postData = [
        'file' => '111',
        'text' => '666',
       'file_name[0]'=>$curl_file1,
       'file_name[1]'=>$curl_file2,
        // 'file_name'=>$curl_file1 ,
    ];
     
    //這樣寫的話 后面的會覆蓋前面的
    $postData = [
        'file' => '111',
        'text' => '666',
       'file_name[]'=>$curl_file1,
       'file_name[]'=>$curl_file2,
        // 'file_name'=>$curl_file1 ,
    ];
  • 只有當 Content-Type:application/x-www-form-urlencoded 時 php://input 和$_POST都有數據;當Content-Type:multipart/form-data 時 php://input 獲取不到數據,$_FILES 可以獲取上傳圖片的信息,$_POST也有數據;當Content-Type:application/json 並且傳遞的是json 數據時 , php://input可以正常獲取json 數據,$_POST獲取不到;當Content-Type:text/xml php://input可以正常獲取xml 數據,$_POST獲取不到

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM