firestore_update_rest.php
# ! /usr/bin/php
<?php
//
// firestore_update_rest.php
//
// Feb/16/2020
//
// ------------------------------------------------------------------
include('Requests/library/Requests.php');
include("get_token.php");
//
// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");
Requests::register_autoloader();
//
$key_in = $argv[1];
$population_in = intval ($argv[2]);
print($key_in . "\n");
print($population_in . "\n");
$token = get_token_proc();
//
$project = 'project-jan25-2020';
$headers = array('Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json');
$options = array();
$url_base = "https://firestore.googleapis.com/v1/projects/" . $project . "/databases/(default)/documents/cities";
$url = $url_base . "/" . $key_in;
$url .= "?updateMask.fieldPaths=population&updateMask.fieldPaths=date_mod";
date_default_timezone_set('Asia/Tokyo');
$today = date ("c");
print($today . "\n");
$update01 = array ();
$update01["fields"] = array ();
$update01["fields"]["population"] = ["integerValue"=> $population_in];
$update01["fields"]["date_mod"] = [ "timestampValue"=> $today];
$str_json = json_encode ($update01);
print($str_json . "\n");
$request = Requests::patch($url, $headers, $str_json);
var_dump($request->status_code);
$rvalue = $request->body;
print($rvalue);
fputs (STDERR,"*** 終了 ***\n");
// ------------------------------------------------------------------
?>
get_token.php はこちら
PHP の Rest で Cloud Firestore のデータを作成 (Create)
実行コマンド
export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_update_rest.pho t0923 91476200