首页 文章

HTTP上传到Dropbox并共享

提问于
浏览
0

我有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

    这解决了它

    $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);
    

相关问题