php curl 传参文件

有 curl_file_create 和 CURLFile 两种方法,这两种方法是我亲自测试过的好用(我用的php版本是7,而5.5之前是可以用@这个方法,我没有测试过)

第一种:curl_file_create方法,具体curl_file_create参数请自行百度

php 复制代码
//1 组装传递参数:
$paramsData = [
            'id' => $id,
            'file' => curl_file_create("/tmp/tempVideoImage/1642041087.jpg",'image/jpeg',11),
];

//2 调用curl方法
postDataFileByCurl($api,$paramsData)

//这里多说一句,这里的curl_file_create第一个参数文件的实际路径,可以保存前端传来的文件,用完再删除即可。

第二种方法:CURLFile 可以介绍前端的file参数,直接上传到第三方地址

php 复制代码
//1 组装传递参数:
$paramsData = [
        'id' => $id,
        'file' => new \CURLFile($_FILES["imagePath"]['tmp_name'],$_FILES["imagePath"]['type'],$_FILES["imagePath"]['name']), // 使用CURLFile类 【参数是前端传输过来的】;
];

//2 调用curl方法
postDataFileByCurl($api,$paramsData)

第三方地址介绍参数(也是php)直接用_FILES就可以

php 复制代码
 var_dump($_POST);
    var_dump($_FILES);

curl公共方法

php 复制代码
function postDataFileByCurl($post_url, $parameters)
    {
        $timeout =  10;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $post_url);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
        ob_start();
        $exe_result = curl_exec($ch);
        ob_end_clean();
        curl_close($ch);
        return $exe_result;
}
相关推荐
ServBay2 天前
告别面条代码,PSL 5.0 重构 PHP 性能与安全天花板
后端·php
JaguarJack4 天前
FrankenPHP 原生支持 Windows 了
后端·php·服务端
BingoGo4 天前
FrankenPHP 原生支持 Windows 了
后端·php
JaguarJack5 天前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo5 天前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack6 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay7 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954487 天前
CTF 伪协议
php
BingoGo9 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack9 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端