PHP curl upload binary stream image

Time:2021-7-26

PHP curl upload binary stream image

preface

Module data in the project is controlled byPHPThe crawler updates. When a new picture is detected, it needs to be uploaded to the cross regional CDN back to the source server (static resource server). The server leader only provides oneUpload API

resolvent

  1. Save the picture locally for reusePHP CURL + new \CURLFile($path)Upload (disadvantage: IO operation)
  2. Simulate the splicing request data message and send the picture directly to the user as a binary fileUpload 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);

Solution ideas

  1. Review HTTP knowledge
  2. adoptpostmanGoogle ChromeUpload file to view the sent request data
  3. Splice request body
    • set Header multipart/form-data; boundary={md5(microtime())}
    • set Body Block Content-Type: application/octet-stream

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]