Skip to content

Commit a88c8ee

Browse files
committed
Initial push
0 parents  commit a88c8ee

19 files changed

+1191
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.phar
2+
vendor

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 MaxCDN, LLC.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# MaxCDN REST API PHP Client
2+
3+
## Requirements
4+
- PHP 5.3 or above
5+
- PHP Curl Extension
6+
7+
## Installation with Composer
8+
Composer is the recommended way to utilize the MaxCDN RWS SDK PHP library. For more information on how to use Composer, see http://www.getcomposer.org .
9+
10+
1. Add "MaxCDN/rws-sdk-php" as a dependency in your composer.json project file.
11+
12+
```json
13+
{
14+
"require": {
15+
"MaxCDN/rws-sdk-php": "2.*"
16+
}
17+
}
18+
```
19+
20+
2. Download and install Composer (if not already installed).
21+
22+
```bash
23+
curl -sS https://getcomposer.org/installer | php
24+
```
25+
26+
3. Install your package dependencies.
27+
28+
```bash
29+
php composer.phar install -o
30+
```
31+
32+
This will download the MaxCDN library and configure composer to use it. Composer will also build an autoloader for your use in the next step.
33+
34+
4. Use Composer's autoloader.
35+
36+
Composer prepares an autoload file which autoloads the RWS library for you on demand. To use it, provide the following at the top of your PHP source files:
37+
38+
```php
39+
require_once '/path/to/vendor/autoload.php';
40+
```
41+
42+
It is advised that you understand how to optimze Composer's usage in Production environments. For more information about Composer, visit http://getcomposer.org
43+
44+
The libraries are located in the src/ directory. The classes are organized in a PSR-0 hierarchy. You can use any PSR-0 compliant autoloader for this library. Composer is the recommended method (see above).
45+
46+
## Usage
47+
```php
48+
<?php
49+
50+
$api = new MaxCDN("my_alias","consumer_key","consumer_secret");
51+
52+
// get account information
53+
echo $api->get('/account.json');
54+
55+
// delete a file from the cache
56+
$params = array('file' => '/robots.txt');
57+
echo $api->delete('/zones/pull.json/6055/cache', $params);
58+
59+
?>
60+
```
61+
62+
## Methods
63+
64+
It has support for `GET`, `POST`, `PUT` and `DELETE` OAuth signed requests.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "maxcdn/php-maxcdn",
3+
"description": "PHP Wrapper for MaxCDN Remote Web Services",
4+
"type": "library",
5+
"license": "MIT",
6+
"homepage": "http://docs.maxcdn.com",
7+
"support": {
8+
"email": "support@maxcdn.com",
9+
"source": "http://www.github.com/MaxCDN/php-maxcdn"
10+
},
11+
"require": {
12+
"php": ">=5.3.0",
13+
"ext-curl": "*"
14+
},
15+
"autoload": {
16+
"psr-0": {
17+
"MaxCDN\\": "src/",
18+
"": "src/"
19+
}
20+
}
21+
22+
}

src/MaxCDN.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* MaxCDN REST Client Library
4+
*
5+
* @copyright 2012
6+
* @author Karlo Espiritu
7+
* @version 1.0 2012-09-21
8+
*/
9+
class MaxCDN {
10+
11+
public $alias;
12+
13+
public $key;
14+
15+
public $secret;
16+
17+
public $MaxCDNrws_url = 'https://rws.maxcdn.com';
18+
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+
// set url
51+
curl_setopt($ch, CURLOPT_URL, $req_req);
52+
53+
//return the transfer as a string
54+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
55+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE);
56+
57+
// set curl timeout
58+
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
59+
60+
// set curl custom request type if not standard
61+
if ($method_type != "GET" && $method_type != "POST") {
62+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method_type);
63+
}
64+
65+
66+
if ($method_type == "POST" || $method_type == "PUT" || $method_type == "DELETE") {
67+
$query_str = \MaxCDN\OAuth\OAuthUtil::build_http_query($params);
68+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Content-Length: ' . strlen($query_str)));
69+
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_str);
70+
}
71+
72+
// retrieve headers
73+
curl_setopt($ch, CURLOPT_HEADER, 1);
74+
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
75+
76+
//set user agent
77+
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP MaxCDN API Client');
78+
79+
// make call
80+
$result = curl_exec($ch);
81+
$headers = curl_getinfo($ch);
82+
$curl_error = curl_error($ch);
83+
84+
// close curl resource to free up system resources
85+
curl_close($ch);
86+
87+
// $json_output contains the output string
88+
$json_output = substr($result, $headers['header_size']);
89+
90+
// catch errors
91+
if(!empty($curl_error) || empty($json_output)) {
92+
throw new \MaxCDN\RWSException("CURL ERROR: $curl_error, Output: $json_output", $headers['http_code'], null, $headers);
93+
}
94+
95+
return $json_output;
96+
}
97+
98+
public function get($selected_call, $params = array()){
99+
100+
return $this->execute($selected_call, 'GET', $params);
101+
}
102+
103+
public function post($selected_call, $params = array()){
104+
return $this->execute($selected_call, 'POST', $params);
105+
}
106+
107+
public function put($selected_call, $params = array()){
108+
return $this->execute($selected_call, 'PUT', $params);
109+
}
110+
111+
public function delete($selected_call, $params = array()){
112+
return $this->execute($selected_call, 'DELETE', $params);
113+
}
114+
115+
116+
}

src/MaxCDN/OAuth/OAuthConsumer.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* OAuth Consumer representation.
4+
*/
5+
namespace MaxCDN\OAuth;
6+
7+
class OAuthConsumer {
8+
public $key;
9+
public $secret;
10+
11+
function __construct($key, $secret, $callback_url=NULL) {
12+
$this->key = $key;
13+
$this->secret = $secret;
14+
$this->callback_url = $callback_url;
15+
}
16+
17+
function __toString() {
18+
return "\\MaxCDN\\OAuth\\OAuthConsumer[key=$this->key,secret=$this->secret]";
19+
}
20+
}
21+

src/MaxCDN/OAuth/OAuthDataStore.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace MaxCDN\OAuth;
3+
4+
class OAuthDataStore {
5+
function lookup_consumer($consumer_key) {
6+
// implement me
7+
}
8+
9+
function lookup_token($consumer, $token_type, $token) {
10+
// implement me
11+
}
12+
13+
function lookup_nonce($consumer, $token, $nonce, $timestamp) {
14+
// implement me
15+
}
16+
17+
function new_request_token($consumer, $callback = null) {
18+
// return a new token attached to this consumer
19+
}
20+
21+
function new_access_token($token, $consumer, $verifier = null) {
22+
// return a new access token attached to this consumer
23+
// for the user associated with this token if the request token
24+
// is authorized
25+
// should also invalidate the request token
26+
}
27+
28+
}
29+

src/MaxCDN/OAuth/OAuthException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Exception class for OAuth failures.
4+
*/
5+
namespace MaxCDN\OAuth;
6+
7+
class OAuthException extends Exception {
8+
// pass
9+
}
10+

0 commit comments

Comments
 (0)