使用卷曲和PHP发送文件Anonfile

问题描述 投票:0回答:1

我试图使用上anonfile卷曲和PHP发送一个文件,但我得到这个JSON:

{ “状态”:假, “错误”:{ “消息”: “否选择文件。”, “类型”: “ERROR_FILE_NOT_PROVIDED”, “代码”:10}}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://anonfile.com/api/upload");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,'test.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
print_r($server_output);
curl_close ($ch);

换句话说,如何翻译这个命令到PHP?

curl -F "[email protected]" https://anonfile.com/api/upload

我试了几个例子在那里,但仍然没有线索

$target_url = 'https://anonfile.com/api/upload';
$args['file'] = '@/test.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
php curl
1个回答
1
投票
curl_setopt($ch, CURLOPT_POSTFIELDS,'test.txt');

不会工作,因为它真的只是发送文本字符串test.txt

$args['file'] = '@/test.txt';

不会起作用,因为上传文件@前缀在PHP 5.5中弃用,残疾人按默认的PHP 5.6,在PHP 7.0完全去除。在PHP 5.5及更高版本,使用CURLFilemultipart/form-data格式上传文件。

作为PHP 5.5及更高版本(这是古代在这一点上)的,

curl -F "[email protected]" https://anonfile.com/api/upload

翻译成

$ch=curl_init();
curl_setopt_array($ch,array(
    CURLOPT_URL=>'https://anonfile.com/api/upload',
    CURLOPT_POST=>1,
    CURLOPT_POSTFIELDS=>array(
        'file'=>new CURLFile("test.txt")
    )
));
curl_exec($ch);
curl_close($ch);
© www.soinside.com 2019 - 2024. All rights reserved.