|
| 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 | + |
| 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` |
0 commit comments