PHP CURL publish binary stream pictures

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

  1. Save the picture locally and upload it with PHP CURL + new \CURLFile($path) (Disadvantage: IO operation)
  2. 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

  1. Revisit HTTP knowledge
  2. Upload files through postman, Google Chrome to view the sent request data
  3. Splicing request body
    • set Header multipart/form-data; boundary={md5(microtime())}
    • set Body Block Content-Type: application/octet-stream

#Support Author

coffee


Did you enjoyed the article ?
Subscribe to the newsletter šŸ‘Øā€šŸ’»

Be the first to know when I write something new! If you don't like emails, you can follow me on GitHub.