欢迎光临
我们一直在努力

使用 PHP 构建 GCP 云应用的全面教程

本教程指导使用 php 构建和部署 gcp 云应用,涵盖环境设置、使用 cloud functions、cloud storage、cloud sql 以及实战案例:创建留言板应用。环境设置: 安装 php、composer,创建 gcp 项目,启用 cloud sdk。cloud functions: 创建无服务器代码处理程序,并将其部署为 http 触发器。cloud storage: 创建存储桶,上传文件。cloud sql: 创建托管式数据库实例,并建立连接。实战案例:留言板应用 使用 cloud functions、cloud storage 和 cloud sql 创建一个简单的留言板应用程序。

使用 PHP 构建 GCP 云应用的全面教程

使用 PHP 构建 GCP 云应用的全面教程

简介

Google Cloud Platform (GCP) 是一套强大的云服务,提供各种工具和基础设施来创建和部署可扩展、高可用和安全的应用程序。本教程旨在指导您使用 PHP 构建和部署 GCP 云应用。

环境设置

  • 安装 PHP
  • 安装 Composer
  • 设置 GCP 项目并启用 Cloud SDK

创建应用

  1. 使用 Composer 安装 Google Cloud Client Libraries:
composer require google/cloud
  1. 创建项目根目录:
mkdir my-app
cd my-app
  1. 初始化 Git 存储库:
<a style='color:#f60; text-decoration:underline;' href="https://www.codesou.cn/" target="_blank">git</a> init
git add .
git commit -m "Initial commit"

使用 Cloud Functions

Cloud Functions 是无服务器计算服务,允许您执行事件触发的代码。

  1. 创建一个 Cloud Function handler:
<?php
use PsrHttpMessageServerRequestInterface;
use PsrHttpMessageResponseInterface;

function helloWorld(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
    $body = "Hello World!";

    $response->getBody()->write($body);
    return $response
        ->withStatus(200)
        ->withHeader('Content-Type', 'text/plain');
}
  1. 部署 Cloud Function:
gcloud functions deploy hello-world 
--runtime <a style='color:#f60; text-decoration:underline;' href="https://www.codesou.cn/" target="_blank">php7</a>4 
--entry-point helloWorld 
--trigger-http

使用 Cloud Storage

Cloud Storage 是对象存储服务,用于存储和管理不可变数据。

  1. 创建一个 Cloud Storage Bucket:
use GoogleCloudStorageStorageClient;
use GoogleCloudStorageBucket;

$storage = new StorageClient();
$storage->createBucket('my-bucket');
  1. 上传一个文件到 Cloud Storage:
$file = fopen('myfile.txt', 'r');
$storage->upload('my-bucket', 'myfile.txt', $file);

使用 Cloud SQL

Cloud SQL 是托管式数据库服务,可让您轻松设置、管理和扩展 MySQL、PostgreSQL 和 SQL Server 数据库。

  1. 创建一个 Cloud SQL 实例:
use GoogleCloudSqlAdminV1DatabaseAdminClient;
use GoogleCloudSqlAdminV1Instance;
use GoogleCloudSqlAdminV1InstanceId;

$admin = new DatabaseAdminClient();

$instanceId = new InstanceId(
    $projectId,
    $instanceRegion,
    $instanceName
);

$instance = new Instance();

$admin->createInstance($instanceId, $instance);
  1. 连接到 Cloud SQL 实例:
use PDO;

$dsn = sprintf('<a style='color:#f60; text-decoration:underline;' href="https://www.codesou.cn/" target="_blank">mysql</a>:dbname=%s;host=%s;port=%s', $databaseName, $instanceHost, $port);
$conn = new PDO($dsn, $dbUser, $dbPwd);

实战案例:创建留言板应用

让我们创建一个简单的留言板应用程序,它将使用 Cloud Functions 接收请求、Cloud Storage 存储留言并使用 Cloud SQL 进行数据库操作。

创建 Cloud Function

<?php
use PsrHttpMessageServerRequestInterface;
use PsrHttpMessageResponseInterface;
use GoogleCloudSpannerSpannerClient;

function createMessage(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
    $payload = json_decode($request->getBody()->getContents(), true);
    $message = $payload['message'];

    // Connect to Cloud SQL
    $db = new SpannerClient([
        'projectId' => $projectId,
        'databaseId' => $databaseId,
    ]);

    $results = $db->execute('INSERT INTO Messages (message) VALUES (@message)', [
        'parameters' => ['message' => $message],
    ]);

    $body = json_encode($results->rows());

    $response->getBody()->write($body);
    return $response
        ->withStatus(200)
        ->withHeader('Content-Type', 'application/json');
}

部署 Cloud Function

gcloud functions deploy createMessage 
--runtime php74 
--entry-point createMessage 
--trigger-http

创建 Cloud SQL 实例

use GoogleCloudSqlAdminV1DatabaseAdminClient;
use GoogleCloudSqlAdminV1Instance;
use GoogleCloudSqlAdminV1InstanceId;

$admin = new DatabaseAdminClient();

$instanceId = new InstanceId(
    $projectId,
    $instanceRegion,
    $instanceName
);

$instance = new Instance();
$instance->getDatabaseName() = $databaseName;

$admin->createInstance($instanceId, $instance);

连接到 Cloud SQL 实例

use PDO;

$dsn = sprintf('mysql:dbname=%s;host=%s;port=%s', $databaseName, $instanceHost, $port);
$conn = new PDO($dsn, $dbUser, $dbPwd);

结论

本教程为您提供了一个使用 PHP 构建并部署可扩展、高可用和安全的 GCP 云应用的基础知识。通过遵循这些步骤和示例,您可以释放 GCP 的强大功能,并为您的应用程序增添价值。

大量免费API接口:立即学习
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

赞(0) 打赏
未经允许不得转载:码农资源网 » 使用 PHP 构建 GCP 云应用的全面教程
分享到

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册