Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 98c6a21

Browse files
v1.0
0 parents  commit 98c6a21

File tree

5 files changed

+641
-0
lines changed

5 files changed

+641
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

README.md

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
Omnisend PHP-SDK
2+
=============
3+
4+
Simple Omnisend API v3 wrapper in PHP.
5+
6+
Omnisend API v3 documentation can be found [here](https://api-docs.omnisend.com/).
7+
8+
Requires PHP > 5.3, cURL or ``allow_url_fopen`` to be enabled.
9+
10+
Installation
11+
------------
12+
13+
You can install Omnisend PHP-SDK by either using Composer either by downloading and including it manually.
14+
15+
#### Install using a composer
16+
17+
1. Run these commands:
18+
19+
```
20+
composer require omnisend/php-sdk
21+
composer install
22+
```
23+
24+
2. Add the autoloader to your application:
25+
```php
26+
require_once("vendor/autoload.php");
27+
```
28+
**Note:** check and correct, if needed, "vendor/autoload.php" path.
29+
30+
31+
#### Install manually
32+
33+
Download the `Omnisend.php` file and include it manually:
34+
35+
```php
36+
require_once('Omnisend.php');
37+
```
38+
**Note:** check and correct if needed "Omnisend.php" path.
39+
40+
Available methods & options
41+
--------
42+
**Creating instance with your API Key and options (optional)**
43+
```php
44+
$options = array(
45+
'timeout' => 30,
46+
'verifySSL' => false
47+
);
48+
$omnisend = new Omnisend('API-KEY', $options);
49+
```
50+
51+
Available options:
52+
53+
|Name|Type|Description|
54+
|---|---|---|
55+
|timeout|int|Timeout. If not passed - will be calculated depending on PHP max_execution_time
56+
|verifySSL|bool|Default - true. Enable (true) or disable (false) SSL verification.
57+
58+
**Available methods**
59+
60+
`endpoint` - endpoint url (ex. 'contacts', 'products/prod123'). See [documentation](https://api-docs.omnisend.com/) for available endpoints.
61+
62+
`queryParams` - array of query parameters
63+
64+
`fields` - array of fields
65+
66+
* getSnippet() - returns html snippet code
67+
* get(endpoint, queryParams) - make `GET` request.
68+
* push(endpoint, fields, queryParams) - makes `POST` request. If error occurs (resource exists in Omnisend) - makes `PUT` request. This method can be used if you don't know if item exists in Omnisend. `queryParams` - optional.
69+
* post(endpoint, fields, queryParams) - make `POST` request. Used to create new item in Omnisend. Will return an error if an item already exists in Omnisend. `queryParams` - optional.
70+
* put(endpoint, fields, queryParams) - make `PUT` request. Used to replace item in Omnisend. Will return an error if an item doesn't exists in Omnisend. `queryParams` - optional.
71+
* patch(endpoint, fields, queryParams) - make `PATCH` request. Used to update item in Omnisend. Will return an error if an item doesn't exists in Omnisend. `queryParams` - optional.
72+
* delete(endpoint, queryParams) - make `DELETE` request. Used to delete item in Omnisend. Will return an error if an item doesn't exists in Omnisend. `queryParams` - optional.
73+
74+
Responses
75+
--------
76+
77+
Each method will return `false` in case of an error, `array` (see [documentation](https://api-docs.omnisend.com/) for responses) or `true` (for empty body (204) responses) in case of a success.
78+
79+
So you can easily check if a request was successful:
80+
```php
81+
$cart = $omnisend->delete('carts/cart-123');
82+
if ($cart) {
83+
//request was successful
84+
} else {
85+
//there was an error
86+
}
87+
```
88+
89+
In case of a failed request, you can get an error description with `lastError()`:
90+
```php
91+
var_dump($omnisend->lastError());
92+
```
93+
94+
Output will be an array with:
95+
* error - error description
96+
* statusCode - HTTP response [status code](https://api-docs.omnisend.com/v3/overview/responses)
97+
* fields - optional - array of missing required, incorrect or incorrectly formatted `fields` (passed with a request)
98+
99+
Example:
100+
101+
```php
102+
array {
103+
["error"]=> "2 error(s) found. Check 'fields' array for details."
104+
["statusCode"]=> 400
105+
["fields"]=>
106+
array {
107+
[0]=>
108+
array {
109+
[0]=> "cartSum: field required but not found in Json"
110+
}
111+
[1]=>
112+
array {
113+
[0]=> "currency: field required but not found in Json"
114+
}
115+
}
116+
}
117+
```
118+
119+
Examples
120+
--------
121+
122+
1. Create an instance with your API key and options (optional)
123+
124+
```php
125+
$omnisend = new Omnisend('your-api-key');
126+
```
127+
128+
2. Make a request, for example, create a new contact in Omnisend:
129+
130+
```php
131+
$contacts = $omnisend->post(
132+
'contacts',
133+
array(
134+
"email" => "[email protected]",
135+
"firstName" => "Vanessa",
136+
"lastName" => "Kensington",
137+
"status" => "subscribed",
138+
"statusDate" => "2018-12-11T10:29:43+00:00"
139+
)
140+
);
141+
```
142+
3. Check if a request was successful:
143+
144+
```php
145+
if ($contacts) {
146+
//request was successful
147+
148+
//print response
149+
print_r($contacts);
150+
//get contactID from response
151+
$contactID = $contacts['contactID'];
152+
} else {
153+
//there was an error
154+
print_r($omnisend->lastError());
155+
}
156+
```
157+
158+
See more examples in `examples/examples.php`

composer.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "omnisend/php-sdk",
3+
"description": "Simple to use Omnisend API v3 wrapper",
4+
"type": "library",
5+
"version": "1.0",
6+
"minimum-stability": "dev",
7+
"require": {
8+
"php": ">=5.3.0",
9+
"ext-curl": "*"
10+
},
11+
"autoload": {
12+
"psr-0": {
13+
"Omnisend": "src/"
14+
}
15+
}
16+
}

examples/examples.php

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
require_once __DIR__ . "/../src/Omnisend.php";
3+
4+
//change 'your-api-key' to you're API Key
5+
$omnisend = new Omnisend('your-api-key', array('timeout' => 15));
6+
7+
// Create new contact
8+
$contactID = "";
9+
$contacts = $omnisend->post(
10+
'contacts',
11+
array(
12+
"email" => "[email protected]",
13+
"firstName" => "Vanessa",
14+
"lastName" => "Kensington",
15+
"status" => "subscribed",
16+
"statusDate" => "2018-12-11T10:29:43+00:00",
17+
)
18+
);
19+
if ($contacts) {
20+
print_r($contacts);
21+
$contactID = $contacts['contactID'];
22+
} else {
23+
print_r($omnisend->lastError());
24+
}
25+
26+
// updated in example 1 created contact
27+
if ($contactID) {
28+
$contacts = $omnisend->patch(
29+
'contacts/' . $contactID,
30+
array("country" => "United Kingdom")
31+
);
32+
if ($contacts) {
33+
print_r($contacts);
34+
} else {
35+
print_r($omnisend->lastError());
36+
}
37+
}
38+
39+
// Get contacts with filter (query parameters)
40+
$contacts = $omnisend->get('contacts', array("limit" => 10, "offset" => 0));
41+
if ($contacts) {
42+
print_r($contacts);
43+
} else {
44+
print_r($omnisend->lastError());
45+
}
46+
47+
// Create or update product
48+
$product = $omnisend->push(
49+
'products',
50+
array(
51+
"productID" => "prod666",
52+
"title" => "Container for mojo",
53+
"status" => "inStock",
54+
"description" => "Super quality metal container",
55+
"currency" => "USD",
56+
"productUrl" => "http://www.example.com/products/prod-666",
57+
"vendor" => "Nanotech",
58+
"type" => "containers",
59+
"createdAt" => "2000-01-01T00:00:01Z",
60+
"images" => [
61+
array(
62+
"imageID" => "prod-66-img1",
63+
"url" => "http://www.example.com/images/products/prod-666.png",
64+
"isDefault" => true,
65+
"variants" => [
66+
"prod666",
67+
],
68+
),
69+
],
70+
"tags" => [
71+
"container",
72+
"metal",
73+
],
74+
"categoryIDs" => [
75+
"cat123",
76+
"cat1267",
77+
],
78+
"variants" => array(
79+
[
80+
"variantID" => "prod666",
81+
"title" => "Container for mojo",
82+
"sku" => "123",
83+
"status" => "inStock",
84+
"price" => 66666,
85+
"oldPrice" => 75000,
86+
"productUrl" => "http://www.example.com/products/prod-666",
87+
"imageID" => "prod-66-img1",
88+
"customFields" => [
89+
"protectionClass" => "IP99",
90+
],
91+
],
92+
),
93+
)
94+
);
95+
if ($product) {
96+
//Product successfully saved
97+
print_r($product);
98+
$productID = $product['productID'];
99+
} else {
100+
print_r($omnisend->lastError());
101+
}
102+
103+
//delete product
104+
if (!isset($productID)) {
105+
$productID = "prod666";
106+
}
107+
108+
$product = $omnisend->delete('products/' . $productID);
109+
if ($product) {
110+
echo "Product successfully deleted.";
111+
} else {
112+
print_r($omnisend->lastError());
113+
}
114+
115+
//Output Omnisend snippet
116+
echo $omnisend->getSnippet();

0 commit comments

Comments
 (0)