关于PHP CURL上传二进制流图片
16lz
2021-01-22
前言
项目中模块数据由PHP
爬虫进行更新,当检测到有新图片时需要上传到跨地区的CDN回源服务器(静态资源服务器),服务器负责人只提供一个上传API
解决方法
1.将图片保存到本地再使用PHP CURL
+ new \CURLFile($path)
上传(缺点: IO操作)
2.模拟拼接请求数据报文,将图片以二进制文件直接发送给上传API
√
composer require ar414/curl-upload-binary-image
<?phprequire_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);
解决思路
1.重温HTTP知识
2.通过postman
,Google Chrome
上传文件 查看发送的请求数据
3.拼接请求体
- set Header
multipart/form-data; boundary={md5(microtime())}
- set Body Block
Content-Type: application/octet-stream