无法将CURLFile类的对象转换为字符串

如何通过cURL实现图片上传?
我正在使用zamzar沙箱api将pdf文件转换为.txt文件。如果我为$sourceFile variable使用pdf的“ https://s3.amazonaws.com/zamzar-samples/sample.pdf”路径,则其工作正常。但是,如果我使用系统路径“ test.pdf”,则它不起作用。

我试图通过curl_file_create,但出现以下错误:

  

可恢复的致命错误:不能为类CURLFile的对象   转换为字符串。

我正在使用PHP版本7.3.0

<?php
error_reporting(E_ALL);
$endpoint = "https://api.zamzar.com/v1/jobs";
$apiKey = "GiVUYsF4A8ssq93FR48H";
$sourceFile = "https://s3.amazonaws.com/zamzar-samples/sample.pdf";//new CurlFile("test.pdf");
$targetFormat = "txt";


$postData = array(
  "source_file" => $sourceFile,"target_format" => $targetFormat
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$endpoint);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_SAFE_UPLOAD,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,CURLOPT_USERPWD,$apiKey . ":");
$body = curl_exec($ch);
curl_close($ch);
$response = json_decode($body,true);
echo "Response:\n---------\n";
echo"<pre>";
print_r($response);
?>

Response: ---------
Array
(
    [id] => 8442473
    [key] => GiVUYsF4A8ssq93FR48H
    [status] => initialising
    [sandbox] => 
    [created_at] => 2019-11-12T10:35:01Z
    [finished_at] => 
    [import] => Array
        (
            [id] => 671784
            [url] => https://s3.amazonaws.com/zamzar-samples/sample.pdf
            [status] => initialising
        )

    [target_files] => Array
        (
        )

    [target_format] => txt
    [credit_cost] => 0
)

上面的例子也给了我很好的输出。如果我将amazonaws中的system path替换为空白,则不会给出任何响应。

caoxingxiecan 回答:无法将CURLFile类的对象转换为字符串

CURLOPT_SAFE_UPLOAD始终设置为false,curl期望因此将文件作为字符串(@/path/to/file)传递。对于您而言,代码将文件作为对象发送,因为它在可用时使用curl_file_create()

CURLOPT_SAFE_UPLOAD设置为false时,请勿使用curl_file_create()(这是向后兼容的标志)。

此外,尝试在CURLOPT_POSTFIELDSsee here why)之后设置CURLOPT_SAFE_UPLOAD

PHP 5.x是not supported anymore,我建议摆脱旧的语法(@/path/to/fileCURLOPT_SAFE_UPLOAD = false),并始终使用curl_file_create()

本文链接:https://www.f2er.com/3165562.html

大家都在问