|
1 | 1 | <?php
|
2 |
| -/** |
| 2 | +/** |
3 | 3 | * MaxCDN REST Client Library
|
4 |
| - * |
| 4 | + * |
5 | 5 | * @copyright 2012
|
6 | 6 | * @author Karlo Espiritu
|
7 | 7 | * @version 1.0 2012-09-21
|
8 | 8 | */
|
9 |
| -class MaxCDN { |
10 |
| - |
11 |
| - public $alias; |
| 9 | +class MaxCDN |
| 10 | +{ |
| 11 | + public $alias; |
12 | 12 |
|
13 |
| - public $key; |
| 13 | + public $key; |
| 14 | + |
| 15 | + public $secret; |
| 16 | + |
| 17 | + public $apiUrl; |
14 | 18 |
|
15 |
| - public $secret; |
16 |
| - |
17 |
| - public $MaxCDNrws_url = 'https://rws.maxcdn.com'; |
18 |
| - |
19 | 19 | private $consumer;
|
20 |
| - |
21 |
| - public function __construct($alias, $key, $secret, $options=null) { |
22 |
| - $this->alias = $alias; |
23 |
| - $this->key = $key; |
24 |
| - $this->secret = $secret; |
25 |
| - $this->consumer = new \MaxCDN\OAuth\OAuthConsumer($key, $secret, NULL); |
26 |
| - |
27 |
| - } |
28 |
| - |
29 |
| - private function execute($selected_call, $method_type, $params) { |
30 |
| - // the endpoint for your request |
31 |
| - $endpoint = "$this->MaxCDNrws_url/$this->alias$selected_call"; |
32 |
| - |
33 |
| - //parse endpoint before creating OAuth request |
34 |
| - $parsed = parse_url($endpoint); |
35 |
| - if (array_key_exists("parsed", $parsed)) |
36 |
| - { |
37 |
| - parse_str($parsed['query'], $params); |
38 |
| - } |
39 |
| - |
40 |
| - //generate a request from your consumer |
41 |
| - $req_req = \MaxCDN\OAuth\OAuthRequest::from_consumer_and_token($this->consumer, NULL, $method_type, $endpoint, $params); |
42 |
| - |
43 |
| - //sign your OAuth request using hmac_sha1 |
44 |
| - $sig_method = new \MaxCDN\OAuth\OAuthSignatureMethod_HMAC_SHA1(); |
45 |
| - $req_req->sign_request($sig_method, $this->consumer, NULL); |
46 |
| - |
47 |
| - // create curl resource |
48 |
| - $ch = curl_init(); |
49 |
| - |
50 |
| - // force curl http/1.1 |
51 |
| - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
52 |
| - |
53 |
| - // set url |
54 |
| - curl_setopt($ch, CURLOPT_URL, $req_req); |
55 |
| - |
56 |
| - //return the transfer as a string |
57 |
| - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
58 |
| - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , TRUE); |
59 |
| - |
60 |
| - // set curl timeout |
61 |
| - curl_setopt($ch, CURLOPT_TIMEOUT, 60); |
62 |
| - |
63 |
| - // set curl custom request type if not standard |
64 |
| - if ($method_type != "GET" && $method_type != "POST") { |
65 |
| - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method_type); |
66 |
| - } |
67 |
| - |
68 |
| - |
69 |
| - if ($method_type == "POST" || $method_type == "PUT" || $method_type == "DELETE") { |
70 |
| - $query_str = \MaxCDN\OAuth\OAuthUtil::build_http_query($params); |
71 |
| - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Content-Length: ' . strlen($query_str))); |
72 |
| - curl_setopt($ch, CURLOPT_POSTFIELDS, $query_str); |
73 |
| - } |
74 |
| - |
75 |
| - // retrieve headers |
76 |
| - curl_setopt($ch, CURLOPT_HEADER, 1); |
77 |
| - curl_setopt($ch, CURLINFO_HEADER_OUT, 1); |
78 |
| - |
79 |
| - //set user agent |
80 |
| - curl_setopt($ch, CURLOPT_USERAGENT, 'PHP MaxCDN API Client'); |
81 |
| - |
82 |
| - // make call |
83 |
| - $result = curl_exec($ch); |
84 |
| - $headers = curl_getinfo($ch); |
85 |
| - $curl_error = curl_error($ch); |
86 |
| - |
87 |
| - // close curl resource to free up system resources |
88 |
| - curl_close($ch); |
89 |
| - |
90 |
| - // $json_output contains the output string |
91 |
| - $json_output = substr($result, $headers['header_size']); |
92 |
| - |
93 |
| - // catch errors |
94 |
| - if(!empty($curl_error) || empty($json_output)) { |
95 |
| - throw new \MaxCDN\RWSException("CURL ERROR: $curl_error, Output: $json_output", $headers['http_code'], null, $headers); |
96 |
| - } |
97 |
| - |
98 |
| - return $json_output; |
99 |
| - } |
100 |
| - |
101 |
| - public function get($selected_call, $params = array()){ |
102 |
| - |
103 |
| - return $this->execute($selected_call, 'GET', $params); |
104 |
| - } |
105 |
| - |
106 |
| - public function post($selected_call, $params = array()){ |
107 |
| - return $this->execute($selected_call, 'POST', $params); |
108 |
| - } |
109 |
| - |
110 |
| - public function put($selected_call, $params = array()){ |
111 |
| - return $this->execute($selected_call, 'PUT', $params); |
112 |
| - } |
113 |
| - |
114 |
| - public function delete($selected_call, $params = array()){ |
115 |
| - return $this->execute($selected_call, 'DELETE', $params); |
116 |
| - } |
117 |
| - |
118 |
| - |
| 20 | + |
| 21 | + public function __construct($alias, $key, $secret, $server = 'https://rws.maxcdn.com') |
| 22 | + { |
| 23 | + $this->alias = $alias; |
| 24 | + $this->key = $key; |
| 25 | + $this->secret = $secret; |
| 26 | + $this->consumer = new \MaxCDN\OAuth\OAuthConsumer($key, $secret, null); |
| 27 | + $this->apiUrl = $server; |
| 28 | + } |
| 29 | + |
| 30 | + private function execute($selected_call, $method_type, $params) |
| 31 | + { |
| 32 | + // the endpoint for your request |
| 33 | + $endpoint = "$this->apiUrl/$this->alias$selected_call"; |
| 34 | + |
| 35 | + //parse endpoint before creating OAuth request |
| 36 | + $parsed = parse_url($endpoint); |
| 37 | + if (array_key_exists("parsed", $parsed)) { |
| 38 | + parse_str($parsed['query'], $params); |
| 39 | + } |
| 40 | + |
| 41 | + //generate a request from your consumer |
| 42 | + $req_req = \MaxCDN\OAuth\OAuthRequest::from_consumer_and_token( |
| 43 | + $this->consumer, |
| 44 | + null, |
| 45 | + $method_type, |
| 46 | + $endpoint, |
| 47 | + $params |
| 48 | + ); |
| 49 | + |
| 50 | + //sign your OAuth request using hmac_sha1 |
| 51 | + $sig_method = new \MaxCDN\OAuth\OAuthSignatureMethod_HMAC_SHA1(); |
| 52 | + $req_req->sign_request($sig_method, $this->consumer, null); |
| 53 | + |
| 54 | + // create curl resource |
| 55 | + $ch = curl_init(); |
| 56 | + |
| 57 | + // force curl http/1.1 |
| 58 | + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
| 59 | + |
| 60 | + // set url |
| 61 | + curl_setopt($ch, CURLOPT_URL, $req_req); |
| 62 | + |
| 63 | + //return the transfer as a string |
| 64 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 65 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
| 66 | + |
| 67 | + // set curl timeout |
| 68 | + curl_setopt($ch, CURLOPT_TIMEOUT, 60); |
| 69 | + |
| 70 | + // set curl custom request type if not standard |
| 71 | + if ($method_type != "GET" && $method_type != "POST") { |
| 72 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method_type); |
| 73 | + } |
| 74 | + |
| 75 | + if ($method_type == "POST" || $method_type == "PUT" || $method_type == "DELETE") { |
| 76 | + $query_str = \MaxCDN\OAuth\OAuthUtil::build_http_query($params); |
| 77 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Content-Length: ' . strlen($query_str))); |
| 78 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $query_str); |
| 79 | + } |
| 80 | + |
| 81 | + // retrieve headers |
| 82 | + curl_setopt($ch, CURLOPT_HEADER, 1); |
| 83 | + curl_setopt($ch, CURLINFO_HEADER_OUT, 1); |
| 84 | + |
| 85 | + //set user agent |
| 86 | + curl_setopt($ch, CURLOPT_USERAGENT, 'PHP MaxCDN API Client'); |
| 87 | + |
| 88 | + // make call |
| 89 | + $result = curl_exec($ch); |
| 90 | + $headers = curl_getinfo($ch); |
| 91 | + $curl_error = curl_error($ch); |
| 92 | + |
| 93 | + // close curl resource to free up system resources |
| 94 | + curl_close($ch); |
| 95 | + |
| 96 | + // $json_output contains the output string |
| 97 | + $json_output = substr($result, $headers['header_size']); |
| 98 | + |
| 99 | + // catch errors |
| 100 | + if (!empty($curl_error) || empty($json_output)) { |
| 101 | + throw new \MaxCDN\RWSException( |
| 102 | + "CURL ERROR: $curl_error, Output: $json_output", |
| 103 | + $headers['http_code'], |
| 104 | + null, |
| 105 | + $headers |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + return $json_output; |
| 110 | + } |
| 111 | + |
| 112 | + public function get($selected_call, $params = array()) |
| 113 | + { |
| 114 | + return $this->execute($selected_call, 'GET', $params); |
| 115 | + } |
| 116 | + |
| 117 | + public function post($selected_call, $params = array()) |
| 118 | + { |
| 119 | + return $this->execute($selected_call, 'POST', $params); |
| 120 | + } |
| 121 | + |
| 122 | + public function put($selected_call, $params = array()) |
| 123 | + { |
| 124 | + return $this->execute($selected_call, 'PUT', $params); |
| 125 | + } |
| 126 | + |
| 127 | + public function delete($selected_call, $params = array()) |
| 128 | + { |
| 129 | + return $this->execute($selected_call, 'DELETE', $params); |
| 130 | + } |
119 | 131 | }
|
0 commit comments