Skip to content

Instantly share code, notes, and snippets.

@alanivey
Created December 7, 2011 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanivey/5bd70668070580bcd121 to your computer and use it in GitHub Desktop.
Save alanivey/5bd70668070580bcd121 to your computer and use it in GitHub Desktop.
Liquid Web StormonDemand API with JSON
<?php
/*
* Author: Jason Gillman Jr.
* Description: This is my attempt at writing a PHP wrapper that will ease Storm API calls with PHP
* It will be designed to use the JSON format for talking with the API server.
* $api_method is as described in docs (Case matters)
* request() method returns an array generated from the API return
* Source: http://lw.rrfaae.com/2011/07/utilizing-the-storm-api-with-php/
* Converted from YAML to JSON by Alan Ivey
*/
class StormAPI
{
// Let's define attributes
private $api_user, $api_pass, $base_url, $api_format, $api_full_uri, $api_request;
private $api_request_body, $api_method, $api_params, $api_return;
function __construct($api_user, $api_pass, $api_method)
{
// Requires PEAR Package HTTP_Request2
require_once('HTTP/Request2.php');
$this->api_user = $api_user;
$this->api_pass = $api_pass;
$this->api_method = $api_method;
$this->base_url = 'https://api.stormondemand.com/';
$this->api_format = 'json';
$this->api_full_uri = $this->base_url . $this->api_method . "." .$this->api_format;
}
function add_param($parameter, $value)
{
$this->api_request_body['params'][$parameter] = $value;
}
function request()
{
// HTTP Request goodness
if(isset($this->api_request_body)) // We have params
{
$api_request = new HTTP_Request2($this->api_full_uri, HTTP_Request2::METHOD_POST); // Instantiate with POST method since we'll be feeding params
$api_request->setHeader('Content-type: application/json'); // Specify what you're feeding it
$api_request->setBody(json_encode($this->api_request_body));
$ztest = json_encode($this->api_request_body);
print_r($ztest);
}
else // No params
{
$api_request = new HTTP_Request2($this->api_full_uri, HTTP_Request2::METHOD_GET); // Instantiate with GET method since no params
}
$api_request->setAuth($this->api_user, $this->api_pass, HTTP_Request2::AUTH_BASIC); // Auth stuff
$api_request->setConfig('ssl_verify_peer', FALSE); // This is needed, otherwise HTTP_Request errors last I checked
// Now send the request and get the return on investment
try
{
$api_return = $api_request->send();
return json_decode($api_return->getBody()); // We now have a nice array of all the returned data
}
catch (HTTP_Request2_Exception $e)
{
echo 'Error: ' . $e->getMessage();
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment