PHPからDBX Platformを利用してDropbox内のファイル一覧を取得する方法
以前にPHPからDropboxにファイルをアップロードするという記事を書いたが、今回はそのアップロードしたファイル一覧のデータ(ファイル名や更新日時等)を取得したい。以下に実装方法をメモ。
目次
Dropbox側の準備
Dropbox側の準備については以下過去記事を参考に「アプリの作成~リフレッシュトークンの取得」まで済ませておくこと。
https://taitan916.info/blog/archives/4694#Dropbox
実装方法
ソースコード
<?php
const APP_KEY = '【App key】';
const APP_SECRET = '【App secret】';
const REFRESH_TOKEN = '【リフレッシュトークン】';
//一時的なアクセストークンを取得
function getAccessToken() {
$url = 'https://api.dropboxapi.com/oauth2/token';
$data = [
'grant_type' => 'refresh_token',
'refresh_token' => REFRESH_TOKEN,
'client_id' => APP_KEY,
'client_secret' => APP_SECRET
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$res = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$access_token = '';
if( !curl_errno($ch) && $http_code == "200" ){
$res = json_decode($res, true);
$access_token = $res['access_token'];
} else {
echo "ERROR: Failed to access Dropbox API : " . curl_error($ch) . "<br>";
}
curl_close($ch);
return $access_token;
}
//ファイル一覧を取得
function getFileList( $access_token ){
$url = 'https://api.dropboxapi.com/2/files/list_folder';
$headers = array(
'Authorization: Bearer ' . $access_token,
'Content-Type: application/json',
);
//ディレクトリを指定したい場合はこちらで設定する
$param = array(
"path" => "",
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($param),
CURLOPT_RETURNTRANSFER => true,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$res = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $res;
}
//一時的なアクセストークンを取得
$access_token = getAccessToken();
$file_list = getFileList($access_token);
var_dump($file_list);
「//ディレクトリを指定したい場合はこちらで設定する」の部分について、アプリのルートディレクトリの場合は空("path" => "")でOK。
今回の場合だと「/アプリ/php_connection_test」ディレクトリが対象になる。
出力例
上記ソースコードが正常に実行されるとvar_dumpで以下のように出力される筈(一部伏字)。
{
"entries": [
{
".tag": "file",
"name": "002.jpg",
"path_lower": "/002.jpg",
"path_display": "/002.jpg",
"id": "id:xxxxxxxxxxxxxx",
"client_modified": "2024-03-29T07:43:13Z",
"server_modified": "2024-03-29T07:43:13Z",
"rev": "xxxxxxxxxxxxxx",
"size": 128914,
"is_downloadable": true,
"content_hash": "xxxxxxxxxxxxxx"
},
{
".tag": "file",
"name": "001.jpg",
"path_lower": "/001.jpg",
"path_display": "/001.jpg",
"id": "id:xxxxxxxxxxxxxx",
"client_modified": "2024-03-29T07:56:26Z",
"server_modified": "2024-03-29T07:56:26Z",
"rev": "xxxxxxxxxxxxxx",
"size": 117227,
"is_downloadable": true,
"content_hash": "xxxxxxxxxxxxxx"
},
{
".tag": "file",
"name": "003.jpg",
"path_lower": "/003.jpg",
"path_display": "/003.jpg",
"id": "id:xxxxxxxxxxxxxx",
"client_modified": "2024-03-29T07:56:37Z",
"server_modified": "2024-03-29T07:56:37Z",
"rev": "xxxxxxxxxxxxxx",
"size": 120499,
"is_downloadable": true,
"content_hash": "xxxxxxxxxxxxxx"
}
],
"cursor": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"has_more": false
}
その他
タイムゾーンについて
client_modified / server_modifiedの日時が明らかに異なっていたため調べてみると、タイムゾーンは全てUTCになるとのこと。
client_modified / server_modifiedについて
フォーラムの以下引用が参考になりそう。
client_modified Timestamp(format="%Y-%m-%dT%H:%M:%SZ") ファイルの場合、これはファイルが Dropbox に追加されたときにデスクトップ クライアントによって設定された変更時刻です。 この時間は検証されていないため (Dropbox サーバーはデスクトップ クライアントが送信したものをすべて保存します)、これは表示目的 (並べ替えなど) にのみ使用する必要があり、ファイルが変更されたかどうかを判断するためなどには使用しないでください。
server_modified Timestamp(format="%Y-%m-%dT%H:%M:%SZ") Dropbox 上でファイルが最後に変更された時刻。
基本的にはserver_modifiedを使う形になると思われる。
参考サイト
関連記事
-
-
PHPからDBX Platformを利用してサーバ内のファイルをアップロードする方法
PHPで何らかのファイルを保存するようなケースだと今まではローカルに保存する、も ...
-
-
PHPからDBX Platformを利用してDropbox内にディレクトリ(フォルダ)を作成する方法
PHPからDBX Platformを利用してDropbox内にディレクトリ(フォ ...
-
-
PHPからDBX Platformを利用してDropbox内のファイルを削除する方法
以前にPHPからDropboxのファイル一覧のデータ(ファイル名や更新日時等)を ...