Skip to content

Commit 69e6db6

Browse files
committed
Making it a working composer package and a modern namespaced class. Updated readme and added a new example.
1 parent 86281d2 commit 69e6db6

File tree

7 files changed

+535
-81
lines changed

7 files changed

+535
-81
lines changed

Dockerfile

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
# zend 2 is provided by the image, though we could switch to composer
2-
FROM jeffersonvv/zend2
1+
FROM php:7
2+
3+
# installing composer
4+
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
5+
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
6+
php composer-setup.php && \
7+
php -r "unlink('composer-setup.php');" && \
8+
mv composer.phar /usr/local/bin/composer
9+
10+
# git is required for composer, micro is my favorite editor
11+
RUN apt update ; apt install -y git micro
12+
13+
COPY composer-example /app
14+
WORKDIR /app
315

416
# then two dependencies of OX lib
5-
RUN composer require zendframework/zendoauth
6-
RUN composer require zendframework/zendrest
7-
# and just throw it into vendor (so it can load dependencies)
8-
# vendor = /var/www/zend/vendor
9-
COPY OX3_Api_Client2.php vendor/
17+
#RUN composer require zendframework/zendoauth
18+
#RUN composer require zendframework/zendrest
19+
RUN composer update
20+
21+
# overwrite sources downloaded from github if you're building it from THIS repo anyway
22+
# so that you can include any local changes
23+
COPY src ./vendor/openx/ox3-php-api-client/src
24+
25+
CMD tail -f /dev/null
1026

27+
ENTRYPOINT /bin/bash
1128

1229
# if you wanna try debugging with Charles:
1330
#RUN wget www.charlesproxy.com/getssl
@@ -17,4 +34,4 @@ COPY OX3_Api_Client2.php vendor/
1734
#RUN chmod 644 /usr/local/share/ca-certificates/charles/charles.crt
1835
# these seem ignored
1936
#ENV http_proxy=http://host.docker.internal:8888
20-
#ENV https_proxy=https://host.docker.internal:8888
37+
#ENV https_proxy=https://host.docker.internal:8888

README.md

Lines changed: 110 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,55 @@
11
# ox3-php-api-client
22
This library provides a client class with examples to facilitate access to the OpenX API.
3-
The file OX3_Api_Client.php supports Zend Framework Version 1 and the file OX3_Api_Client2.php supports Zend Framework Version 2.
3+
It handles, most of all, the OAuth1 login process used by OpenX API, and some basic path operations.
4+
5+
In `src\OpenX\PlatformAPI` directory,
6+
* `OXApiClient.php` (using patched Zend2) is the one supported by composer and autoloader, a fully namespaced client to be used in PHP7+
7+
8+
and if you need an older approach and want to use/copy just the file...
9+
* `OX3_Api_Client.php` supports Zend Framework Version 1
10+
* `OX3ApiClient2.php` supports Zend Framework Version 2
11+
12+
13+
## Help appreciated
14+
15+
As we do not actively maintain PHP projects, any help in
16+
straightening up the package and readme will be appreciated.
17+
18+
What might need a review:
19+
- better or less mixed install instructions
20+
- let us know if you need modern (composer, namespaces) or old (dirty file copying, old PHP) setup support
21+
- the `/3.0/` paths of the API are obsolete and not supported anymore, we can clean them up from the code
22+
- make the package independent from frameworks
23+
- especially ones that are obsolete (Zend)
24+
25+
We'd like to thank:
26+
- Fei Song for a push towards making
27+
this a proper composer package.
28+
29+
30+
## Installation
31+
32+
**(recommended) if working with Composer and Zend2**
33+
34+
* See the example `Dockerfile` for inspiration
35+
* You should only need to add `openx/ox3-php-api-client` as dependency
36+
* Then you can use the class `OpenX\PlatformAPI\OXApiClient` in your project.
37+
38+
### Try it out in the docker image
39+
40+
* `docker build . -t openx-test`
41+
* `docker run -it openx-test bash`
42+
* `export OPENX_URI=http://your-ui.openx.net`
43+
* `export [email protected]`
44+
* `export OPENX_PASWORD="My password satisfies the sane requirements for a long p@assword!"`
45+
* `export OPENX_KEY=c0n5um3rK3y`
46+
* `export OPENX_SECRET=c0n5um3rS3cR3T`
47+
* `export OPENX_REALM="your_realm" # (usually same as hostname prefix)`
48+
* `php example.php`
49+
50+
51+
## Older install instructions, to be reviewed:
452

5-
#Installation
653
**If Working with Zend Framework 1:**
754
1) Install Zend Framework 1.12.13 (link: http://framework.zend.com/downloads/latest#ZF2).
855
2) Set include_path:
@@ -27,53 +74,55 @@ set_include_path($path1);
2774
4) Install the required Zend packages, ZendOAuth and ZendRest:
2875
To do so, add the "repositories" and "require" sections in the composer.json file so the file looks
2976
as follows:
30-
````
31-
{
32-
"name": "",
33-
"authors": [
77+
```json
3478
{
35-
"name": "",
36-
"email": ""
79+
"name": "",
80+
"authors": [
81+
{
82+
"name": "",
83+
"email": ""
84+
}
85+
],
86+
"repositories": [
87+
{
88+
"type": "composer",
89+
"url": "https://packages.zendframework.com/"
90+
}
91+
],
92+
"require": {
93+
"zendframework/zendoauth": "2.0.*",
94+
"zendframework/zendrest": "2.0.*"
95+
}
3796
}
38-
],
97+
```
3998

40-
"repositories": [
41-
{
42-
"type": "composer",
43-
"url": "https://packages.zendframework.com/"
44-
}
45-
],
46-
"require": {
47-
"zendframework/zendoauth": "2.0.*",
48-
"zendframework/zendrest": "2.0.*"
49-
}
50-
}
51-
````
5299
5) Run the command "php composer.phar install" to install the packages and their dependencies.
53100
6) Set include_path:
54101
* Method 1: Navigate to /private/etc/php.ini and set include_path to the paths to the Zend Framework under the ".../library/" directory and to the newly installed packages under the ".../vendor/" directory. The "vendor" folder should appear in your working directory after installing the packages with composer.
55-
````
102+
```ini
56103
include_path = ".:/Users/.../ZendFramework-2.4.5/library/:/Users/.../vendor/"
57-
````
104+
```
58105
* Method 2: Alternatively, you can create a file called 'set_path.php' and set your paths there. If you chose to set the path using this option, the file set_path.php should look like:
59-
````
106+
```php
60107
<?php
61108
$path1 = '/Users/.../ZendFramework-2.4.5/library/';
62109
$path2 = '/Users/.../vendor/';
63110

64111
set_include_path($path1 . PATH_SEPARATOR . $path2);
65112
?>
66-
````
113+
```
67114
** If you use this option make sure to create the file in the same folder as the folder which contains the OX3_API_Client2.php file. Also make sure to require the file by adding the line "require_once 'set_path.php'; " to the top of the OX3_Api_Client2.php file.
68115

69116
#Authentication/Example Scripts
70117
Add this to your code to authenticate with Oauth:
71-
````
118+
```injectablephp
72119
<?php
73120
// If using Zend Framework 1
74121
require_once 'OX3_Api_Client.php';
75122
// if Using Zend Framework 2
76123
require_once 'OX3_Api_Client2.php';
124+
// if using the composer autoloader with the namespaced client
125+
require_once('vendor/autoload.php');
77126
78127
$uri = 'http://host';
79128
$email = '[email protected]';
@@ -85,61 +134,69 @@ $realm = '';
85134
// If using Zend Framework 1
86135
$client = new OX3_API_Client($uri, $email, $password, $key, $secret, $realm);
87136
// if Using Zend Framework 2
88-
$client = new OX3_API_Client2($uri, $email, $password, $key, $secret, $realm);
137+
$client = new OX3_API_Client2($uri, $email, $password, $key, $secret, $realm);
138+
// if using the new class
139+
$client = new \OpenX\PlatformAPI\OXApiClient($uri, $email, $password, $key, $secret, $realm);
89140
?>
90141
````
91142
** Note that when running the example scripts, OX3_Api_Client.php/OX3_Api_Client2.php must be in the same folder as the script. Also note that the example scripts contain some user configurable variables (besides the authentication section), which are described at the top of the scripts.
92143
93144
#Usage
94-
* To see the results in a friendly format on the command line, use the functions json_Decode, getBody, and print_r.
95-
Ex) $result = $client->get('/account');
96-
print_r(json_decode($result->getBody(), true));
145+
* To see the results in a friendly format on the command line, use the functions json_decode, getBody, and print_r.
146+
Ex.:
147+
148+
$result = $client->get('/account');
149+
print_r(json_decode($result->getBody(), true));
97150
98151
**GET REQUESTS:**
99152
* To get all current objects of a certain type, use the following request:
100-
````
153+
```injectablephp
101154
$result = $client->get('/"object_type"');
102-
Ex) $result = $client->get('/account');
103-
````
155+
// Example:
156+
$result = $client->get('/account');
157+
```
104158

105159
* To get the object(s) with a specific value for some attribute(s), pass in the value of the desired attribute as an array along with the path:
106-
````
160+
```injectablephp
107161
$query = array("attribute"=>"value");
108-
$result = $client->get('/object_type', $query')
109-
Ex) $query1 = array('name'=>'OpenX');
162+
$result = $client->get('/object_type', $query)
163+
// Example:
164+
$query1 = array('name'=>'OpenX');
110165
result1 = $client->get('/account', $query1);
111-
--> Returns the account(s) with the name OpenX
112-
````
166+
// --> Returns the account(s) with the name OpenX
167+
```
113168

114169
* Many fields have multiple options for what value they can take on. To see these options, use the following request:
115-
````
116-
$result = $client->get('/options/"field_options"')
117-
Ex) $content_types = $client->get('/options/content_type_options');
118-
````
170+
```injectablephp
171+
$result = $client->get('/options/:field_options')
172+
// Example:
173+
$content_types = $client->get('/options/content_type_options');
174+
```
119175

120176
**POST REQUESTS:**
121177
* To create an object, make a post request, passing in the path along with an array which includes the values of the fields for the object
122-
````
178+
```injectablephp
123179
$query = array(
124180
'account_uid'=>"...",
125181
'currency'=>"...",
126182
.
127183
.
128184
.
129185
'timezone'=>"...");
130-
$result = $client->post('/"object_type"/', $query);
131-
````
186+
$result = $client->post('/:object_type/', $query);
187+
```
132188

133189
**PUT REQUESTS:**
134190
* To update an object, make a put resquest, passing in the path along with an array which includes the parameters that are being updated
135-
````
136-
$query = array('timezone'=>'"updated_value"');
137-
$result = $client->put('/"object_type"/"object_uid"', $query);
138-
````
191+
```injectablephp
192+
$query = array('timezone'=>'updated_value');
193+
$result = $client->put('/:object_type/:object_uid', $query);
194+
```
139195

140196
**DELETE REQUESTS:**
141197
* To delete an object, make a delete request, passing in the path including the uids/id of the object that is to be deleted:
142-
````
143-
$result = $client->delete('/"object_type"/"object_uid"');
144-
Ex) $result = $client->delete('/site/6003a1c2-e000-fff1-8123-0c9a66');
145-
````
198+
```injectablephp
199+
$result = $client->delete('/:object_type/:object_uid');
200+
// Example:
201+
$result = $client->delete('/site/6003a1c2-e000-fff1-8123-0c9a66');
202+
```

composer-example/composer.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"repositories": [
3+
{
4+
"type": "vcs",
5+
"url": "https://github.com/openx/OX3-PHP-API-Client"
6+
}
7+
],
8+
"require": {
9+
"openx/ox3-php-api-client": "dev-main",
10+
"zendframework/zend-http": "^2.6",
11+
"zendframework/zendoauth": "^2.0",
12+
"zendframework/zendrest": "^2.0",
13+
"ext-json": "*"
14+
}
15+
}

composer-example/example.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
// init autoloader
4+
require_once('vendor/autoload.php');
5+
6+
$uri = getenv('OPENX_URI');
7+
$email = getenv('OPENX_EMAIL');
8+
$password = getenv('OPENX_PASSWORD');
9+
$key = getenv('OPENX_KEY');
10+
$secret = getenv('OPENX_SECRET');
11+
$realm = getenv('OPENX_REALM')?:'';
12+
13+
// grab a client
14+
$client = new \OpenX\PlatformAPI\OXApiClient($uri, $email, $password, $key, $secret, $realm);
15+
16+
// and make a request
17+
$response = $client->get('/session');
18+
$raw_body = $response->getBody();
19+
$session = json_decode($raw_body);
20+
print_r($session->user); // logs the correct user

composer.json

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
{
2-
"name": "openx/ox3-php-api-client",
3-
"description": "OpenX Platform API PHP CLIENT",
4-
"type": "package",
5-
"require": {
6-
"zendframework/zend-http": "^2.6",
7-
"zendframework/zendoauth": "^2.0",
8-
"zendframework/zendrest": "^2.0"
2+
"name": "openx/ox3-php-api-client",
3+
"description": "OpenX Platform API PHP CLIENT",
4+
"type": "package",
5+
"require": {
6+
"zendframework/zend-http": "^2.6",
7+
"zendframework/zendoauth": "^2.0",
8+
"zendframework/zendrest": "^2.0",
9+
"ext-json": "*"
10+
},
11+
"authors": [
12+
{
13+
"name": "fei.song",
14+
"email": "[email protected]"
915
},
10-
"authors": [
11-
{
12-
"name": "fei.song",
13-
"email": "[email protected]"
14-
}
15-
],
16-
"autoload": {
17-
"psr-0": {
18-
"OpenX\\PlatformAPI": "src"
19-
}
16+
{
17+
"name": "Cezar Pokorski",
18+
"email": "[email protected]"
2019
}
20+
],
21+
"autoload": {
22+
"psr-0": {
23+
"OpenX\\PlatformAPI": "src"
24+
}
25+
}
2126
}

examples/simple.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require_once __DIR__ ."/../vendor/autoload.php";
2+
require_once __DIR__ . "/../vendor/autoload.php";
33

44
use OpenX\PlatformAPI\OX3ApiClient2;
55

@@ -16,9 +16,9 @@
1616
$result = $client->get('/site');
1717

1818

19-
if ($result->getStatusCode() == 200) {
19+
if ($result->getStatusCode() === 200) {
2020
echo "Response: " . print_r(json_decode($result->getBody(), true), true) . "\n";
21-
} else {;
21+
} else {
2222
echo "Non-200 response:\ncode: " . $result->getStatus() . "\nmessage: " . $result->getMessage() . "\nbody: " . $result->getBody() . "\n";
2323
}
2424

0 commit comments

Comments
 (0)