php模拟文件上传使用curl向远程服务器上传文件,php将图片转成二进制文件进行请求接口上传

原生phpCURLFile类向服务器上传文件,代码

// 要上传的文件路径
$file_path = '/path/to/file.txt';
// 远程服务器接收文件的 API 地址
$upload_url = 'http://example.com/upload.php';
// 创建 CURLFile 对象
$file = new CURLFile($file_path);
// 构建 POST 数据
$data = array(
    'file' => $file,
    'name' => basename($file_path),
);
// 创建 CURL 请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发送请求并获取响应
$response = curl_exec($ch);
// 检查是否上传成功
if ($response === false) {
    
    
    echo '上传失败:' . curl_error($ch);
} else {
    
    
    echo '上传成功:' . $response;
}
// 关闭 CURL 请求
curl_close($ch);

代码中,CURLFile 类**(CURLFile类源码附到了文章最后面
)**创建一个 CURL 文件对象。接着构建 POST 数据,将文件对象作为参数值传递。最后使用 curl 发送请求,获取响应并检查是否上传成功。

Tp5版本CURLFile类向服务器上传文件,代码

// 要上传的文件路径
$file_path = '/path/to/file.txt';
// 远程服务器接收文件的 API 地址
$upload_url = 'http://example.com/upload.php';
// 创建 CURLFile 对象
$file = new \CURLFile($file_path);
// 构建 POST 数据
$data = array(
    'file' => $file,
    'name' => basename($file_path),
);
// 创建 CURL 请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发送请求并获取响应
$response = curl_exec($ch);
// 检查是否上传成功
if ($response === false) {
    
    
    echo '上传失败:' . curl_error($ch);
} else {
    
    
    echo '上传成功:' . $response;
}
// 关闭 CURL 请求
curl_close($ch);

代码中,使用 TP5 内置的 CURLFile 类创建一个 CURL 文件对象。

CURLFile类源码

class CURLFile {
    
    
    private $name;
    private $mime = '';
    private $postname = '';
    public function __construct($filename, $mimetype = '', $postname = '') {
    
    
        $this->name = $filename;
        if ($mimetype) {
    
    
            $this->mime = $mimetype;
        }
        if ($postname) {
    
    
            $this->postname = $postname;
        }
    }
    public function getName($as_array = false) {
    
    
        if ($as_array) {
    
    
            return array('name' => $this->name, 'type' => $this->mime, 'postname' => $this->postname);
        }
        return $this->name;
    }
    public function getMimeType() {
    
    
        return $this->mime;
    }
    public function getPostFilename() {
    
    
        return $this->postname;
    }
    public function __toString() {
    
    
        return sprintf("@%s;filename=%s%s", $this->name, $this->postname, $this->mime ? ";type=" . $this->mime : '');
    }
}

CURLFile类详细解读

CURLFile 类是 PHP 内置的一个类,用于在使用 CURL 上传文件时,构建文件对象。从源码中可以看出,CURLFile 类中包含了以下四个方法:

__construct($filename, $mimetype = ‘’, $postname = ‘’):构造函数,用于创建 CURLFile 对象,需要传递文件路径、文件类型和文件名参数。

getName($as_array = false):获取文件路径,如果 $as_array 参数为 true,则返回一个包含文件路径、文件类型和文件名的关联数组。

getMimeType():获取文件类型。

getPostFilename():获取文件名。

__toString():将 CURLFile 对象转换成字符串,用于在 CURL POST 请求中发送数据。
总之,通过 CURLFile 类,我们可以方便地构建文件对象,从而实现文件上传功能。

猜你喜欢

转载自blog.csdn.net/gjwgjw1111/article/details/129706949