簡體   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