1- # Client for 3scale web service management system API [ ![ Build Status] ( https://secure.travis-ci.org/3scale/3scale_ws_api_for_php.png?branch=master )] ( http://travis-ci.org/3scale/3scale_ws_api_for_php )
1+ # Client for 3scale Web Service Management API [ ![ Build Status] ( https://secure.travis-ci.org/3scale/3scale_ws_api_for_php.png?branch=master )] ( http://travis-ci.org/3scale/3scale_ws_api_for_php )
223scale integration plugin for PHP applications. 3scale is an API Infrastructure service which handles API Keys, Rate Limiting, Analytics, Billing Payments and Developer Management. Includes a configurable API dashboard and developer portal CMS. More product stuff at http://www.3scale.net/ , support information at http://support.3scale.net/ .
33
4+ ### Tutorials
5+ * Plugin Setup: https://support.3scale.net/docs/deployment-options/plugin-setup
6+ * Rate Limiting: https://support.3scale.net/docs/access-control/rate-limits
7+ * Analytics Setup: https://support.3scale.net/quickstarts/3scale-api-analytics
8+
49## Installation
510
611Download the source code from github: http://github.com/3scale/3scale_ws_api_for_php and place it somewhere accessible from your project.
@@ -10,106 +15,182 @@ Download the source code from github: http://github.com/3scale/3scale_ws_api_for
1015Require the ThreeScaleClient.php file (assuming you placed the library somewhere within the
1116include path):
1217
13- require_once('lib/ThreeScaleClient.php')
18+ ``` php
19+ require_once('lib/ThreeScaleClient.php')
20+ ```
1421
15- Then, create an instance of the client, giving it your provider API key:
22+ Then create an instance of the client
23+ ``` php
24+ $client = new ThreeScaleClient();
25+ ```
1626
17- $client = new ThreeScaleClient("your provider key");
27+ > NOTE: unless you specify ``` ThreeScaleClient(); ``` you will be expected to specify
28+ a ` provider_key ` parameter, which is deprecated in favor of Service Tokens:
29+ ``` php
30+ $client = new ThreeScaleClient("your provider key");
31+ ```
1832
1933Because the object is stateless, you can create just one and store it globally.
2034
21- ** NOTE: From November 2016 ` service_id ` is mandatory.**
35+ Then you can perform calls in the client:
36+
37+ ``` php
38+ $response = $client->authorize("app id", "app key", new ThreeScaleClientCredentials("service id", "service token"));
39+ ```
40+
41+ ``` php
42+ $response = $client->report(array(array('app_id' => "app's id"],'usage' => array('hits' => 1))), new ThreeScaleClientCredentials("service id", "service token"));
43+ ```
44+
45+ ** NOTE:` service_id ` is mandatory since November 2016, both when using service tokens and when using provider keys**
2246
2347### Authorize
2448
2549To authorize a particular application, call the ` authorize ` method passing it the
26- application id and optionally the application key:
50+ ` application id ` and ` service id ` and optionally the application key:
51+
52+ ``` php
53+ $response = $client->authorize("app id", "app key", new ThreeScaleClientCredentials("service id", "service token"));
54+ ```
55+
56+ If you had configured a (deprecated) provider key, you would instead use:
2757
28- $response = $client->authorize("the app id", "the app key");
29- or
30- $response = $client->authorize("the app id", "the app key", "the service id");
58+ ``` php
59+ $response = $client->authorize("the app id", "the app key", "service id"));
60+ ```
3161
3262Then call the ` isSuccess() ` method on the returned object to see if the authorization was
3363successful:
3464
35- if ($response->isSuccess()) {
36- // All fine, proceeed.
37- } else {
38- // Something's wrong with this app.
39- }
65+ ``` php
66+ if ($response->isSuccess()) {
67+ // All fine, proceeed.
68+ } else {
69+ // Something's wrong with this app.
70+ }
71+ ```
4072
4173If both provider and app id are valid, the response object contains additional information about the status of the application:
4274
43- // Returns the name of the plan the application is signed up to.
44- $response->getPlan()
75+ ``` php
76+ //Returns the name of the plan the application is signed up to.
77+ $response->getPlan()
78+ ```
4579
4680If the plan has defined usage limits, the response contains details about the usage broken down by the metrics and usage limit periods.
4781
48- // The usageReports array contains one element per each usage limit defined on the plan.
49- $usageReports = $response->getUsageReports();
50- $usageReport = $usageReports[0];
82+ ``` php
83+ // The usageReports array contains one element per each usage limit defined on the plan.
84+ $usageReports = $response->getUsageReports();
85+ $usageReport = $usageReports[0];
5186
52- // The metric
53- $usageReport->getMetric() // "hits"
87+ // The metric
88+ $usageReport->getMetric() // "hits"
5489
55- // The period the limit applies to
56- $usageReport->getPeriod() // "day"
57- $usageReport->getPeriodStart() // 1272405600 (Unix timestamp for April 28, 2010, 00:00:00)
58- $usageReport->getPeriodEnd() // 1272492000 (Unix timestamp for April 29, 2010, 00:00:00)
90+ // The period the limit applies to
91+ $usageReport->getPeriod() // "day"
92+ $usageReport->getPeriodStart() // 1272405600 (Unix timestamp for April 28, 2010, 00:00:00)
93+ $usageReport->getPeriodEnd() // 1272492000 (Unix timestamp for April 29, 2010, 00:00:00)
5994
60- // The current value the application already consumed in the period
61- $usageReport->getCurrentValue() // 8032
95+ // The current value the application already consumed in the period
96+ $usageReport->getCurrentValue() // 8032
6297
63- // The maximal value allowed by the limit in the period
64- $usageReport->getMaxValue() // 10000
65-
66- // If the limit is exceeded, this will be true, otherwise false:
67- $usageReport->isExceeded() // false
98+ // The maximal value allowed by the limit in the period
99+ $usageReport->getMaxValue() // 10000
100+
101+ // If the limit is exceeded, this will be true, otherwise false:
102+ $usageReport->isExceeded() // false
103+ ```
68104
69105If the authorization failed, the ` getErrorCode() ` returns system error code and ` getErrorMessage() ` human readable error description:
70106
71- $response->getErrorCode() // "usage_limits_exceeded"
72- $response->getErrorMessage() // "Usage limits are exceeded"
107+ ``` php
108+ $response->getErrorCode() // "usage_limits_exceeded"
109+ $response->getErrorMessage() // "Usage limits are exceeded"
110+ ```
111+
112+ ### Authrep
113+
114+ To authorize a particular application, call the ` authrep ` method passing it the
115+ ` application id ` and ` service id ` and optionally the application key:
116+
117+ ``` php
118+ $response = $client->authrep("app id", "app key", new ThreeScaleClientCredentials("service id", "service token"), array('hits' => 1));
119+ ```
120+
121+ Then call the ` isSuccess() ` method on the returned object to see if the authorization was
122+ successful:
123+
124+ ``` php
125+ if ($response->isSuccess()) {
126+ // All fine, proceeed.
127+ } else {
128+ // Something's wrong with this app.
129+ }
130+ ```
131+
132+ If both provider and app id are valid, the response object contains additional information about the status of the application:
133+
134+ ``` php
135+ //Returns the name of the plan the application is signed up to.
136+ $response->getPlan()
137+ ```
138+
139+ You can also use other patterns such as ` user_key ` mode during the authrep call
140+
141+ ``` php
142+ $response = $client->authrep_with_user_key("app id", "app key", new ThreeScaleClientCredentials("service id", "service token"), array('hits' => 1));
143+ ```
73144
74145### Report
75146
76147To report usage, use the ` report ` method. You can report multiple transaction at the same time:
77148
78- $response = $client->report(array(
79- array('app_id' => "first app's id", 'usage' => array('hits' => 1)),
80- array('app_id' => "second app's id", 'usage' => array('hits' => 1))), "service id");
149+ ``` php
150+ $response = $client->report(array(
151+ array('app_id' => "first app's id",'usage' => array('hits' => 1)),
152+ array('app_id' => "second app's id", 'usage' => array('hits' => 1))), new ThreeScaleClientCredentials("service id", "service token"));
153+ ```
81154
82- The ` "app_id" ` and ` "usage" ` parameters are required. Additionaly, you can specify a timestamp
155+ The ` "app_id" ` , ` "usage" ` parameters are required alongiwth ` service id ` and ` service token ` . Additionaly, you can specify a timestamp
83156of the transaction:
84157
85- $response = $client->report(array(
86- array('app_id' => "app's id",
87- 'usage' => array('hits' => 1),
88- 'timestamp' => mktime(12, 36, 0, 4, 28, 2010))));
158+ ``` php
159+ $response = $client->report(array(
160+ array('app_id' => "app's id",
161+ 'usage' => array('hits' => 1),
162+ 'timestamp' => mktime(12, 36, 0, 4, 28, 2010, new ThreeScaleClientCredentials("service id", "service token")));
163+ ```
89164
90165The timestamp can be either an unix timestamp (as integer) or a string. The string has to be in a
91166format parseable by the [ strtotime] ( http://php.net/manual/en/function.strtotime.php ) function.
92167For example:
93168
94- "2010-04-28 12:38:33 +0200"
169+ ``` php
170+ "2010-04-28 12:38:33 +0200"
171+ ```
95172
96173If the timestamp is not in UTC, you have to specify a time offset. That's the "+0200"
97174(two hours ahead of the Universal Coordinate Time) in the example above.
98175
99176Then call the ` isSuccess() ` method on the returned response object to see if the report was
100177successful:
101178
102- if ($response->isSuccess()) {
103- // All OK.
104- } else {
105- // There was an error.
106- }
179+ ``` php
180+ if ($response->isSuccess()) {
181+ // All OK.
182+ } else {
183+ // There was an error.
184+ }
185+ ```
107186
108187In case of error, the ` getErrorCode() ` returns system error code and ` getErrorMessage() `
109188human readable error description:
110189
111- $response->getErrorCode() // "provider_key_invalid"
112- $response->getErrorMessage() // "provider key \"foo\" is invalid"
190+ ``` php
191+ $response->getErrorCode() // "provider_key_invalid"
192+ $response->getErrorMessage() // "provider key \"foo\" is invalid"
193+ ```
113194
114195## Plugin integration
115196
@@ -119,6 +200,10 @@ If you are interested in integrating the plugin with:
119200
120201* [ Symphony2] ( http://symfony.com/ ) check [ tonivdv's 3scaleBundle] ( https://github.com/tonivdv/3scaleBundle ) . This is kindly maintained by [ Adlogix] ( http://www.adlogix.eu ) tech team.
121202
203+ ## To Test
204+
205+ To run tests: ` php test/all.php `
206+
122207## Legal
123208
124209Copyright (c) 2010 3scale networks S.L., released under the MIT license.
0 commit comments