jgillman's Liquid Web Update Unofficial tips, tricks, and happenings from a Liquid Web sales engineer

30Apr/120

StormOnDemand API Class update

I've yet again updated my StormOnDemand API PHP class.

For those who don't want to go to the PHP Classes site (which I highly recommend you do if you code OOP PHP - it's got a great collection of various PHP Classes), here is the source:

 PHP | 
 
 copy code |
?

01
02
<?php
03
	/*
04
	 * Author: Jason Gillman Jr.
05
	 * Description: This is my attempt at writing a PHP wrapper that will ease Storm API calls with PHP
06
	 * 				It will be designed to use the JSON format for talking with the API server.
07
	 * 				$api_method is as described in docs (Case doesn't matter)
08
	 * 				request() method returns an array generated from the API return
09
	 */
10
	class StormAPI
11
	{
12
		// Let's define attributes
13
		private $api_user, $api_pass, $base_url, $api_format, $api_full_uri, $api_request;
14
		private $api_request_body, $api_method, $api_params, $api_return; 
15
 
16
		function __construct($api_user, $api_pass, $api_method)
17
		{	
18
			$this->api_user = $api_user;
19
			$this->api_pass = $api_pass;
20
			$this->api_method = $api_method;
21
			$this->base_url = 'https://api.stormondemand.com/';
22
			$this->api_format = 'json';
23
 
24
			$this->api_full_uri = $this->base_url . $this->api_method . "." .$this->api_format;
25
			$this->api_request = curl_init($this->api_full_uri); // Instantiate
26
			curl_setopt($this->api_request, CURLOPT_RETURNTRANSFER, TRUE); // Don't dump directly to output
27
			curl_setopt($this->api_request, CURLOPT_SSL_VERIFYPEER, TRUE); // It does look like verification works now.
28
			curl_setopt($this->api_request, CURLOPT_USERPWD, "$this->api_user:$this->api_pass"); // Pass the creds
29
		}
30
 
31
		function add_param($parameter, $value)
32
		{
33
			$this->api_request_body['params'][$parameter] = $value;
34
		}
35
 
36
		function clear_params()
37
		{
38
			unset($this->api_request_body);
39
			curl_setopt($this->api_request, CURLOPT_HTTPGET, TRUE); //If the request was previously run with params, this cleans those out. Otherwise they go back with the request
40
		}
41
 
42
		function new_method($api_method, $clearparams = TRUE) // Clears out parameters by default, since they may not apply now
43
		{
44
			if($clearparams = TRUE)
45
			{
46
				unset($this->api_request_body);
47
				curl_setopt($this->api_request, CURLOPT_HTTPGET, TRUE); //If the request was previously run with params, this cleans those out. Otherwise they go back with the request
48
			}
49
 
50
			$this->api_method = $api_method; // New method, coming right up!
51
			$this->api_full_uri = $this->base_url . $this->api_method . "." .$this->api_format; // New URI since method change
52
			curl_setopt($this->api_request, CURLOPT_URL, $this->api_full_uri);
53
 
54
		}
55
 
56
		function request()
57
		{
58
			if(is_array($this->api_request_body)) // We have params
59
			{
60
				curl_setopt($this->api_request, CURLOPT_POST, TRUE); //POST method since we'll be feeding params
61
				curl_setopt($this->api_request, CURLOPT_HTTPHEADER, Array('Content-type: application/json')); // Since we'll be using JSON
62
				curl_setopt($this->api_request, CURLOPT_POSTFIELDS, json_encode($this->api_request_body)); // Insert the parameters
63
			}
64
 
65
			// Now send the request and get the return on investment
66
			try
67
			{
68
				return json_decode(curl_exec($this->api_request), TRUE); // Pull the trigger and get nice pretty arrays of returned data
69
			}
70
			catch (HTTP_Request2_Exception $e)
71
			{
72
				echo 'Error: ' . $e->getMessage();
73
			}
74
		}
75
	}
76
?>
77

Essentially, I added two new methods: clear_params() and new_method(). Both of these do what you might think - clearing existing set parameters and choosing a different API method.

Also, if you're so inclined, I believe you should be able to track updates to the PHP Classes project that I have with this RSS link. I've also added it to the right side of the page.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


*

No trackbacks yet.

FireStats icon Powered by FireStats
Optimization WordPress Plugins & Solutions by W3 EDGE