Php.gif

Contents


64-warn.gif
NOTE: GoGrid Support WILL NOT troubleshoot customer-modified code. The code snippets and examples provided in this API documentation can be modified to customize your own code, but GoGrid Support cannot troubleshoot any errors you may receive due to missing syntax or illegal arguments. We WILL support our API interface and can troubleshoot any API server-side errors you may be receiving. Please note that all of the code snippets and examples provided in this API documentation have been thoroughly tested and verified as properly working.


Prerequisites

GoGridClient.php

<?PHP
/*
 * This class provides functions for generating and submitting signed HTTP requests for the GoGrid API.
 * It can be instantiated with or without server, key and secret values.
 *
 */
class GoGridClient
{
  // The key, secret and server for the api server 
  // Developers should substitue their apikey and secret here
  var $_format  = "xml";
  var $_server  = "https://api.gogrid.com/api";
  var $_apikey  = "YOUR API KEY";
  var $_secret  = "YOUR SHARED SECRET";
  var $_v       = "1.0";


  /*
   * Constructor that takes values for server, key and secret
   */
  function GoGridClient($server = null, $apikey = null, $secret = null, $format = null, $version = null)
  {
    if($server != null)
    {
      $this->_server = $server;
    }
    if($version != null)
    {
      $this->_v = $version;
    }
    if($format != null)
    {
      $this->_format = $format;
    }
    if($apikey != null)
    {
      $this->_apikey = $apikey;
    }
    if($secret != null)
    {
      $this->_secret = $secret;
    }

    return true;
  }

  /*
   * Build a signed request url for the API
   */
  function getRequestURL($method, $_params)
  {
    $params['format'] = $this->_format;
    $params['api_key'] = $this->_apikey;
    $params['v'] = $this->_v;

    // Start building the URL string with the server and method
    $requestUrl = $this->_server . "/$method?";

    // Copy the passed in parameters into the default parameter
    if($_params)
    {
      foreach($_params as $key => $val)
      {
        $params[$key] = $val;
      }
    }

    // Generate the signature from the api_key and secret in the params
    $params['sig'] = $this->getSignature($this->_apikey,$this->_secret);

    // Append the param keys and alues to the URL string
    foreach($params as $key => $val)
    {
      $requestUrl .= "$key=" . urlencode($val) . "&";
    }
    return $requestUrl;
  }

  /*
   * Get a URL signature value for a particular key and secret
   */
  function getSignature($key, $secret)
  {
    $timestamp = time();
    $sig = md5($key . $secret . $timestamp);
    return $sig;
  }

  /*
   * Make the api request over HTTP
   */
  function sendAPIRequest($url)
  {
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    // grab URL and pass it to the browser
    $response = curl_exec($ch);

    // Handle any HTTP errors by simply returning the error message string
    if (curl_errno($ch))
    {
      $response = curl_error($ch);
    } else {
      curl_close($ch); // make sure the connection is closed
    }

    return $response;
  }

}
?>

GoGridAPIExample.php

<?php
/*
 * Example of how to use the GoGridClient class.
 * 
 */
require_once("GoGridClient.php");


// Instantiate the client object with default values
$client = new GoGridClient();

// make a call
$request = $client->getRequestURL("grid/server/list",array("format" => "xml"));
print("Request :$request\n");  

$response = $client->sendAPIRequest($request);
print("Response :$response\n");

?>
Personal tools