如何通过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
替换为空白,则不会给出任何响应。