转换cURL以在php函数中使用 [英] Convert cURL to be used in php function
问题描述
目前,我正在 box 平台上工作,并尝试上传文件到盒子服务器。 Box使用cURL上传文件,而我正尝试从php发送cURl请求。到目前为止,我已经将大多数cURL逗号转换为php行话,但是我不知道如何传递要上传文件的属性(名称,路径,包含文件夹)。
Currently im working on box platform, and trying to upload a file to the box server. Box uses cURL to upload files, and i'm trying to send cURl requests from php. So far i've converted most of the cURL commads to php jargon, but i could't figure out how to pass in the attributes(name, path, containing folder) of the file to be uploaded.
这里是cURL
curl https://upload.box.com/api/2.0/files/content -H "Authorization: Bearer APP_USER_TOKEN" -X POST -F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}' -F file=@Jon_Snow.jpeg
这是不完整的cURL命令的php版本。
and here is the php version for the incomplete cURL command.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://upload.box.com/api/2.0/files/content");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Bearer ".json_decode($accessToken, true )['access_token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
我该怎么做cURL命令的最后一部分?
How do i do the last part of the cURL command which is
-F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}' -F file=@Jon_Snow.jpeg
编辑:可能重复的建议不准确,我在问以 -F attribute ='{ name: Jon_Snow.jpeg, parent:{ id: 0}}''形式将属性添加到上载文件的方法
我看不到建议的答案如何
the suggestion of 'possible duplicate' is not accurate, i am asking of a way to add attributes to the uploaded files in the form -F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}'
i dont see how the suggested answer is relevant
推荐答案
找到答案了,我需要 json_encode
属性数组,然后使用 new \CURLFile()
函数创建文件句柄,它不适用于 realpath()
Found the answer, i needed to json_encode
the attributes array and then use new \CURLFile()
function to create a file handle, it did NOT work with realpath()
$attributes = array('name'=>$fileName,'parent'=>array('id'=>$folderId));
$file = new \CURLFile($filePath);
$fields = array('attributes' => json_encode($attributes), 'file' => $file);
$headers = array("Authorization: Bearer ".$accessToken);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uploadUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
这篇关于转换cURL以在php函数中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!