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

About PHP CURL uploading binary stream images

藏色散人
Release: 2023-04-09 17:22:01
forward
5376 people have browsed it

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
Copy after login
<?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);
Copy after login

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!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template