繁体   English   中英

在PHP curl中发布文件

[英]Posting a file in php curl

由于此REST API使用的是多格式格式,因此我尝试使用curl发布文件。

这是我目前拥有的。

    $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,
    ));

使用CURLFile类不起作用,关于应该怎么做的任何建议?

您正在PHP5.5及更高版本上寻找此功能:

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

或者在PHP5.4或以下版本中:

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

只是不要忘记设置适当的标志:

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

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

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

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

暂无
暂无

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

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