PHP CURL 上传二进制流图片 
                                                    
                        
                    
                    
  
                    
                    
前言
项目中模块数据由PHP爬虫进行更新,当检测到有新图片时需要上传到跨地区的CDN回源服务器(静态资源服务器),服务器负责人只提供一个上传API
解决方法
- 将图片保存到本地再使用
PHP CURL+new \CURLFile($path)上传(缺点: IO操作) - 模拟拼接请求数据报文,将图片以二进制文件直接发送给
上传API√ 
composer require ar414/curl-upload-binary-image
<?php
require_once '../vendor/autoload.php';
use Ar414\UploadBinaryImage;
$url = 'http://0.4.1.4:414/upload?path=/test/';
$fields = [];
$fieldName = 'file';
$fileName = 'ar414.png';
$fileBody = file_get_contents('https://github.com/ar414-com/ar414-com/raw/master/assets/ar414.png');
$ret = UploadBinaryImage::upload($url,$fields,$fieldName,$fileName,$fileBody);
var_dump($ret);
解决思路
- 重温HTTP知识
 - 通过
postman,Google Chrome上传文件 查看发送的请求数据 - 拼接请求体
- set Header 
multipart/form-data; boundary={md5(microtime())} - set Body Block 
Content-Type: application/octet-stream 
 - set Header 
 
本作品采用《CC 协议》,转载必须注明作者和本文链接
          
          
          
                关于 LearnKu
              
                    
                    
                    
 
推荐文章: