HTTP上传到保管箱并共享

时间:2018-12-08 19:40:38

标签: php rest api dropbox

我有保管箱文件上传。以下代码可以正常工作,但是我想自动与“具有链接的任何人”共享并返回共享链接,以便可以在程序中引用它。

PHP代码

$DROPBOX_path = 'folder/subfolder1/subfolder2/user.png';
$path = './tmp/user.png';
$fp = fopen($path, 'rb');
$size = filesize($path);

$cheaders = array('Authorization: Bearer '. $DROPBOX_ACCESS_TOKEN,
        'Content-Type: application/octet-stream',
        'Dropbox-API-Arg: '.
        json_encode(
            array(
                "path"=> '/'.$DROPBOX_path,
                "mode" => "add",
                "autorename" => false,
                "mute" => true

            )
        )

    );


$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

echo $response;
curl_close($ch);
fclose($fp);

我已经尝试过,但是出现错误: 调用API函数“ sharing / create_shared_link_with_settings”时出错:请求正文:无法将输入解码为JSON

    $cheaders = array('Authorization: Bearer '. $DROPBOX_ACCESS_TOKEN,
        'Content-Type: application/json',
        'data: '.
        json_encode(
            array(
                "path"=> '/'.$DROPBOX_path,
                "settings" => array("requested_visibility" => "public")

            )
        )

    );

    $ch = curl_init('https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings');
//$ch = curl_init('https://api.dropboxapi.com/1/shares/auto/');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

echo $response;
curl_close($ch);
fclose($fp);

1 个答案:

答案 0 :(得分:0)

此问题已解决

$parameters = array('path' => '/'.$DROPBOX_path);

$headers = array('Authorization: Bearer '.$DROPBOX_ACCESS_TOKEN,
                 'Content-Type: application/json');

$curlOptions = array(
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode($parameters),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_VERBOSE => true
    );

$ch = curl_init('https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings');
curl_setopt_array($ch, $curlOptions);

$response = curl_exec($ch);
echo $response;

curl_close($ch);