PHP ON GOOGLE CLOUD PLATFORM

Quickstart View Documentation

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

  • Build scalable web apps and APIs
  • Access fully-managed MySQL, NoSQL, or PostgreSQL databases
  • Use Google’s machine learning APIs with native library support
  • Run containerized applications at scale
  • 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
$ composer require google/cloud-storage
2
Set up a Google Cloud Platform project
  1. In the GCP Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  2. Make sure that billing is enabled for your project.

    Learn how to 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
$ composer require google/cloud-bigquery
2
Set up a Google Cloud Platform project
  1. In the GCP Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  2. Make sure that billing is enabled for your project.

    Learn how to enable billing

  3. Enable the BigQuery API.

    Enable the API

3
Write your code
require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\BigQuery\BigQueryClient;


// get the project ID as the first argument
if (2 != count($argv)) {
    die("Usage: php stackoverflow.php YOUR_PROJECT_ID\n");
}

$projectId = $argv[1];

$bigQuery = new BigQueryClient([
    'projectId' => $projectId,
]);
$query = <<<ENDSQL
SELECT
  CONCAT(
    'https://stackoverflow.com/questions/',
    CAST(id as STRING)) as url,
  view_count
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE tags like '%google-bigquery%'
ORDER BY view_count DESC
LIMIT 10;
ENDSQL;
$queryJobConfig = $bigQuery->query($query);
$queryResults = $bigQuery->runQuery($queryJobConfig);

if ($queryResults->isComplete()) {
    $i = 0;
    $rows = $queryResults->rows();
    foreach ($rows as $row) {
        printf('--- Row %s ---' . PHP_EOL, ++$i);
        printf('url: %s, %s views' . PHP_EOL, $row['url'], $row['view_count']);
    }
    printf('Found %s row(s)' . PHP_EOL, $i);
} else {
    throw new Exception('The query failed to complete');
}
Write log data to Stackdriver Logging
1
Install
$ composer require google/cloud-logging
2
Set up a Google Cloud Platform project
  1. In the GCP Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  2. Make sure that billing is enabled for your project.

    Learn how to 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
$ composer require google/cloud-language
2
Set up a Google Cloud Platform project
  1. In the GCP Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  2. Make sure that billing is enabled for your project.

    Learn how to 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.