【发布时间】:2020-09-21 11:36:51
【问题描述】:
具体与这个特定线程 Upload a file to a website using php 相关,我想在 PHP 上使用 cURL 来实现它。
如何设置?
以下是我尝试过的。
请注意,我只是通过添加我尝试过的代码来编辑它。
<?php
if(isset($_POST["submit"])){
$errors= array();//
$name = $_FILES['fileToUpload']['name'];
$size = $_FILES['fileToUpload']['size'];
$type = $_FILES['fileToUpload']['type'];
$tmp_name = $_FILES['fileToUpload']['tmp_name'];
$upload_ok =1;
$file_ext=strtolower(end(explode('.',$_FILES['fileToUpload']
['name'])));//
$extensions= array("pdf","jpeg","jpg","png");
$jj = file_get_contents($_FILES['fileToUpload']['tmp_name']);
$mj = base64_encode($jj);
if(in_array($file_ext,$extensions)=== false){
$errors[]="vvvvv.";
}
if($size >= xxx){
$errors[]='xxx;'
}
if(empty($errors)==true){
echo "Success </br>";
$request = "xxxxxxxxx";
$headers = array(
"Authorization: Bearer xxxxxx",
"Content-type => multipart/form-data",
"Accept:application/json",
);
$data = [
'voucher_data' => 'base64_encode($tmp_name)',
'callback_url' => 'xxxxxxxxx'
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "xxxxxxxxxx",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($curl);
curl_close($curl);
var_dump($response);
}else{
print_r($errors);
}
?>
我尝试按照下面的建议使用 curl_file_create($mj) 但在 var_dump 上...它说 bool(false)
【问题讨论】:
-
请提供您迄今为止所做的示例代码。请阅读更多关于如何提供mcve
标签: php json api curl file-upload