关于PHP CURL上传二进制流图片

推荐:《PHP视频教程》

PHP CURL 上传二进制流图片

前言

项目中模块数据由PHP爬虫进行更新,当检测到有新图片时需要上传到跨地区的CDN回源服务器(静态资源服务器),服务器负责人只提供一个上传API

解决方法

1.将图片保存到本地再使用PHP CURL + new \CURLFile($path)上传(缺点: IO操作)

2.模拟拼接请求数据报文,将图片以二进制文件直接发送给上传API

composer require ar414/curl-upload-binary-image
<?php

require_once &#39;../vendor/autoload.php&#39;;

use Ar414\UploadBinaryImage;

$url = &#39;http://0.4.1.4:414/upload?path=/test/&#39;;
$fields = [];
$fieldName = &#39;file&#39;;
$fileName = &#39;ar414.png&#39;;

$fileBody = file_get_contents(&#39;https://github.com/ar414-com/ar414-com/raw/master/assets/ar414.png&#39;);

$ret = UploadBinaryImage::upload($url,$fields,$fieldName,$fileName,$fileBody);
var_dump($ret);

解决思路

1.重温HTTP知识

2.通过postmanGoogle Chrome 上传文件 查看发送的请求数据

3.拼接请求体

  • set Header multipart/form-data; boundary={md5(microtime())}
  • set Body Block Content-Type: application/octet-stream

以上就是关于PHP CURL上传二进制流图片的详细内容,更多请关注https://www.sxiaw.com/其它相关文章!