2016-10-12 24 views
0

我目前正在建立一個例程,需要從一個特定的Dropbox文件夾下載文件,將它們發送到另一臺服務器,然後將它們移動到Dropbox上的另一個文件夾。Dropbox API - 錯誤:預計列表得到字典

我正在使用Dropbox的/files/move_batch API端點來執行此操作。

這裏是發送到API的PARAMS移動倍數文件(以及我只是想現在移動一個文件,因爲它仍然沒有工作):

$params = array(
      'headers'   => array(
       'method' => 'POST', 
       'content-type' => 'application/json; charset=utf-8', 
       ), 
      'body' => json_encode(array(
       'entries'   => array(
        'from_path' => self::$files[0], 
        'to_path' => '/Applications/Archives/' . substr(self::$files[0], strrpos(self::$files[0], '/') + 1), 
        ), 
       'autorename'  => true, 
       )), 
      ); 

但我不斷收到同樣的錯誤消息:

Error in call to API function "files/move_batch": request body: entries: expected list, got dict 

我不知道什麼API意味着一個列表或它應該如何形成。

回答

1

entries值應該是一個dictlist,每個要移動的文件之一,每一個都包含一個from_pathto_path。不過,您的代碼將entries的值提供爲單個dict。 (在PHP中,您可以使用array關鍵字使list s和dict s都可以使用。)

當您將其分解爲多個部分時,查看和處理起來更容易。這是一個可以做到這一點的工作示例。

<?php 

    $fileop1 = array(
        'from_path' => "/test_39995261/a/1.txt", 
        'to_path' => "/test_39995261/b/1.txt" 
       ); 

    $fileop2 = array(
        'from_path' => "/test_39995261/a/2.txt", 
        'to_path' => "/test_39995261/b/2.txt" 
       ); 

    $parameters = array(
      'entries' => array($fileop1, $fileop2), 
      'autorename' => true, 
    ); 

    $headers = array('Authorization: Bearer <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/files/move_batch'); 
    curl_setopt_array($ch, $curlOptions); 

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

    curl_close($ch); 

?> 

要使用此批端點移動只是一個文件,你將在該行更改爲類似:

  'entries' => array($fileop1), 
+0

謝謝您的回答!現在我試圖找出如何構建條目參數與許多文件... – rak007