使用php在Box中创建文件夹

时间:2012-11-12 09:54:48

标签: php api curl box-api

我正在尝试在Box上创建一个简单的文件夹,但我很难过。我一直收到这个错误:

  

{ “类型”: “错误”, “状态”:404, “代码”: “NOT_FOUND”, “HELP_URL”: “http://developers.box.com/docs/#errors”, “消息” :“未找到”,“request_id”:“71794126250a0c2f00fe35”}

这是我的代码:

$parent = array();
$parent['id'] = '0';
$params = array();
$params['name'] = 'Testfolder';
$params['parent'] = $parent;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/folders");
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: BoxAuth api_key={myAPIkey}&auth_token={myToken}"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

我错过了什么。 请帮忙。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

根据BOX Docs

  

默认情况下,请求正文数据应为JSON和响应正文   数据以JSON的形式发回。

$parent = array();
$parent['id'] = '0';
$params = array();
$params['name'] = 'Testfolder';
$params['parent'] = $parent;

$params = json_encode($params);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/folders");
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($params), Authorization: BoxAuth api_key={myAPIkey}&auth_token={myToken}"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);