0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP の Rest で Cloud Firestore のデータを作成 (Create)

Posted at
firestore_create_rest.php
# ! /usr/bin/php
<?php
//
//	firestore_create_rest.php
//
//					Feb/16/2020
//
// ------------------------------------------------------------------
include('Requests/library/Requests.php');
include("get_token.php");
//
// ------------------------------------------------------------------
function add_data_proc($url_base,$headers,$key,$name,$population,$date_mod)
{
$url = $url_base . "?documentId=" . $key;

$update01 = array ();
$update01["fields"] = array ();
$update01["fields"]["name"] = ["stringValue"=> $name];
$update01["fields"]["population"] = ["integerValue"=> $population];
$update01["fields"]["date_mod"] = [ "timestampValue"=> $date_mod];

$str_json = json_encode ($update01);
print($str_json . "\n");
$request = Requests::post($url, $headers, $str_json);
var_dump($request->status_code);

$rvalue = $request->body;

print($rvalue);
}

// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");
Requests::register_autoloader();
//

$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";


add_data_proc($url_base,$headers,'t0921','宇都宮',38921,"2010-10-5T00:15:00Z");
add_data_proc($url_base,$headers,'t0922','小山',45921,"2010-10-9T00:14:00Z");
add_data_proc($url_base,$headers,'t0923','佐野',76921,"2010-11-7T00:16:00Z");
add_data_proc($url_base,$headers,'t0924','足利',94921,"2010-12-15T00:12:00Z");
add_data_proc($url_base,$headers,'t0925','日光',27921,"2010-8-2T00:11:00Z");

fputs (STDERR,"*** 終了 ***\n");
// ------------------------------------------------------------------
?>
get_token.php
<?php
//
//	get_token.php
//
//					Feb/03/2020
//
// ------------------------------------------------------------------
function get_token_proc ()
{
	$token = '';
	$command = 'gcloud auth application-default print-access-token';

	try
		{
		exec($command, $out, $ret);
		$token = $out[0];
		}
	catch (Exception $ee)
		{
		print("*** error *** exec ***\n");
		print('Error:'.$ee->getMessage());
		}

	return	$token;
}

// ------------------------------------------------------------------
?>

実行コマンド

export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_create_rest.php
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?