PHP CURL publish binary stream pictures
#Foreword
The module data in the project is updated by the PHP
crawler. When a new image is detected, it needs to be uploaded to the cross-regional CDN back-to-origin server (static resource server). The person in charge of the server only provides an upload API
.
#Solution
- Save the picture locally and upload it with
PHP CURL
+new \CURLFile($path)
(Disadvantage: IO operation) - Simulate the splicing request data message, and send the picture directly to the
upload API
as a binary fileā
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);
#Solutions
- Revisit HTTP knowledge
- Upload files through
postman
,Google Chrome
to view the sent request data - Splicing request body
- set Header
multipart/form-data; boundary={md5(microtime())}
- set Body Block
Content-Type: application/octet-stream
- set Header
#Support Author
Did you enjoyed the article ?