PHP curl upload binary stream image


PHP curl upload binary stream image


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


  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

require_once '../vendor/autoload.php';

use Ar414\UploadBinaryImage;

$url = '';
$fields = [];
$fieldName = 'file';
$fileName = 'ar414.png';

$fileBody = file_get_contents('');

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

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 […]