文件的使用curl分發時發現不能直接將其傳入curl,需要使用CURLFile()來實現
分發類
1 <?php 2 /** 3 * 請求轉發控制器 4 * Created by PhpStorm. 5 * User: xinchen 6 * Date: 2019/07/11 7 * Time: 18:10 8 */ 9 10 namespace App\Http\Controllers; 11 12 use App\Traits\CurlTraits; 13 use Illuminate\Http\Request; 14 use Illuminate\Support\Facades\Config; 15 use Illuminate\Support\Facades\Storage; 16 17 class PortForwardController extends Controller 18 { 19 use CurlTraits; 20 21 /** 22 * 分發請求 23 * @param Request $request 24 * @param $method 25 * @param $system_urlpok 26 * @param $requset_url 27 * @return array|bool|mixed 28 */ 29 public function ForwardRequest(Request $request,$method,$system_url){ 30 //將請求數據改為數組 31 $arr = $request->toArray(); 32 33 //獲取請求的url 34 $requset_url = $arr['url']; 35 36 //需要在sercice 添加端口配置 37 $system_url = Config::get('services.project_ip')[$system_url]; 38 39 //拼接url 40 $url = $system_url . $requset_url; 41 42 //獲取請求方式 43 $requestMethod = $method == 'post' ? 1 : 0; 44 45 //去掉多余參數 46 unset($arr['url']); 47 unset($arr['token']); 48 49 //獲取參數 50 $params = $arr; 51 52 $fileCharater = $request->file('file'); 53 if($fileCharater){ 54 if ($fileCharater->isValid()) { 55 //獲取文件相關信息 56 $originalName = $fileCharater->getClientOriginalName(); // 文件原名 57 $ext = $fileCharater->getClientOriginalExtension(); // 擴展名 58 $realPath = $fileCharater->getRealPath(); //臨時文件的絕對路徑 59 60 // 判斷文件是否為指定的上傳文件后綴 該步驟可以刪除,除非確定只需要該類型 61 // if (!in_array($ext, array('xls', 'xlsx'))) { 62 // 返回上一次請求位置,並攜帶錯誤消息 63 //return redirect()->back()->withErrors('請輸入xls或xlsx后綴文件')->withInput(); 64 //} 65 $filename = date('Y-m-d') .$originalName. '-' . uniqid() . '.' . $ext; 66 Storage::disk('gateway')->put($filename, file_get_contents($realPath)); //文件存到本地,需要在config文件夾的filesystem的定義 67 $filePath = storage_path('gateway/'.$filename); 68 $obj = new \CURLFile($filePath); 69 $params['file'] = $obj; 70 } 71 } 72 73 //判斷網址是否為https 74 $https = isHttps($url); 75 76 //使用curl獲取數據 77 $res = $this->curl($url,$params,$requestMethod,$https); 78 79 if(isset($filePath)){ 80 unlink($filePath); 81 } 82 83 if(!$res) { 84 //數據獲取失敗 85 return [ 86 "status" => "error", 87 'code' => 4000000, 88 'message' => '數據加載失敗,接口報錯,請聯系管理員', 89 'data' => [] 90 ]; 91 }else{ 92 //獲取成功,直接返回 93 return $res; 94 } 95 } 96 97 public function testView(){ 98 return view('testup'); 99 } 100 }
config文件夾的filesystem添加本地空間目錄
1 // 新建一個本地端gateway空間(目錄) 用於存儲上傳的文件 2 'gateway' => [ //名字需要與上面分發類代碼中的一致 3 4 'driver' => 'local', 5 6 // 文件將上傳到storage/exports目錄 7 'root' => storage_path('gateway'), 8 9 ],
Curl的trait
1 <?php 2 /** 3 * curl公用trait 4 * Created by PhpStorm. 5 * User: xinchen 6 * Date: 2019/07/12 7 * Time: 09:28 8 */ 9 10 namespace App\Traits; 11 12 trait CurlTraits 13 { 14 15 /** 16 * 獲取當前用戶服務中的店鋪 17 * **/ 18 public static function curl($url, $params = false, $ispost = 0, $https = 0) 19 { 20 // $httpInfo = array(); 21 $ch = curl_init(); 22 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 23 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'); 24 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 25 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 26 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 27 if ($https) { 28 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 對認證證書來源的檢查 29 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書中檢查SSL加密算法是否存在 30 } 31 if ($ispost) { 32 curl_setopt($ch, CURLOPT_POST, true); 33 curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 34 curl_setopt($ch, CURLOPT_URL, $url); 35 } else { 36 if ($params) { 37 if (is_array($params)) { 38 $params = http_build_query($params); 39 } 40 curl_setopt($ch, CURLOPT_URL, $url . '?' . $params); 41 } else { 42 curl_setopt($ch, CURLOPT_URL, $url); 43 } 44 } 45 46 $response = curl_exec($ch); 47 48 if ($response === FALSE) { 49 //echo "cURL Error: " . curl_error($ch); 50 return false; 51 } 52 // $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //暫時不需要 53 // $httpInfo = array_merge($httpInfo, curl_getinfo($ch)); //暫時不需要 54 curl_close($ch); 55 56 return $response; 57 } 58 }
