zoukankan      html  css  js  c++  java
  • php 使用GuzzleHttp请求接口,传输文件buffer

    你可以使用composer.phar客户端将Guzzle作为依赖添加到项目:

    php composer.phar require guzzlehttp/guzzle:~6.0
    

      或者,你可以编辑项目中已存在的composer.json文件,添加Guzzle作为依赖:

    {
       "require": {
          "guzzlehttp/guzzle": "~6.0"
       }
    }
    

      安装完毕后,你需要引入Composer的自动加载文件:

    require 'vendor/autoload.php';
    
            $filePath=BASE_PATH.'/storage/file/1.jpg';
            $request='{"requestId":"zzsc20200812000000","platformCode":"10012935181","billAccount":"test3485_003@163.com","buffer":"jpg","fileName":"test.jpg"}';
            $request=json_decode($request,true);
            $buffer=readfile($filePath);//文件buffer
            $request['buffer']=base64_encode($buffer);
            $url='http://192.168.1.130:9501/fileUpload/test';
            $headers = [
                "X-99Bill-PlatformCode:10012935181"
            ];
            $body=['headers'=>$headers,'json'=>$request];    
          $options = [];
            // $client 为协程化的 GuzzleHttpClient 对象
            $clientFactory=container()->get(ClientFactory::class);
            $client = $clientFactory->create($options);
      
            $response = $client->request($method,$url,$body);
            // $code = $response->getStatusCode(); // 200
            // $reason = $response->getReasonPhrase(); // OK
            $body = $response->getBody();
            $responseContents = $body->getContents();
            return $responseContents;        
    

      

  • 相关阅读:
    百度网盘免费下载加速
    linux docker 知识点总结
    openwrt 支持 EC200T
    openwrt 如何添加新设备支持
    openwrt DEPEND
    menuconfig kconfig
    golang 汇编
    按值传递
    YAPI认证用户利用Mock功能远程代码执行事件通告
    携程持久化KV存储实践
  • 原文地址:https://www.cnblogs.com/blog-dyn/p/13494993.html
Copyright © 2011-2022 走看看