简体   繁体   English

使用 Curl 和 PHP 将文件发送到 Anonfile

[英]Sending file to Anonfile using Curl and PHP

I'm trying to send a file using curl and PHP on anonfile but I get this json:我正在尝试在anonfile上使用 curl 和 PHP 发送文件,但我得到了这个 json:

{"status":false,"error":{"message":"No file chosen.","type":"ERROR_FILE_NOT_PROVIDED","code":10}} {"status":false,"error":{"message":"未选择文件。","type":"ERROR_FILE_NOT_PROVIDED","code":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);

In other words, how to translate this command into PHP?换句话说,如何将这个命令翻译成PHP?

curl -F "file=@test.txt" https://anonfile.com/api/upload

I tried several examples out there but still no clue我尝试了几个例子,但仍然没有线索

$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;
curl_setopt($ch, CURLOPT_POSTFIELDS,'test.txt');

won't work because it's literally just sending the literal string test.txt不会工作,因为它实际上只是发送文字字符串test.txt

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

won't work because the @ prefix to upload files was deprecated in PHP 5.5, disabled-by-default in PHP 5.6, and completely removed in PHP 7.0.将不起作用,因为上传文件的@前缀在 PHP 5.5 中已弃用,在 PHP 5.6 中默认禁用,并在 PHP 7.0 中完全删除。 in PHP 5.5 and higher, use CURLFile to upload files in the multipart/form-data format.在 PHP 5.5 及更高版本中,使用CURLFilemultipart/form-data格式上传文件。

as of PHP 5.5+ (which is ancient at this point),从 PHP 5.5+ 开始(在这一点上很古老),

curl -F "file=@test.txt" https://anonfile.com/api/upload

translates to翻译成

$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);
$request = curl_init('https://api.anonfiles.com/upload');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, 
CURLOPT_SAFE_UPLOAD, true); curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    [
        'file' => new CURLFile(realpath('test.txt'), 'text/plain'),
    ] );
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

echo curl_exec($request);

var_dump(curl_getinfo($request));

curl_close($request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM