2

Im having problems with the dropbox API.

When i try to get the metadata for my folder, i get the data output like this:

{"hash":"10f86b5b7c9c9276501f67a71ecd41c9","thumb_exists":false,"bytes":0,"path":"\/","is_dir":true,"size":"0 bytes","root":"app_folder","contents":[{"revision":7,"rev":"7069e0896","thumb_exists":true,"bytes":19749,"modified":"Tue, 20 Mar 2012 05:06:43 +0000","client_mtime":"Mon, 26 Sep 2011 11:50:43 +0000","path":"\/1_sml.jpg","is_dir":false,"icon":"page_white_picture","root":"dropbox","mime_type":"image\/jpeg","size":"19.3 KB"},{"revision":6,"rev":"6069e0896","thumb_exists":true,"bytes":15797,"modified":"Tue, 20 Mar 2012 05:06:43 +0000","client_mtime":"Mon, 26 Sep 2011 11:51:09 +0000","path":"\/2_sml.jpg","is_dir":false,"icon":"page_white_picture","root":"dropbox","mime_type":"image\/jpeg","size":"15.4 KB"},{"revision":5,"rev":"5069e0896","thumb_exists":true,"bytes":13349,"modified":"Tue, 20 Mar 2012 05:06:43 +0000","client_mtime":"Mon, 26 Sep 2011 11:51:26 +0000","path":"\/3_sml.jpg","is_dir":false,"icon":"page_white_picture","root":"dropbox","mime_type":"image\/jpeg","size":"13 KB"},{"revision":4,"rev":"4069e0896","thumb_exists":true,"bytes":8838,"modified":"Tue, 20 Mar 2012 05:06:43 +0000","client_mtime":"Mon, 26 Sep 2011 11:51:46 +0000","path":"\/4_sml.jpg","is_dir":false,"icon":"page_white_picture","root":"dropbox","mime_type":"image\/jpeg","size":"8.6 KB"},{"revision":3,"rev":"3069e0896","thumb_exists":true,"bytes":99646,"modified":"Tue, 20 Mar 2012 04:57:58 +0000","client_mtime":"Tue, 20 Sep 2011 14:14:26 +0000","path":"\/bg.jpg","is_dir":false,"icon":"page_white_picture","root":"dropbox","mime_type":"image\/jpeg","size":"97.3 KB"}],"icon":"folder"}

My problem is that i would like get the output for each the image/file name only.. But i can find the right way to do it.. i through i could do it this way:

$info = json_encode($dropbox->getMetaData(''));
foreach($info->contents->path as $file){
    echo $file;
}

But i get this error:

Warning: Invalid argument supplied for foreach() in /home/djrasmusp/rasmusp.com/db/index.php on line 16

But is there anyone that can give me a helping hand with my problem?

Rasmus Pedersen
  • 176
  • 1
  • 13

3 Answers3

2

Try json_decode instead of json_encode (and maybe use second parameter to produce associated array instead of stdClass).

jmattheis
  • 10,494
  • 11
  • 46
  • 58
Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
  • then i get this error message: Warning: json_decode() expects parameter 1 to be string, array given in /home/djrasmusp/rasmusp.com/db/index.php on line 15 Warning: Invalid argument supplied for foreach() in /home/djrasmusp/rasmusp.com/db/index.php on line 16 – Rasmus Pedersen Mar 20 '12 at 23:10
  • var_dump($dropbox->getMetaData(''));? – Peter Kiss Mar 21 '12 at 07:21
  • found the problem i hade to write: foreach($info['contents'] as $a){} instead of foreach($info->contents as $a) – Rasmus Pedersen Mar 21 '12 at 16:13
1

I had the same problem, here is my solution:

$metaData = $dropbox->metaData($path);

foreach($metaData['body']->contents as $file){
    $f = str_replace("/", "", $file->path);
    echo "<li>$f</li>";
}
Dion
  • 3,145
  • 20
  • 37
0

tested by me :)

require_once('bootstrap.php');
$metaData = $dropbox->metaData();
foreach($metaData['body']->contents as $file)
{ 
   echo "<pre>";
   echo $file->path; 
}
hirosht
  • 932
  • 2
  • 17
  • 31
Cake PHP
  • 163
  • 5
  • 12