Skip to content

Navigation Menu

Sign in
Appearance settings
    • GitHub Copilot
      Write better code with AI
    • GitHub Models New
      Manage and compare prompts
    • GitHub Advanced Security
      Find and fix vulnerabilities
    • Actions
      Automate any workflow
    • Codespaces
      Instant dev environments
    • Issues
      Plan and track work
    • Code Review
      Manage code changes
    • Discussions
      Collaborate outside of code
    • Code Search
      Find more, search less
    Explore
    • Why GitHub
    • All features
    • Documentation
    • GitHub Skills
    • Blog
  • By company size
    • Enterprises
    • Small and medium teams
    • Startups
    • Nonprofits
    By use case
    • DevSecOps
    • DevOps
    • CI/CD
    • View all use cases
    By industry
    • Healthcare
    • Financial services
    • Manufacturing
    • Government
    • View all industries
    View all solutions
  • Topics
    • AI
    • DevOps
    • Security
    • Software Development
    • View all
    Explore
    • Learning Pathways
    • Events & Webinars
    • Ebooks & Whitepapers
    • Customer Stories
    • Partners
    • Executive Insights
    • GitHub Sponsors
      Fund open source developers
    • The ReadME Project
      GitHub community articles
    Repositories
    • Topics
    • Trending
    • Collections
    • Enterprise platform
      AI-powered developer platform
    Available add-ons
    • GitHub Advanced Security
      Enterprise-grade security features
    • Copilot for business
      Enterprise-grade AI features
    • Premium Support
      Enterprise-grade 24/7 support
  • Pricing

Search code, repositories, users, issues, pull requests...

Clear
    Search syntax tips

    Provide feedback

    We read every piece of feedback, and take your input very seriously.

    Saved searches

    Use saved searches to filter your results more quickly

    To see all available qualifiers, see our documentation.

    Sign in
    Sign up
    Appearance settings
    You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert
    jins-coder / php-docs-samples Public
    forked from GoogleCloudPlatform/php-docs-samples
    • Notifications You must be signed in to change notification settings
    • Fork 0
    • Star 0
    • Code
    • Pull requests 0
    • Actions
    • Projects 0
    • Security

      Uh oh!

      There was an error while loading. Please reload this page.

    • Insights
    Additional navigation options
    • Code
    • Pull requests
    • Actions
    • Projects
    • Security
    • Insights

    Commit 22dc3a3

    Browse filesBrowse files
    bshafferbshaffer
    authored
    feat(compute): add cloud-client compute sample (GoogleCloudPlatform#1305)
    1 parent 91d2d90 commit 22dc3a3
    Copy full SHA for 22dc3a3

    File tree

    Expand file treeCollapse file tree

    7 files changed

    +251
    -39
    lines changed
    Filter options
    • compute/helloworld
      • README.md
      • api-client
        • README.md
        • app.php
        • composer.json
      • cloud-client
        • README.md
        • app.php
        • composer.json
    Expand file treeCollapse file tree

    7 files changed

    +251
    -39
    lines changed

    ‎compute/helloworld/README.md

    Copy file name to clipboardExpand all lines: compute/helloworld/README.md
    +4-39Lines changed: 4 additions & 39 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2,44 +2,9 @@
    22

    33
    ## Description
    44
    This is a simple web-based example of calling the Google Compute Engine API
    5-
    in PHP.
    5+
    in PHP. These samples include calling the API with both the
    6+
    [Google API client](api-client) and [Google Cloud Client](cloud-client) (alpha).
    67

    7-
    ## Prerequisites
    8-
    Please make sure that all of the following is installed before trying to run
    9-
    the sample application.
    108

    11-
    - [PHP 5.2.x or higher](http://www.php.net/)
    12-
    - [PHP Curl extension](http://www.php.net/manual/en/intro.curl.php)
    13-
    - [PHP JSON extension](http://php.net/manual/en/book.json.php)
    14-
    - The [`google-api-php-client`](https://github.com/google/google-api-php-client)
    15-
    library checked out locally
    16-
    17-
    ## Setup Authentication
    18-
    NOTE: This README assumes that you have enabled access to the Google Compute
    19-
    Engine API via the Google API Console page.
    20-
    21-
    1) Visit https://code.google.com/apis/console/?api=compute to register your
    22-
    application.
    23-
    - Click on "API Access" in the left column
    24-
    - Click the button labeled "Create an OAuth2 client ID..." if you have not
    25-
    generated any client IDs, or "Create another client ID..." if you have
    26-
    - Give your application a name and click "Next"
    27-
    - Select "Web Application" as the "Application type"
    28-
    - Click "Create client ID"
    29-
    - Click "Edit settings..." for your new client ID
    30-
    - Under the redirect URI, enter the location of your application
    31-
    - Click "Update"
    32-
    - Click on "Overview" in the left column and note the Project ID
    33-
    34-
    2) Update app.php with the redirect uri, consumer key, secret, and Project ID
    35-
    obtained in step 1.
    36-
    - Update `YOUR_CLIENT_ID` with your oauth2 client id.
    37-
    - Update `YOUR_CLIENT_SECRET` with your oauth2 client secret.
    38-
    - Update `YOUR_REDIRECT_URI` with the fully qualified
    39-
    redirect URI.
    40-
    - Update `YOUR_GOOGLE_COMPUTE_ENGINE_PROJECT` with your Project ID from the
    41-
    API Console.
    42-
    43-
    ## Running the Sample Application
    44-
    3) Load app.php on your web server, and visit the appropriate website in
    45-
    your web browser.
    9+
    * [Google Cloud Client](cloud-client) (**Recommended**): Under active development, currently in alpha.
    10+
    * [Google API client](api-client): Stable and generally available, but no longer under active development

    ‎compute/helloworld/api-client/README.md

    Copy file name to clipboard
    +45Lines changed: 45 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,45 @@
    1+
    # Google Compute Engine PHP Sample Application
    2+
    3+
    ## Description
    4+
    This is a simple web-based example of calling the Google Compute Engine API
    5+
    in PHP.
    6+
    7+
    ## Prerequisites
    8+
    Please make sure that all of the following is installed before trying to run
    9+
    the sample application.
    10+
    11+
    - [PHP 5.2.x or higher](http://www.php.net/)
    12+
    - [PHP Curl extension](http://www.php.net/manual/en/intro.curl.php)
    13+
    - [PHP JSON extension](http://php.net/manual/en/book.json.php)
    14+
    - The [`google-api-php-client`](https://github.com/google/google-api-php-client)
    15+
    library checked out locally
    16+
    17+
    ## Setup Authentication
    18+
    NOTE: This README assumes that you have enabled access to the Google Compute
    19+
    Engine API via the Google API Console page.
    20+
    21+
    1) Visit https://code.google.com/apis/console/?api=compute to register your
    22+
    application.
    23+
    - Click on "API Access" in the left column
    24+
    - Click the button labeled "Create an OAuth2 client ID..." if you have not
    25+
    generated any client IDs, or "Create another client ID..." if you have
    26+
    - Give your application a name and click "Next"
    27+
    - Select "Web Application" as the "Application type"
    28+
    - Click "Create client ID"
    29+
    - Click "Edit settings..." for your new client ID
    30+
    - Under the redirect URI, enter the location of your application
    31+
    - Click "Update"
    32+
    - Click on "Overview" in the left column and note the Project ID
    33+
    34+
    2) Update app.php with the redirect uri, consumer key, secret, and Project ID
    35+
    obtained in step 1.
    36+
    - Update `YOUR_CLIENT_ID` with your oauth2 client id.
    37+
    - Update `YOUR_CLIENT_SECRET` with your oauth2 client secret.
    38+
    - Update `YOUR_REDIRECT_URI` with the fully qualified
    39+
    redirect URI.
    40+
    - Update `YOUR_GOOGLE_COMPUTE_ENGINE_PROJECT` with your Project ID from the
    41+
    API Console.
    42+
    43+
    ## Running the Sample Application
    44+
    3) Load app.php on your web server, and visit the appropriate website in
    45+
    your web browser.

    ‎compute/helloworld/app.php renamed to ‎compute/helloworld/api-client/app.php

    Copy file name to clipboard
    File renamed without changes.

    ‎compute/helloworld/api-client/composer.json

    Copy file name to clipboard
    +13Lines changed: 13 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,13 @@
    1+
    {
    2+
    "require": {
    3+
    "google/apiclient": "^2.9"
    4+
    },
    5+
    "scripts": {
    6+
    "pre-autoload-dump": "Google\\Task\\Composer::cleanup"
    7+
    },
    8+
    "extra": {
    9+
    "google/apiclient-services": [
    10+
    "Compute"
    11+
    ]
    12+
    }
    13+
    }

    ‎compute/helloworld/cloud-client/README.md

    Copy file name to clipboard
    +22Lines changed: 22 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,22 @@
    1+
    # Google Compute Engine PHP Sample Application
    2+
    3+
    ## Description
    4+
    5+
    This is a simple web-based example of calling the Google Compute Engine API
    6+
    in PHP.
    7+
    8+
    ## Prerequisites
    9+
    10+
    * Run `composer install` in this directory to install the `google/cloud-compute`
    11+
    library.
    12+
    * Follow [Getting started with authentication](https://cloud.google.com/docs/authentication/getting-started) to authenticate with Service Account credentials.
    13+
    14+
    ## Running the Sample Application
    15+
    16+
    Run app.php on your web server:
    17+
    18+
    ```
    19+
    php -S localhost:8080
    20+
    ```
    21+
    22+
    Visit the website (http://localhost:8080/app.php) in your web browser.

    ‎compute/helloworld/cloud-client/app.php

    Copy file name to clipboard
    +162Lines changed: 162 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,162 @@
    1+
    2+
    /*
    3+
    * Copyright 2021 Google Inc.
    4+
    *
    5+
    * Licensed under the Apache License, Version 2.0 (the "License");
    6+
    * you may not use this file except in compliance with the License.
    7+
    * You may obtain a copy of the License at
    8+
    *
    9+
    * http://www.apache.org/licenses/LICENSE-2.0
    10+
    *
    11+
    * Unless required by applicable law or agreed to in writing, software
    12+
    * distributed under the License is distributed on an "AS IS" BASIS,
    13+
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14+
    * See the License for the specific language governing permissions and
    15+
    * limitations under the License.
    16+
    */
    17+
    18+
    require_once 'vendor/autoload.php';
    19+
    20+
    use Google\Cloud\Compute\V1\InstancesClient;
    21+
    use Google\Cloud\Compute\V1\ZonesClient;
    22+
    use Google\Cloud\Compute\V1\MachineTypesClient;
    23+
    use Google\Cloud\Compute\V1\ImagesClient;
    24+
    use Google\Cloud\Compute\V1\FirewallsClient;
    25+
    use Google\Cloud\Compute\V1\NetworksClient;
    26+
    use Google\Cloud\Compute\V1\DisksClient;
    27+
    use Google\Cloud\Compute\V1\GlobalOperationsClient;
    28+
    use Google\Protobuf\Internal\Message;
    29+
    30+
    /**
    31+
    * Set these variables to your project and zone.
    32+
    */
    33+
    $projectId = 'php-docs-samples-kokoro';
    34+
    $zoneName = 'us-central1-f';
    35+
    36+
    // Instantiate clients for calling the Compute API.
    37+
    $instancesClient = new InstancesClient();
    38+
    $zonesClient = new ZonesClient();
    39+
    $disksClient = new DisksClient();
    40+
    $machineTypesClient = new MachineTypesClient();
    41+
    $imagesClient = new ImagesClient();
    42+
    $firewallsClient = new FirewallsClient();
    43+
    $networksClient = new NetworksClient();
    44+
    $globalOperationsClient = new GlobalOperationsClient();
    45+
    46+
    /**
    47+
    * Helper function to pretty-print a Protobuf message.
    48+
    */
    49+
    function print_message(Message $message)
    50+
    {
    51+
    return json_encode(
    52+
    json_decode($message->serializeToJsonString(), true),
    53+
    JSON_PRETTY_PRINT
    54+
    );
    55+
    }
    56+
    ?>
    57+
    58+
    59+
    60+
    61+

    Google Cloud Compute Sample App

    62+
    63+

    List Instances

    64+
    65+
    foreach ($instancesClient->list_($projectId, $zoneName) as $instance): ?>
    66+
     print_message($instance) ?>
    67+
    endforeach ?>
    68+
    69+
    70+

    List Zones

    71+
    72+
    foreach ($zonesClient->list_($projectId) as $zone): ?>
    73+
     print_message($zone) ?>
    74+
    endforeach ?>
    75+
    76+
    77+

    List Disks

    78+
    79+
    foreach ($disksClient->list_($projectId, $zoneName) as $disk): ?>
    80+
     print_message($disk) ?>
    81+
    endforeach ?>
    82+
    83+
    84+

    List Machine Types

    85+
    86+
    foreach ($machineTypesClient->list_($projectId, $zoneName) as $machineType): ?>
    87+
     print_message($machineType) ?>
    88+
    endforeach ?>
    89+
    90+
    91+

    List Images

    92+
    93+
    foreach ($imagesClient->list_($projectId) as $image): ?>
    94+
     print_message($image) ?>
    95+
    endforeach ?>
    96+
    97+
    98+

    List Firewalls

    99+
    100+
    foreach ($firewalls = $firewallsClient->list_($projectId) as $firewall): ?>
    101+
     print_message($firewall) ?>
    102+
    endforeach ?>
    103+
    104+
    105+

    List Networks

    106+
    107+
    foreach ($networksClient->list_($projectId) as $network): ?>
    108+
     print_message($network) ?>
    109+
    endforeach ?>
    110+
    111+
    112+

    List Operations

    113+
    114+
    foreach ($globalOperationsClient->list_($projectId) as $operation): ?>
    115+
     print_message($operation) ?>
    116+
    endforeach ?>
    117+
    118+
    119+
    120+
    121+
    122+
    146+
    147+

    ‎compute/helloworld/cloud-client/composer.json

    Copy file name to clipboard
    +5Lines changed: 5 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,5 @@
    1+
    {
    2+
    "require": {
    3+
    "google/cloud-compute": "^0.1.0"
    4+
    }
    5+
    }

    0 commit comments

    Comments
    0 (0)

    Footer

    © 2025 GitHub, Inc.

    Footer navigation

    • Terms
    • Privacy
    • Security
    • Status
    • Docs
    • Contact
    You can’t perform that action at this time.