从过去几天开始,我一直在寻找如何使用 Curl 在 PHP >=5.5 中上传文件。终于找到新的CurlFile方法,但无法使用远程 URL。这是我正在使用的代码:
$access_token = 'MY_API_ACCESS_TOKEN';
$fields = array(
"name" => $name,
"parent" => array(
"id" => $folder_id
)
);
$another = array(
'attributes' => json_encode($fields),
'file' => new CurlFile($remoteUrl)
);
$header = array (
"Authorization: Bearer $access_token",
"Content-Type: multipart/form-data"
);
$options = array(
CURLOPT_URL => $UPLOAD_URL,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $another,
CURLOPT_HTTPHEADER => $header,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
更换
$remoteUrl
时使用本地文件路径一切正常。Q] CurlFile 可以远程上传吗?如果是,我做错了什么?
最佳答案
好的,我找到了答案:Source
无法使用 curl 上传远程文件.必须先在本地下载文件,然后使用 CurlFile 上传。
关于PHP:使用远程 URL 上传 CurlFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37818268/