zoukankan      html  css  js  c++  java
  • Laravel实现from的curl文件转发

    文件的使用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 AppHttpControllers;
     11 
     12 use AppTraitsCurlTraits;
     13 use IlluminateHttpRequest;
     14 use IlluminateSupportFacadesConfig;
     15 use IlluminateSupportFacadesStorage;
     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 AppTraits;
    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 }
  • 相关阅读:
    POJ 1062 坑爹的聘礼(枚举等级差选择性找边)
    c++ string函数详细返回值及用法!
    POJ 2240 利率变权值 Floyd变乘法
    POJ 1797 最大运载量
    API code
    编程题目的讨论
    C语言位运算符:与、或、异或、取反、左移和右移
    &与&& C语言
    反思
    CreateWindow的出错解决
  • 原文地址:https://www.cnblogs.com/phpk/p/11187156.html
Copyright © 2011-2022 走看看