Home >Backend Development >PHP Tutorial >About PHP CURL uploading binary stream images

About PHP CURL uploading binary stream images

藏色散人
藏色散人forward
2020-12-25 15:51:375379browse

Recommended: "PHP Video Tutorial"

PHP CURL 上传二进制流图片

##Preface

The module data in the project is updated by the

PHP crawler. When a new image is detected, it needs to be uploaded to a 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 image locally and then use

PHP CURL new \CURLFile($path) Upload (disadvantage: IO operation)

2. Simulate splicing request data message and send the image directly to

Upload API √## as a binary file #

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);

Solution ideas

1. Review HTTP knowledge

2.Through

postman

, Google Chrome Upload the file to view the sent request data3. Splice the request body

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

The above is the detailed content of About PHP CURL uploading binary stream images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete