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;        

  

原文地址:https://www.cnblogs.com/blog-dyn/p/13494993.html