简体   繁体   English

在PHP curl中发布文件

[英]Posting a file in php curl

I'm trying to post a file using curl due to this rest api using a multiform. 由于此REST API使用的是多格式格式,因此我尝试使用curl发布文件。

Here's what I currently have. 这是我目前拥有的。

    $curlFile = new \CURLFile("shirt.png");
    $data = array
    (
        "apikey" => $key,
        "isTgaUploadEnabled" => "True",
        "onVerificationPage" => "False",
        "file" => $curlFile,
    );

    curl_setopt_array($curl, array(
        CURLOPT_HEADER => TRUE,
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_POSTFIELDS => $data,
    ));

It doesn't work using the CURLFile class, any suggestions on what I should do? 使用CURLFile类不起作用,关于应该怎么做的任何建议?

You are looking for this on PHP5.5 and above: 您正在PHP5.5及更高版本上寻找此功能:

$file = new CURLFile('cats.jpg','image/jpeg','test_name');
$args= array('test_file' => $file);

Or this on PHP5.4 or below: 或者在PHP5.4或以下版本中:

$args['file'] = '@/path/to/file';

Just don't forget to set appropriate flags: 只是不要忘记设置适当的标志:

curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);

https://wiki.php.net/rfc/curl-file-upload https://wiki.php.net/rfc/curl-file-upload

http://php.net/manual/en/class.curlfile.php http://php.net/manual/en/class.curlfile.php

http://php.net/manual/en/curlfile.construct.php http://php.net/manual/en/curlfile.construct.php

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

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