經過curl上傳圖片php
PHP < 5.5:
使用 目前使用的php版本 7.1 測試沒法使用 前面加@ 的方法上傳文件 ,查找資料 使用 curl_setopt($ch,CURLOPT_SAFE_UPLOAD,FALSE) 能夠解決,可是經測試 這種方式不行,顯示的CURLOPT_SAFE_UPLOAD這個選項在該版本php中已經被廢棄json
能夠經過檢測 有沒有 curl_file_create 這個函數 也能夠檢測 有沒有類\CURLFile class_exists('\CURLFile')segmentfault
$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 後的字符串,相似'para1=val1¶2=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);
注意:服務器
$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¶2=val2&...'Content-Type 頭部會被設置爲 application/x-www-form-urlencoded ,經過file_get_contents('php://input')能夠獲取到post提交的到數據,經過$_POST 也能夠,$_FILES 獲取不到數據
app
$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);
設置Content-Type: application/json,傳遞json數據參數時,file_get_contents('php://input')能夠獲取到數據,其餘獲取不到
curl
不設置Content-Type: application/json,傳遞json數據參數時,傳遞file_get_contents('php://input')能夠獲取到數據,$_FILES獲取不到 ,$_POST 獲取到的是["{"name":"xp","age":"18","sex":"男"}" => ""]
函數
設置Content-Type: application/json,傳遞數組參數時,獲取不到參數
post
//像這樣寫能夠獲取到多圖 $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 , ];
參考:測試