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

3Apr/120

Updated Storm On Demand API PHP Class

I just wanted to put out that I've updated my API class to use cURL instead of HTTP_REQUEST2 in my initial post.

Although it still utilizes YAML, this is my first move at getting away from third party packages.

Enjoy!

 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 YAML format for talking with the API server.
07
	 * 				$api_method is as described in docs (Case matters)
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 = 'yaml';
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, FALSE); // Apparently verification doesn't work with the API?
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 request()
37
		{
38
			if(isset($this->api_request_body)) // We have params
39
			{
40
				curl_setopt($this->api_request, CURLOPT_POST, TRUE); //POST method since we'll be feeding params
41
				curl_setopt($this->api_request, CURLOPT_HTTPHEADER, Array('Content-type: application/yaml')); // Since we'll be using YAML
42
				curl_setopt($this->api_request, CURLOPT_POSTFIELDS, yaml_emit($this->api_request_body)); // Insert the parameters
43
			}
44
 
45
			// Now send the request and get the return on investment
46
			try
47
			{
48
				return yaml_parse(curl_exec($this->api_request)); // Pull the trigger and get nice pretty arrays of returned data
49
			}
50
			catch (HTTP_Request2_Exception $e)
51
			{
52
				echo 'Error: ' . $e->getMessage();
53
			}
54
		}
55
	}
56
?>
57

Update on 4Apr12 @ 1300EDT
I have updated the class to utilize JSON instead of YAML. Code change was actually pretty straight forward. It is shown below:

 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 YAML format for talking with the API server.
07
	 * 				$api_method is as described in docs (Case matters)
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, FALSE); // Apparently verification doesn't work with the API?
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 request()
37
		{
38
			if(isset($this->api_request_body)) // We have params
39
			{
40
				curl_setopt($this->api_request, CURLOPT_POST, TRUE); //POST method since we'll be feeding params
41
				curl_setopt($this->api_request, CURLOPT_HTTPHEADER, Array('Content-type: application/json')); // Since we'll be using JSON
42
				curl_setopt($this->api_request, CURLOPT_POSTFIELDS, json_encode($this->api_request_body)); // Insert the parameters
43
			}
44
 
45
			// Now send the request and get the return on investment
46
			try
47
			{
48
				return json_decode(curl_exec($this->api_request), TRUE); // Pull the trigger and get nice pretty arrays of returned data
49
			}
50
			catch (HTTP_Request2_Exception $e)
51
			{
52
				echo 'Error: ' . $e->getMessage();
53
			}
54
		}
55
	}
56
?>
57

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