Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@ekzemplaro

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

More than 1 year has passed since last update.
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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ekzemplaro
Python3,Node.js,Go,Nginx,RaspberryPi あたりがカバー範囲です。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?