PHP ON GOOGLE CLOUD PLATFORM

Quickstart View Documentation

PHP tools to build cloud-native apps, deploy at scale, and analyze data

  • check Build scalable web apps and APIs
  • check Access fully-managed MySQL, NoSQL, or PostgreSQL databases
  • check Use Google’s machine learning APIs with native library support
  • check Run containerized applications at scale
  • check Deploy your favorite framework or CMS
lovingly-crafted composer packages

Take advantage of our APIs and services using the Google Cloud Composer packages and test run our code samples.

Upload Files to Google Cloud Storage
Perform data analytics using Google BigQuery
Write log data to Stackdriver Logging
Analyze text with the Cloud Natural Language API
Upload Files to Google Cloud Storage
1
Install
$ require google/cloud-storage
2
Set up a Google Cloud Platform project
  1. In the Cloud Platform Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  2. Enable billing for your project.

    Enable billing

  3. Enable the Cloud Storage API.

    Enable the API

3
Write your code
use Google\Cloud\Storage\StorageClient;

/**
 * Upload a file.
 *
 * @param string $bucketName the name of your Google Cloud bucket.
 * @param string $objectName the name of the object.
 * @param string $source the path to the file to upload.
 *
 * @return Psr\Http\Message\StreamInterface
 */
function upload_object($bucketName, $objectName, $source)
{
    $storage = new StorageClient();
    $file = fopen($source, 'r');
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->upload($file, [
        'name' => $objectName
    ]);
    printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
}
Perform data analytics using Google BigQuery
1
Install
$ require google/cloud-bigquery
2
Set up a Google Cloud Platform project
  1. In the Cloud Platform Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  2. Enable billing for your project.

    Enable billing

  3. Enable the BigQuery API.

    Enable the API

3
Write your code
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\BigQuery\BigQueryClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$bigquery = new BigQueryClient([
    'projectId' => $projectId
]);

# The name for the new dataset
$datasetName = 'my_new_dataset';

# Creates the new dataset
$dataset = $bigquery->createDataset($datasetName);

echo 'Dataset ' . $dataset->id() . ' created.';
Write log data to Stackdriver Logging
1
Install
$ require google/cloud-logging
2
Set up a Google Cloud Platform project
  1. In the Cloud Platform Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  2. Enable billing for your project.

    Enable billing

  3. Enable the Stackdriver Logging API.

    Enable the API

3
Write your code
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\Logging\LoggingClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$logging = new LoggingClient([
    'projectId' => $projectId
]);

# The name of the log to write to
$logName = 'my-log';

# Selects the log to write to
$logger = $logging->logger($logName);

# The data to log
$text = 'Hello, world!';

# Creates the log entry
$entry = $logger->entry($text);

# Writes the log entry
$logger->write($entry);

echo 'Logged ' . $text;
Analyze text with the Cloud Natural Language API
1
Install
$ require google/cloud-language
2
Set up a Google Cloud Platform project
  1. In the Cloud Platform Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  2. Enable billing for your project.

    Enable billing

  3. Enable the Cloud Natural Language API.

    Enable the API

3
Write your code
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\Language\LanguageClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$language = new LanguageClient([
    'projectId' => $projectId
]);

# The text to analyze
$text = 'Hello, world!';

# Detects the sentiment of the text
$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();

echo 'Text: ' . $text . '
Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
PHP QUICK STARTS
Quickly find and Debug issues

Google Stackdriver provides powerful monitoring, logging, and diagnostics. It equips you with insight into the health, performance, and availability of cloud-powered applications, enabling you to find and fix issues faster.

Google Stackdriver
Unified monitoring, logging, and diagnostics for applications on Google Cloud Platform and AWS.
Stackdriver Error Reporting
A walk through of getting an error alert and investigating the error in the Google Cloud Console.
Stackdriver Monitor, diagnose, and fix
In this video, Aja Hammerly uses Stackdriver to find and fix some subtle errors in an example app, and you'll learn how to use Stackdriver on your own projects.
Learn more
PHP ON GCP COMMUNITIES

Got something to say? Join our community to ask questions, or just chat with the experts at Google who help build PHP for Google Cloud Platform.