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
    php-func / 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 5612cec

    Browse filesBrowse files
    ziyadmziyadm
    committed
    Initial checkin of Google Compute Engine PHP Application
    0 parents  commit 5612cec
    Copy full SHA for 5612cec

    File tree

    Expand file treeCollapse file tree

    2 files changed

    +382
    -0
    lines changed
    Filter options
    • README.md
    • app.php
    Expand file treeCollapse file tree

    2 files changed

    +382
    -0
    lines changed

    ‎README.md

    Copy file name to clipboard
    +44Lines changed: 44 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,44 @@
    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 library checked out locally
    15+
    16+
    ## Setup Authentication
    17+
    NOTE: This README assumes that you have enabled access to the Google Compute
    18+
    Engine API via the Google API Console page.
    19+
    20+
    1) Visit https://code.google.com/apis/console/?api=compute to register your
    21+
    application.
    22+
    - Click on "API Access" in the left column
    23+
    - Click the button labeled "Create an OAuth2 client ID..." if you have not
    24+
    generated any client IDs, or "Create another client ID..." if you have
    25+
    - Give your application a name and click "Next"
    26+
    - Select "Web Application" as the "Application type"
    27+
    - Click "Create client ID"
    28+
    - Click "Edit settings..." for your new client ID
    29+
    - Under the redirect URI, enter the location of your JavaScript application
    30+
    - Click "Update"
    31+
    32+
    2) Update app.php with the redirect uri, consumer key, secret, and developer
    33+
    key obtained in step 1.
    34+
    - Update 'YOUR_CLIENT_ID' with your oauth2 client id.
    35+
    - Update 'YOUR_CLIENT_SECRET' with your oauth2 client secret.
    36+
    - Update 'YOUR_REDIRECT_URI' with the fully qualified
    37+
    redirect URI.
    38+
    - Update 'YOUR_DEVELOPER_KEY' with your developer key.
    39+
    This is listed under "Simple API Access" at the very bottom of the page,
    40+
    in the Google API Console.
    41+
    42+
    ## Running the Sample Application
    43+
    3) Load app.php on your web server, and visit the appropriate website in
    44+
    your web browser.

    ‎app.php

    Copy file name to clipboard
    +338Lines changed: 338 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,338 @@
    1+
    2+
    /*
    3+
    * Copyright 2012 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+
    /**
    19+
    * Follow the instructions on https://code.google/com/p/google-api-php-client
    20+
    * to download, extract and include the Google APIs client library for PHP into
    21+
    * your project.
    22+
    */
    23+
    require_once 'google-api-php-client/src/Google_Client.php';
    24+
    require_once 'google-api-php-client/src/contrib/Google_ComputeService.php';
    25+
    26+
    session_start();
    27+
    28+
    /**
    29+
    * Visit https://code.google.com/apis/console to generate your
    30+
    * oauth2_client_id, oauth2_client_secret, and to register your
    31+
    * oauth2_redirect_uri.
    32+
    */
    33+
    $client = new Google_Client();
    34+
    $client->setApplicationName("Google Compute Engine PHP Starter Application");
    35+
    $client->setClientId('YOUR_CLIENT_ID');
    36+
    $client->setClientSecret('YOUR_CLIENT_SECRET');
    37+
    $client->setRedirectUri('YOUR_REDIRECT_URI');
    38+
    $client->setDeveloperKey('YOUR_DEVELOPER_KEY');
    39+
    $computeService = new Google_ComputeService($client);
    40+
    41+
    /**
    42+
    * The name of your Google Compute Engine Project.
    43+
    */
    44+
    $project = 'YOUR_GOOGLE_COMPUTE_ENGINE_PROJECT';
    45+
    46+
    /**
    47+
    * Constants for sample request parameters.
    48+
    */
    49+
    define('API_VERSION', 'v1beta13');
    50+
    define('BASE_URL', 'https://www.googleapis.com/compute/'.
    51+
    API_VERSION . '/projects/');
    52+
    define('GOOGLE_PROJECT', 'google');
    53+
    define('DEFAULT_PROJECT', $project);
    54+
    define('DEFAULT_NAME', 'new-node');
    55+
    define('DEFAULT_NAME_WITH_METADATA', 'new-node-with-metadata');
    56+
    define('DEFAULT_MACHINE_TYPE', BASE_URL . DEFAULT_PROJECT .
    57+
    '/machineTypes/n1-standard-1');
    58+
    define('DEFAULT_ZONE', BASE_URL . DEFAULT_PROJECT . '/zones/us-central1-a');
    59+
    define('DEFAULT_IMAGE', BASE_URL . GOOGLE_PROJECT .
    60+
    '/images/ubuntu-12-04-v20120912');
    61+
    define('DEFAULT_NETWORK', BASE_URL . DEFAULT_PROJECT .
    62+
    '/networks/default');
    63+
    64+
    /**
    65+
    * Generates the markup for a specific Google Compute Engine API request.
    66+
    * @param string $apiRequestName The name of the API request to process.
    67+
    * @param string $apiResponse The API response to process.
    68+
    * @return string Markup for the specific Google Compute Engine API request.
    69+
    */
    70+
    function generateMarkup($apiRequestName, $apiResponse) {
    71+
    $apiRequestMarkup = '';
    72+
    $apiRequestMarkup .= "

    " . $apiRequestName . "

    "
    ;
    73+
    74+
    if ($apiResponse['items'] == '' ) {
    75+
    $apiRequestMarkup .= "
    ";
    76+
    $apiRequestMarkup .= print_r(json_decode(json_encode($apiResponse), true), true);
    77+
    $apiRequestMarkup .= "";
    78+
    } else {
    79+
    foreach($apiResponse['items'] as $response) {
    80+
    $apiRequestMarkup .= "
    ";
    81+
    $apiRequestMarkup .= print_r(json_decode(json_encode($response), true), true);
    82+
    $apiRequestMarkup .= "";
    83+
    }
    84+
    }
    85+
    86+
    return $apiRequestMarkup;
    87+
    }
    88+
    89+
    /**
    90+
    * Clear access token whenever a logout is requested.
    91+
    */
    92+
    if (isset($_REQUEST['logout'])) {
    93+
    unset($_SESSION['access_token']);
    94+
    }
    95+
    96+
    /**
    97+
    * Authenticate and set client access token.
    98+
    */
    99+
    if (isset($_GET['code'])) {
    100+
    $client->authenticate($_GET['code']);
    101+
    $_SESSION['access_token'] = $client->getAccessToken();
    102+
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    103+
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    104+
    }
    105+
    106+
    /**
    107+
    * Set client access token.
    108+
    */
    109+
    if (isset($_SESSION['access_token'])) {
    110+
    $client->setAccessToken($_SESSION['access_token']);
    111+
    }
    112+
    113+
    /**
    114+
    * If all authentication has been successfully completed, make Google Compute
    115+
    * Engine API requests.
    116+
    */
    117+
    if ($client->getAccessToken()) {
    118+
    /**
    119+
    * Google Compute Engine API request to retrieve the list of instances in your
    120+
    * Google Compute Engine project.
    121+
    */
    122+
    $instances = $computeService->instances->listInstances(DEFAULT_PROJECT);
    123+
    $instancesListMarkup = generateMarkup('List Instances', $instances);
    124+
    125+
    /**
    126+
    * Google Compute Engine API request to retrieve the list of all data center
    127+
    * locations associated with your Google Compute Engine project.
    128+
    */
    129+
    $zones = $computeService->zones->listZones(DEFAULT_PROJECT);
    130+
    $zonesListMarkup = generateMarkup('List Zones', $zones);
    131+
    132+
    /**
    133+
    * Google Compute Engine API request to retrieve the list of all machine types
    134+
    * associated associated with your Google Compute Engine project.
    135+
    */
    136+
    $machineTypes = $computeService->machineTypes->listMachineTypes(DEFAULT_PROJECT);
    137+
    $machineTypesListMarkup = generateMarkup('List Machine Types',
    138+
    $machineTypes);
    139+
    140+
    /**
    141+
    * Google Compute Engine API request to retrieve the list of all image types
    142+
    * associated associated with your Google Compute Engine project.
    143+
    */
    144+
    $images = $computeService->images->listImages(GOOGLE_PROJECT);
    145+
    $imagesListMarkup = generateMarkup('List Images', $images);
    146+
    147+
    /**
    148+
    * Google Compute Engine API request to retrieve the list of all firewalls
    149+
    * associated associated with your Google Compute Engine project.
    150+
    */
    151+
    $firewalls = $computeService->firewalls->listFirewalls(DEFAULT_PROJECT);
    152+
    $firewallsListMarkup = generateMarkup('List Firewalls', $firewalls);
    153+
    154+
    /**
    155+
    * Google Compute Engine API request to retrieve the list of all networks
    156+
    * associated associated with your Google Compute Engine project.
    157+
    */
    158+
    $networks = $computeService->networks->listNetworks(DEFAULT_PROJECT);
    159+
    $networksListMarkup = generateMarkup('List Networks', $networks);;
    160+
    161+
    /**
    162+
    * Google Compute Engine API request to insert a new instance into your Google
    163+
    * Compute Engine project.
    164+
    */
    165+
    $name = DEFAULT_NAME;
    166+
    $machineType = DEFAULT_MACHINE_TYPE;
    167+
    $zone = DEFAULT_ZONE;
    168+
    $image = DEFAULT_IMAGE;
    169+
    170+
    $googleNetworkInterfaceObj = new Google_NetworkInterface();
    171+
    $network = DEFAULT_NETWORK;
    172+
    $googleNetworkInterfaceObj->setNetwork($network);
    173+
    174+
    $new_instance = new Google_Instance();
    175+
    $new_instance->setName($name);
    176+
    $new_instance->setImage($image);
    177+
    $new_instance->setZone($zone);
    178+
    $new_instance->setMachineType($machineType);
    179+
    $new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj));
    180+
    181+
    $insertInstance = $computeService->instances->insert(DEFAULT_PROJECT,
    182+
    $new_instance);
    183+
    $insertInstanceMarkup = generateMarkup('Insert Instance', $insertInstance);
    184+
    185+
    /**
    186+
    * Google Compute Engine API request to insert a new instance (with metadata)
    187+
    * into your Google Compute Engine project.
    188+
    */
    189+
    $name = DEFAULT_NAME_WITH_METADATA;
    190+
    $machineType = DEFAULT_MACHINE_TYPE;
    191+
    $zone = DEFAULT_ZONE;
    192+
    $image = DEFAULT_IMAGE;
    193+
    194+
    $googleNetworkInterfaceObj = new Google_NetworkInterface();
    195+
    $network = DEFAULT_NETWORK;
    196+
    $googleNetworkInterfaceObj->setNetwork($network);
    197+
    198+
    $metadataItemsObj = new Google_MetadataItems();
    199+
    $metadataItemsObj->setKey('startup-script');
    200+
    $metadataItemsObj->setValue('apt-get install apache2');
    201+
    202+
    $metadata = new Google_Metadata();
    203+
    $metadata->setItems(array($metadataItemsObj));
    204+
    205+
    $new_instance = new Google_Instance();
    206+
    $new_instance->setName($name);
    207+
    $new_instance->setImage($image);
    208+
    $new_instance->setZone($zone);
    209+
    $new_instance->setMachineType($machineType);
    210+
    $new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj));
    211+
    $new_instance->setMetadata($metadata);
    212+
    213+
    $insertInstanceWithMetadata = $computeService->instances->insert(
    214+
    DEFAULT_PROJECT, $new_instance);
    215+
    $insertInstanceWithMetadataMarkup = generateMarkup(
    216+
    'Insert Instance With Metadata', $insertInstanceWithMetadata);
    217+
    218+
    /**
    219+
    * Google Compute Engine API request to get an instance matching the outlined
    220+
    * parameters from your Google Compute Engine project.
    221+
    */
    222+
    $getInstance = $computeService->instances->get(DEFAULT_PROJECT, DEFAULT_NAME);
    223+
    $getInstanceMarkup = generateMarkup('Get Instance', $getInstance);
    224+
    225+
    /**
    226+
    * Google Compute Engine API request to get an instance matching the outlined
    227+
    * parameters from your Google Compute Engine project.
    228+
    */
    229+
    $getInstanceWithMetadata = $computeService->instances->get(DEFAULT_PROJECT,
    230+
    DEFAULT_NAME_WITH_METADATA);
    231+
    $getInstanceWithMetadataMarkup = generateMarkup('Get Instance With Metadata',
    232+
    $getInstanceWithMetadata);
    233+
    234+
    /**
    235+
    * Google Compute Engine API request to delete an instance matching the
    236+
    * outlined parameters from your Google Compute Engine project.
    237+
    */
    238+
    $deleteInstance = $computeService->instances->delete(DEFAULT_PROJECT, DEFAULT_NAME);
    239+
    $deleteInstanceMarkup = generateMarkup('Delete Instance', $deleteInstance);
    240+
    241+
    /**
    242+
    * Google Compute Engine API request to delete an instance matching the
    243+
    * outlined parameters from your Google Compute Engine project.
    244+
    */
    245+
    $deleteInstanceWithMetadata = $computeService->instances->delete(DEFAULT_PROJECT,
    246+
    DEFAULT_NAME_WITH_METADATA);
    247+
    $deleteInstanceWithMetadataMarkup = generateMarkup(
    248+
    'Delete Instance With Metadata', $deleteInstanceWithMetadata);
    249+
    250+
    /**
    251+
    * Google Compute Engine API request to retrieve the list of all operations
    252+
    * associated with your Google Compute Engine project.
    253+
    */
    254+
    $operations = $computeService->operations->listOperations(DEFAULT_PROJECT);
    255+
    $operationsListMarkup = generateMarkup('List Operations', $operations);
    256+
    257+
    // The access token may have been updated lazily.
    258+
    $_SESSION['access_token'] = $client->getAccessToken();
    259+
    } else {
    260+
    $authUrl = $client->createAuthUrl();
    261+
    }
    262+
    ?>
    263+
    264+
    265+
    266+
    267+
    268+
    269+

    Google Compute Engine Sample App

    270+
    271+
    if(isset($instancesListMarkup)): ?>
    272+
    print $instancesListMarkup ?>
    273+
    endif ?>
    274+
    275+
    if(isset($zonesListMarkup)): ?>
    276+
    print $zonesListMarkup ?>
    277+
    endif ?>
    278+
    279+
    if(isset($machineTypesListMarkup)): ?>
    280+
    print $machineTypesListMarkup ?>
    281+
    endif ?>
    282+
    283+
    if(isset($imagesListMarkup)): ?>
    284+
    print $imagesListMarkup ?>
    285+
    endif ?>
    286+
    287+
    if(isset($firewallsListMarkup)): ?>
    288+
    print $firewallsListMarkup ?>
    289+
    endif ?>
    290+
    291+
    if(isset($networksListMarkup)): ?>
    292+
    print $networksListMarkup ?>
    293+
    endif ?>
    294+
    295+
    if(isset($getInstanceWithMetadataMarkup)): ?>
    296+
    297+
    print $getInstanceWithMetadataMarkup ?>
    298+
    299+
    endif ?>
    300+
    301+
    if(isset($getInstanceMarkup)): ?>
    302+
    print $getInstanceMarkup ?>
    303+
    endif ?>
    304+
    305+
    if(isset($deleteInstanceMarkup)): ?>
    306+
    print $deleteInstanceMarkup ?>
    307+
    endif ?>
    308+
    309+
    if(isset($deleteInstanceWithMetadataMarkup)): ?>
    310+
    311+
    print $deleteInstanceWithMetadataMarkup ?>
    312+
    313+
    endif ?>
    314+
    315+
    if(isset($insertInstanceMarkup)): ?>
    316+
    print $insertInstanceMarkup ?>
    317+
    endif ?>
    318+
    319+
    if(isset($insertInstanceWithMetadataMarkup)): ?>
    320+
    321+
    print $insertInstanceWithMetadataMarkup?>
    322+
    323+
    endif ?>
    324+
    325+
    if(isset($operationsListMarkup)): ?>
    326+
    print $operationsListMarkup ?>
    327+
    endif ?>
    328+
    329+
    330+
    if(isset($authUrl)) {
    331+
    print "Connect Me!";
    332+
    } else {
    333+
    print "Logout";
    334+
    }
    335+
    ?>
    336+
    337+
    338+

    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.