firestore_read_rest.php
# ! /usr/bin/php
<?php
//
// firestore_read_rest.php
//
// Feb/03/2020
//
// ------------------------------------------------------------------
include('Requests/library/Requests.php');
include("get_token.php");
//
// ------------------------------------------------------------------
// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");
Requests::register_autoloader();
//
$token = get_token_proc();
// print( $token );
//
$project = 'project-jan25-2020';
$headers = array('Authorization' => 'Bearer ' . $token);
$options = array();
$url = "https://firestore.googleapis.com/v1/projects/" . $project . "/databases/(default)/documents/cities";
$request = Requests::get($url, $headers);
var_dump($request->status_code);
// var_dump($request->body);
$json_string = $request->body;
$dict_aa = json_decode ($json_string,true);
// var_dump($dict_aa["documents"]);
$array_aa = $dict_aa["documents"];
foreach ($array_aa as $unit_aa)
{
$str_aa = $unit_aa["name"];
$tt = explode("/",$str_aa);
$length = count($tt);
$key = $tt[$length - 1];
$name = $unit_aa["fields"]["name"]["stringValue"];
$population = $unit_aa["fields"]["population"]["integerValue"];
$date_mod = $unit_aa["fields"]["date_mod"]["timestampValue"];
//
$str_out = $key . "\t" . $name . "\t" . $population . "\t" . $date_mod;
print($str_out . "\n");
}
fputs (STDERR,"*** 終了 ***\n");
// ------------------------------------------------------------------
?>
get_token.php はこちら
PHP の Rest で Cloud Firestore のデータを作成 (Create)
実行コマンド
export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_read_rest.php