├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── example └── index.php └── src ├── ApiClient.php ├── ApiClientException.php ├── Contracts ├── ApiInterface.php └── TokenStorageInterface.php └── Storage ├── FileStorage.php ├── MemcacheStorage.php ├── MemcachedStorage.php └── SessionStorage.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2015 SendPulse 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SendPulse REST client library 2 | ================ 3 | 4 | [![License](http://poser.pugx.org/sendpulse/rest-api/license)](https://packagist.org/packages/sendpulse/rest-api) 5 | [![Total Downloads](http://poser.pugx.org/sendpulse/rest-api/downloads)](https://packagist.org/packages/sendpulse/rest-api) 6 | [![PHP Version Require](http://poser.pugx.org/sendpulse/rest-api/require/php)](https://packagist.org/packages/sendpulse/rest-api) 7 | 8 | A simple SendPulse REST client library and example for PHP. 9 | 10 | API Documentation [https://sendpulse.com/api](https://sendpulse.com/api) 11 | 12 | 13 | ### Requirements 14 | 15 | - php: >=7.1.0 16 | - ext-json: * 17 | - ext-curl: * 18 | 19 | 20 | ### Installation 21 | 22 | Via Composer: 23 | 24 | ```bash 25 | composer require sendpulse/rest-api 26 | ``` 27 | 28 | ### Example 29 | 30 | ```php 31 | get('addressbooks', [ 53 | 'limit' => 100, 54 | 'offset' => 0 55 | ]); 56 | 57 | var_dump($addressBooks); 58 | } catch (ApiClientException $e) { 59 | var_dump([ 60 | 'message' => $e->getMessage(), 61 | 'http_code' => $e->getCode(), 62 | 'response' => $e->getResponse(), 63 | 'curl_errors' => $e->getCurlErrors(), 64 | 'headers' => $e->getHeaders() 65 | ]); 66 | } 67 | 68 | 69 | /* 70 | * Send POST request 71 | * 72 | * Example: Add new email to mailing lists 73 | */ 74 | try { 75 | $addEmailsResult = $apiClient->post('addressbooks/33333/emails', [ 76 | 'emails' => [ 77 | [ 78 | 'email' => 'test_email@test.com', 79 | 'variables' => [ 80 | 'phone' => '+123456789', 81 | 'my_var' => 'my_var_value' 82 | ] 83 | ], [ 84 | 'email' => 'email_test@test.com', 85 | 'variables' => [ 86 | 'phone' => '+987654321', 87 | 'my_var' => 'my_var_value' 88 | ] 89 | ] 90 | ] 91 | ]); 92 | 93 | var_dump($addEmailsResult); 94 | } catch (ApiClientException $e) { 95 | var_dump([ 96 | 'message' => $e->getMessage(), 97 | 'http_code' => $e->getCode(), 98 | 'response' => $e->getResponse(), 99 | 'curl_errors' => $e->getCurlErrors(), 100 | 'headers' => $e->getHeaders() 101 | ]); 102 | } 103 | 104 | 105 | /* 106 | * Send PUT request 107 | * 108 | * Example: Edit a Mailing List 109 | */ 110 | try { 111 | $addEmailsResult = $apiClient->put('addressbooks/33333', [ 112 | 'name' => "New Name" 113 | ]); 114 | 115 | var_dump($addEmailsResult); 116 | } catch (ApiClientException $e) { 117 | var_dump([ 118 | 'message' => $e->getMessage(), 119 | 'http_code' => $e->getCode(), 120 | 'response' => $e->getResponse(), 121 | 'curl_errors' => $e->getCurlErrors(), 122 | 'headers' => $e->getHeaders() 123 | ]); 124 | } 125 | 126 | 127 | /* 128 | * Send PATCH request 129 | * 130 | * Example: Edit Scheduled Campaign 131 | */ 132 | try { 133 | $editScheduledCampaignResult = $apiClient->patch('campaigns/333333', [ 134 | "name" => "My_API_campaign", 135 | "sender_name" => "sender", 136 | "sender_email" => "sender@test.com", 137 | "subject" => "Hello customer", 138 | "template_id" => 351594, 139 | "send_date" => "2023-10-21 11:45:00" 140 | ]); 141 | 142 | var_dump($editScheduledCampaignResult); 143 | } catch (\Sendpulse\RestApi\ApiClientException $e) { 144 | var_dump([ 145 | 'message' => $e->getMessage(), 146 | 'http_code' => $e->getCode(), 147 | 'response' => $e->getResponse(), 148 | 'curl_errors' => $e->getCurlErrors(), 149 | 'headers' => $e->getHeaders() 150 | ]); 151 | } 152 | 153 | 154 | /* 155 | * Send DELETE request 156 | * 157 | * Example: Delete Emails from a Mailing List 158 | */ 159 | try { 160 | $removeEmailsResult = $apiClient->delete('addressbooks/33333/emails', [ 161 | 'emails' => ['test@test.com'] 162 | ]); 163 | 164 | var_dump($removeEmailsResult); 165 | } catch (ApiClientException $e) { 166 | var_dump([ 167 | 'message' => $e->getMessage(), 168 | 'http_code' => $e->getCode(), 169 | 'response' => $e->getResponse(), 170 | 'curl_errors' => $e->getCurlErrors(), 171 | 'headers' => $e->getHeaders() 172 | ]); 173 | } 174 | 175 | 176 | /* 177 | * Example: Start Automation360 event 178 | */ 179 | try { 180 | $startEventResult = $apiClient->post('events/name/my_event_name', [ 181 | "email" => "test@test.com", 182 | "phone" => "+123456789", 183 | "products" => [ 184 | [ 185 | "id" => "id value", 186 | "name" => "name value" 187 | ] 188 | ] 189 | ]); 190 | 191 | var_dump($startEventResult); 192 | } catch (ApiClientException $e) { 193 | var_dump([ 194 | 'message' => $e->getMessage(), 195 | 'http_code' => $e->getCode(), 196 | 'response' => $e->getResponse(), 197 | 'curl_errors' => $e->getCurlErrors(), 198 | 'headers' => $e->getHeaders() 199 | ]); 200 | } 201 | 202 | 203 | /** 204 | * Example: Crm create a new deal 205 | */ 206 | try { 207 | $crmCreateDeal = $apiClient->post('crm/v1/deals', [ 208 | "pipelineId" => 0, 209 | "stepId" => 0, 210 | "responsibleId" => 0, 211 | "name" => "string", 212 | "price" => 0, 213 | "currency" => "string", 214 | "sourceId" => 0, 215 | "contact" => [ 216 | 0 217 | ], 218 | "attributes" => [ 219 | [ 220 | "attributeId" => 0, 221 | "value" => "string" 222 | ] 223 | ], 224 | "attachments" => [ 225 | "https://link-to-file.com/file.jpg" 226 | ] 227 | ]); 228 | 229 | var_dump($crmCreateDeal); 230 | } catch (ApiClientException $e) { 231 | var_dump([ 232 | 'message' => $e->getMessage(), 233 | 'http_code' => $e->getCode(), 234 | 'response' => $e->getResponse(), 235 | 'curl_errors' => $e->getCurlErrors(), 236 | 'headers' => $e->getHeaders() 237 | ]); 238 | } 239 | 240 | 241 | /** 242 | * Example: Whatsapp send a template message to the specified contact 243 | */ 244 | try { 245 | $sendTemplateByPhoneResult = $apiClient->post('whatsapp/contacts/sendTemplateByPhone', [ 246 | "bot_id" => "xxxxxxxxxxxxxxxxxxxxxxxx", 247 | "phone" => "380931112233", 248 | "template" => [ 249 | "name" => "thanks_for_buying", 250 | "language" => [ 251 | "code" => "en" 252 | ], 253 | "components" => [] 254 | ] 255 | ]); 256 | 257 | var_dump($sendTemplateByPhoneResult); 258 | } catch (ApiClientException $e) { 259 | var_dump([ 260 | 'message' => $e->getMessage(), 261 | 'http_code' => $e->getCode(), 262 | 'response' => $e->getResponse(), 263 | 'curl_errors' => $e->getCurlErrors(), 264 | 'headers' => $e->getHeaders() 265 | ]); 266 | } 267 | 268 | 269 | ``` 270 | 271 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sendpulse/rest-api", 3 | "description": "Sendpulse REST API client library", 4 | "keywords": [ 5 | "sendpulse", 6 | "rest", 7 | "api" 8 | ], 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Maksym Ustymenko", 13 | "email": "m.ustymenko@sendpulse.com" 14 | }, 15 | { 16 | "name": "Alexey Moroz", 17 | "email": "a.moroz@sendpulse.com" 18 | }, 19 | { 20 | "name": "Evgeniy Bilovol", 21 | "email": "e.bilovol@sendpulse.com" 22 | } 23 | ], 24 | "require": { 25 | "php": ">=7.1.0", 26 | "ext-json": "*", 27 | "ext-curl": "*" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Sendpulse\\RestApi\\": "src/" 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /example/index.php: -------------------------------------------------------------------------------- 1 | $e->getMessage(), 26 | 'http_code' => $e->getCode(), 27 | 'response' => $e->getResponse(), 28 | 'curl_errors' => $e->getCurlErrors(), 29 | 'headers' => $e->getHeaders() 30 | ]); 31 | } 32 | 33 | 34 | /** 35 | * Catch error response 36 | */ 37 | try { 38 | $errorResponse = $apiClient->get('addressbooks/404'); 39 | var_dump($errorResponse); 40 | } catch (ApiClientException $e) { 41 | var_dump([ 42 | 'message' => $e->getMessage(), 43 | 'http_code' => $e->getCode(), 44 | 'response' => $e->getResponse(), 45 | 'curl_errors' => $e->getCurlErrors(), 46 | 'headers' => $e->getHeaders() 47 | ]); 48 | } 49 | 50 | 51 | /** 52 | * Get a List of Mailing Lists 53 | * @link https://sendpulse.com/integrations/api/bulk-email#lists-list 54 | */ 55 | try { 56 | $addressBooks = $apiClient->get('addressbooks', [ 57 | 'limit' => 100, 58 | 'offset' => 0 59 | ]); 60 | 61 | var_dump($addressBooks); 62 | } catch (ApiClientException $e) { 63 | var_dump([ 64 | 'message' => $e->getMessage(), 65 | 'http_code' => $e->getCode(), 66 | 'response' => $e->getResponse(), 67 | 'curl_errors' => $e->getCurlErrors(), 68 | 'headers' => $e->getHeaders() 69 | ]); 70 | } 71 | 72 | 73 | /** 74 | * Add Emails to a Mailing List 75 | * @link https://sendpulse.com/integrations/api/bulk-email#add-email 76 | */ 77 | try { 78 | $addEmailsResult = $apiClient->post('addressbooks/33333/emails', [ 79 | 'emails' => [ 80 | [ 81 | 'email' => 'test_email@test.com', 82 | 'variables' => [ 83 | 'phone' => '+123456789', 84 | 'my_var' => 'my_var_value' 85 | ] 86 | ], [ 87 | 'email' => 'email_test@test.com', 88 | 'variables' => [ 89 | 'phone' => '+987654321', 90 | 'my_var' => 'my_var_value' 91 | ] 92 | ] 93 | ] 94 | ]); 95 | 96 | var_dump($addEmailsResult); 97 | } catch (ApiClientException $e) { 98 | var_dump([ 99 | 'message' => $e->getMessage(), 100 | 'http_code' => $e->getCode(), 101 | 'response' => $e->getResponse(), 102 | 'curl_errors' => $e->getCurlErrors(), 103 | 'headers' => $e->getHeaders() 104 | ]); 105 | } 106 | 107 | 108 | /** 109 | * SMTP: send mail 110 | * @link https://sendpulse.com/integrations/api/smtp#send-email-smtp 111 | */ 112 | try { 113 | $smtpSendMailResult = $apiClient->post('smtp/emails', [ 114 | 'email' => [ 115 | 'html' => base64_encode('

Hello!

'), 116 | 'text' => 'text', 117 | 'subject' => 'Mail subject', 118 | 'from' => [ 119 | 'name' => 'API package test', 120 | 'email' => 'from@test.com', 121 | ], 122 | 'to' => [ 123 | [ 124 | 'name' => 'to', 125 | 'email' => 'to@test.com', 126 | ] 127 | ], 128 | 'bcc' => [ 129 | [ 130 | 'name' => 'bcc', 131 | 'email' => 'bcc@test.com', 132 | ] 133 | ], 134 | 'attachments_binary' => [ 135 | 'attach_image.jpg' => base64_encode(file_get_contents('../storage/attach_image.jpg')) 136 | ], 137 | ] 138 | ]); 139 | 140 | var_dump($smtpSendMailResult); 141 | } catch (ApiClientException $e) { 142 | var_dump([ 143 | 'message' => $e->getMessage(), 144 | 'http_code' => $e->getCode(), 145 | 'response' => $e->getResponse(), 146 | 'curl_errors' => $e->getCurlErrors(), 147 | 'headers' => $e->getHeaders() 148 | ]); 149 | } 150 | 151 | /** 152 | * Edit a Mailing List 153 | * @link https://sendpulse.com/integrations/api/bulk-email#edit-list 154 | */ 155 | try { 156 | $addEmailsResult = $apiClient->put('addressbooks/33333', [ 157 | 'name' => "New Name" 158 | ]); 159 | 160 | var_dump($addEmailsResult); 161 | } catch (ApiClientException $e) { 162 | var_dump([ 163 | 'message' => $e->getMessage(), 164 | 'http_code' => $e->getCode(), 165 | 'response' => $e->getResponse(), 166 | 'curl_errors' => $e->getCurlErrors(), 167 | 'headers' => $e->getHeaders() 168 | ]); 169 | } 170 | 171 | 172 | /** 173 | * Edit Scheduled Campaign 174 | * @link https://sendpulse.com/integrations/api/bulk-email#edit-campaign 175 | */ 176 | try { 177 | $editScheduledCampaignResult = $apiClient->patch('campaigns/333333', [ 178 | "name" => "My_API_campaign", 179 | "sender_name" => "sender", 180 | "sender_email" => "sender@test.com", 181 | "subject" => "Hello customer", 182 | "template_id" => 351594, 183 | "send_date" => "2023-10-21 11:45:00" 184 | ]); 185 | 186 | var_dump($editScheduledCampaignResult); 187 | } catch (ApiClientException $e) { 188 | var_dump([ 189 | 'message' => $e->getMessage(), 190 | 'http_code' => $e->getCode(), 191 | 'response' => $e->getResponse(), 192 | 'curl_errors' => $e->getCurlErrors(), 193 | 'headers' => $e->getHeaders() 194 | ]); 195 | } 196 | 197 | 198 | /** 199 | * Start A360 event 200 | * @link https://login.sendpulse.com/emailservice/events/ 201 | */ 202 | try { 203 | $startEventResult = $apiClient->post('events/name/my_event_name', [ 204 | "email" => "test@test.com", 205 | "phone" => "+123456789", 206 | "products" => [ 207 | [ 208 | "id" => "id value", 209 | "name" => "name value" 210 | ] 211 | ] 212 | ]); 213 | 214 | var_dump($startEventResult); 215 | } catch (ApiClientException $e) { 216 | var_dump([ 217 | 'message' => $e->getMessage(), 218 | 'http_code' => $e->getCode(), 219 | 'response' => $e->getResponse(), 220 | 'curl_errors' => $e->getCurlErrors(), 221 | 'headers' => $e->getHeaders() 222 | ]); 223 | } 224 | 225 | 226 | /** 227 | * Delete Emails from a Mailing List 228 | * @link https://sendpulse.com/integrations/api/bulk-email#delete-email 229 | */ 230 | try { 231 | $removeEmailsResult = $apiClient->delete('addressbooks/33333/emails', [ 232 | 'emails' => ['test@test.com'] 233 | ]); 234 | 235 | var_dump($removeEmailsResult); 236 | } catch (ApiClientException $e) { 237 | var_dump([ 238 | 'message' => $e->getMessage(), 239 | 'http_code' => $e->getCode(), 240 | 'response' => $e->getResponse(), 241 | 'curl_errors' => $e->getCurlErrors(), 242 | 'headers' => $e->getHeaders() 243 | ]); 244 | } 245 | 246 | 247 | /** 248 | * Send SMS 249 | * @link https://sendpulse.com/integrations/api/bulk-sms#create-campaign-to-list 250 | */ 251 | try { 252 | $smsSendResult = $apiClient->post('sms/send', [ 253 | "sender" => "my_sender", 254 | "phones" => [ 255 | 380683850429 256 | ], 257 | "body" => "api" 258 | ]); 259 | 260 | var_dump($smsSendResult); 261 | } catch (ApiClientException $e) { 262 | var_dump([ 263 | 'message' => $e->getMessage(), 264 | 'http_code' => $e->getCode(), 265 | 'response' => $e->getResponse(), 266 | 'curl_errors' => $e->getCurlErrors(), 267 | 'headers' => $e->getHeaders() 268 | ]); 269 | } 270 | 271 | 272 | /** 273 | * Crm get pipelines 274 | * @link https://sendpulse.com/integrations/api/crm#/Pipelines/get_pipelines 275 | */ 276 | try { 277 | $crmPipelines = $apiClient->get('crm/v1/pipelines'); 278 | 279 | var_dump($crmPipelines); 280 | } catch (ApiClientException $e) { 281 | var_dump([ 282 | 'message' => $e->getMessage(), 283 | 'http_code' => $e->getCode(), 284 | 'response' => $e->getResponse(), 285 | 'curl_errors' => $e->getCurlErrors(), 286 | 'headers' => $e->getHeaders() 287 | ]); 288 | } 289 | 290 | 291 | /** 292 | * Crm get contacts 293 | * @link https://sendpulse.com/integrations/api/crm#/Contacts/post_contacts_get_list 294 | */ 295 | try { 296 | $crmContacts = $apiClient->post('crm/v1/contacts/get-list'); 297 | 298 | var_dump($crmContacts); 299 | } catch (ApiClientException $e) { 300 | var_dump([ 301 | 'message' => $e->getMessage(), 302 | 'http_code' => $e->getCode(), 303 | 'response' => $e->getResponse(), 304 | 'curl_errors' => $e->getCurlErrors(), 305 | 'headers' => $e->getHeaders() 306 | ]); 307 | } -------------------------------------------------------------------------------- /src/ApiClient.php: -------------------------------------------------------------------------------- 1 | userId = $userId; 70 | $this->secret = $secret; 71 | $this->tokenStorage = $tokenStorage; 72 | $hashName = md5($userId . '::' . $secret); 73 | 74 | /** load token from storage */ 75 | $this->token = $this->tokenStorage->get($hashName); 76 | if (empty($this->token) && !$this->getToken()) { 77 | throw new ApiClientException('Could not connect to api, check your ID and SECRET'); 78 | } 79 | } 80 | 81 | /** 82 | * Get token and store it 83 | * @link https://sendpulse.com/integrations/api#auth 84 | * @return bool 85 | * @throws ApiClientException 86 | */ 87 | private function getToken(): bool 88 | { 89 | $tokenResponse = $this->sendRequest('oauth/access_token', self::METHOD_POST, [ 90 | 'grant_type' => 'client_credentials', 91 | 'client_id' => $this->userId, 92 | 'client_secret' => $this->secret, 93 | ], false); 94 | 95 | if (empty($tokenResponse['access_token'])) { 96 | return false; 97 | } 98 | 99 | $this->refreshToken = false; 100 | $this->token = $tokenResponse['access_token']; 101 | 102 | $hashName = md5($this->userId . '::' . $this->secret); 103 | 104 | return $this->tokenStorage->set($hashName, $this->token); 105 | } 106 | 107 | /** 108 | * Form and send request to API service 109 | * @param string $path 110 | * @param string $method 111 | * @param array $data 112 | * @param bool $useToken 113 | * @return array|null 114 | * @throws ApiClientException 115 | */ 116 | protected function sendRequest(string $path, string $method = self::METHOD_GET, array $data = [], bool $useToken = true): ?array 117 | { 118 | $url = $this->apiUrl . '/' . $path; 119 | $curl = curl_init(); 120 | 121 | $headers = [ 122 | 'Accept: application/json', 123 | 'Content-Type: application/json', 124 | 'Expect:' 125 | ]; 126 | 127 | if ($useToken && !empty($this->token)) { 128 | $headers[] = 'Authorization: ' . self::TOKEN_TYPE_BEARER . ' ' . $this->token; 129 | } 130 | 131 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 132 | 133 | switch ($method) { 134 | case self::METHOD_POST: 135 | curl_setopt($curl, CURLOPT_POST, count($data)); 136 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 137 | break; 138 | case self::METHOD_PUT: 139 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::METHOD_PUT); 140 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 141 | break; 142 | case self::METHOD_PATCH: 143 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::METHOD_PATCH); 144 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 145 | break; 146 | case self::METHOD_DELETE: 147 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::METHOD_DELETE); 148 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 149 | break; 150 | default: 151 | if (!empty($data)) { 152 | $url .= '?' . http_build_query($data); 153 | } 154 | } 155 | 156 | curl_setopt($curl, CURLOPT_URL, $url); 157 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 158 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 159 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 160 | curl_setopt($curl, CURLOPT_HEADER, true); 161 | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 300); 162 | curl_setopt($curl, CURLOPT_TIMEOUT, 300); 163 | 164 | $response = curl_exec($curl); 165 | $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 166 | $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 167 | $responseBody = json_decode(substr($response, $headerSize), true); 168 | $responseHeaders = substr($response, 0, $headerSize); 169 | $curlErrors = curl_error($curl); 170 | 171 | curl_close($curl); 172 | 173 | if ($httpCode >= 400) { 174 | if ($httpCode === 401 && !$this->refreshToken) { 175 | $this->refreshToken = true; 176 | $this->getToken(); 177 | $responseBody = $this->sendRequest($path, $method, $data); 178 | } else { 179 | throw new ApiClientException( 180 | 'Request ' . $method . ' ' . $url . ' failed!', 181 | $httpCode, 182 | null, 183 | $responseBody, 184 | $responseHeaders, 185 | $curlErrors 186 | ); 187 | } 188 | } 189 | 190 | return empty($responseBody) ? null : $responseBody; 191 | } 192 | 193 | /** 194 | * Send GET request 195 | * @param string $path 196 | * @param array $data 197 | * @param bool $useToken 198 | * @return array|null 199 | * @throws ApiClientException 200 | */ 201 | public function get(string $path, array $data = [], bool $useToken = true): ?array 202 | { 203 | return $this->sendRequest($path, self::METHOD_GET, $data, $useToken); 204 | } 205 | 206 | /** 207 | * Send POST request 208 | * @param string $path 209 | * @param array $data 210 | * @param bool $useToken 211 | * @return array|null 212 | * @throws ApiClientException 213 | */ 214 | public function post(string $path, array $data = [], bool $useToken = true): ?array 215 | { 216 | return $this->sendRequest($path, self::METHOD_POST, $data, $useToken); 217 | } 218 | 219 | /** 220 | * Send PUT request 221 | * @param string $path 222 | * @param array $data 223 | * @param bool $useToken 224 | * @return array|null 225 | * @throws ApiClientException 226 | */ 227 | public function put(string $path, array $data = [], bool $useToken = true): ?array 228 | { 229 | return $this->sendRequest($path, self::METHOD_PUT, $data, $useToken); 230 | } 231 | 232 | /** 233 | * Send PATCH request 234 | * @param string $path 235 | * @param array $data 236 | * @param bool $useToken 237 | * @return array|null 238 | * @throws ApiClientException 239 | */ 240 | public function patch(string $path, array $data = [], bool $useToken = true): ?array 241 | { 242 | return $this->sendRequest($path, self::METHOD_PATCH, $data, $useToken); 243 | } 244 | 245 | /** 246 | * Send DELETE request 247 | * @param string $path 248 | * @param array $data 249 | * @param bool $useToken 250 | * @return array|null 251 | * @throws ApiClientException 252 | */ 253 | public function delete(string $path, array $data = [], bool $useToken = true): ?array 254 | { 255 | return $this->sendRequest($path, self::METHOD_DELETE, $data, $useToken); 256 | } 257 | 258 | /** 259 | * Create address book 260 | * @link https://sendpulse.com/integrations/api/bulk-email#create-list 261 | * @param string $bookName 262 | * @return array|null 263 | * @throws ApiClientException 264 | * @deprecated 265 | * @see ApiClient::post() 266 | */ 267 | public function createAddressBook(string $bookName): ?array 268 | { 269 | return $this->post('addressbooks', ['bookName' => $bookName]); 270 | } 271 | 272 | /** 273 | * Edit address book name 274 | * @link https://sendpulse.com/integrations/api/bulk-email#edit-list 275 | * @param int $id 276 | * @param string $newName 277 | * @return array|null 278 | * @throws ApiClientException 279 | * @deprecated 280 | * @see ApiClient::put() 281 | */ 282 | public function editAddressBook(int $id, string $newName): ?array 283 | { 284 | return $this->put('addressbooks/' . $id, ['name' => $newName]); 285 | } 286 | 287 | /** 288 | * Remove address book 289 | * @link https://sendpulse.com/integrations/api/bulk-email#delete-list 290 | * @param int $id 291 | * @return array|null 292 | * @throws ApiClientException 293 | * @deprecated 294 | * @see ApiClient::get() 295 | */ 296 | public function removeAddressBook(int $id): ?array 297 | { 298 | return $this->delete('addressbooks/' . $id); 299 | } 300 | 301 | /** 302 | * Get list of address books 303 | * @link https://sendpulse.com/integrations/api/bulk-email#lists-list 304 | * @param int|null $limit 305 | * @param int|null $offset 306 | * @throws ApiClientException 307 | * @deprecated 308 | * @see ApiClient::get() 309 | */ 310 | public function listAddressBooks(int $limit = null, int $offset = null): ?array 311 | { 312 | $data = array(); 313 | if (null !== $limit) { 314 | $data['limit'] = $limit; 315 | } 316 | if (null !== $offset) { 317 | $data['offset'] = $offset; 318 | } 319 | 320 | return $this->get('addressbooks', $data); 321 | } 322 | 323 | /** 324 | * Get information about book 325 | * @link https://sendpulse.com/integrations/api/bulk-email#list-info 326 | * @param int $id 327 | * @return array|null 328 | * @throws ApiClientException 329 | * @deprecated 330 | * @see ApiClient::get() 331 | */ 332 | public function getBookInfo(int $id): ?array 333 | { 334 | return $this->get('addressbooks/' . $id); 335 | } 336 | 337 | /** 338 | * Get variables from book 339 | * @link https://sendpulse.com/integrations/api/bulk-email#variables 340 | * @param int $id 341 | * @return array|null 342 | * @throws ApiClientException 343 | * @deprecated 344 | * @see ApiClient::get() 345 | */ 346 | public function getBookVariables(int $id): ?array 347 | { 348 | return $this->get('addressbooks/' . $id . '/variables'); 349 | } 350 | 351 | /** 352 | * Change variable by user email 353 | * @link https://sendpulse.com/integrations/api/bulk-email#email-change-variable 354 | * @param int $bookID 355 | * @param string $email User email 356 | * @param array $vars User vars in [key=>value] format 357 | * @return array|null 358 | * @throws ApiClientException 359 | * @deprecated 360 | * @see ApiClient::post() 361 | */ 362 | public function updateEmailVariables(int $bookID, string $email, array $vars): ?array 363 | { 364 | $data = ['email' => $email, 'variables' => []]; 365 | foreach ($vars as $name => $val) { 366 | $data['variables'][] = ['name' => $name, 'value' => $val]; 367 | } 368 | 369 | return $this->post('addressbooks/' . $bookID . '/emails/variable', $data); 370 | } 371 | 372 | /** 373 | * List email addresses from book 374 | * @link https://sendpulse.com/integrations/api/bulk-email#lists-emails 375 | * @param int $id 376 | * @param int|null $limit 377 | * @param int|null $offset 378 | * @return array|null 379 | * @throws ApiClientException 380 | * @deprecated 381 | * @see ApiClient::get() 382 | */ 383 | public function getEmailsFromBook(int $id, int $limit = null, int $offset = null): ?array 384 | { 385 | $data = []; 386 | if (null !== $limit) { 387 | $data['limit'] = $limit; 388 | } 389 | if (null !== $offset) { 390 | $data['offset'] = $offset; 391 | } 392 | 393 | return $this->get('addressbooks/' . $id . '/emails', $data); 394 | } 395 | 396 | /** 397 | * Add new emails to address book 398 | * @link https://sendpulse.com/integrations/api/bulk-email#add-email 399 | * @param int $bookID 400 | * @param array $emails 401 | * @param array $additionalParams 402 | * @return array|null 403 | * @throws ApiClientException 404 | * @deprecated 405 | * @see ApiClient::post() 406 | */ 407 | public function addEmails(int $bookID, array $emails, array $additionalParams = []): ?array 408 | { 409 | if (empty($bookID) || empty($emails)) { 410 | throw new ApiClientException('Empty book id or emails'); 411 | } 412 | 413 | $data = [ 414 | 'emails' => $emails, 415 | ]; 416 | 417 | if ($additionalParams) { 418 | $data = array_merge($data, $additionalParams); 419 | } 420 | 421 | return $this->post('addressbooks/' . $bookID . '/emails', $data); 422 | } 423 | 424 | /** 425 | * Remove email addresses from book 426 | * @link https://sendpulse.com/integrations/api/bulk-email#delete-email 427 | * @param int $bookID 428 | * @param array $emails 429 | * @return array|null 430 | * @throws ApiClientException 431 | * @deprecated 432 | * @see ApiClient::delete() 433 | */ 434 | public function removeEmails(int $bookID, array $emails): ?array 435 | { 436 | return $this->delete('addressbooks/' . $bookID . '/emails', [ 437 | 'emails' => $emails 438 | ]); 439 | } 440 | 441 | /** 442 | * Get information about email address from book 443 | * @link https://sendpulse.com/integrations/api/bulk-email#email-info 444 | * @param int $bookID 445 | * @param string $email 446 | * @return array|null 447 | * @throws ApiClientException 448 | * @deprecated 449 | * @see ApiClient::get() 450 | */ 451 | public function getEmailInfo(int $bookID, string $email): ?array 452 | { 453 | return $this->get('addressbooks/' . $bookID . '/emails/' . $email); 454 | } 455 | 456 | /** 457 | * Get cost of campaign based on address book 458 | * @link https://sendpulse.com/integrations/api/bulk-email#campaign-cost 459 | * @param int $bookID 460 | * @return array|null 461 | * @throws ApiClientException 462 | * @deprecated 463 | * @see ApiClient::get() 464 | */ 465 | public function campaignCost(int $bookID): ?array 466 | { 467 | return $this->get('addressbooks/' . $bookID . '/cost'); 468 | } 469 | 470 | /** 471 | * Get list of campaigns 472 | * @link https://sendpulse.com/integrations/api/bulk-email#campaigns-list 473 | * @param int|null $limit 474 | * @param int|null $offset 475 | * @return array|null 476 | * @throws ApiClientException 477 | * @deprecated 478 | * @see ApiClient::get() 479 | */ 480 | public function listCampaigns(int $limit = null, int $offset = null): ?array 481 | { 482 | $data = []; 483 | if (!empty($limit)) { 484 | $data['limit'] = $limit; 485 | } 486 | if (!empty($offset)) { 487 | $data['offset'] = $offset; 488 | } 489 | 490 | return $this->get('campaigns', $data); 491 | } 492 | 493 | /** 494 | * Get information about campaign 495 | * @link https://sendpulse.com/integrations/api/bulk-email#campaign-info 496 | * @param int $id 497 | * @return array|null 498 | * @throws ApiClientException 499 | * @deprecated 500 | * @see ApiClient::get() 501 | */ 502 | public function getCampaignInfo(int $id): ?array 503 | { 504 | return $this->get('campaigns/' . $id); 505 | } 506 | 507 | /** 508 | * Get campaign statistic by countries 509 | * @link https://sendpulse.com/integrations/api/bulk-email#stat-countries 510 | * @param int $id 511 | * @return array|null 512 | * @throws ApiClientException 513 | * @deprecated 514 | * @see ApiClient::get() 515 | */ 516 | public function campaignStatByCountries(int $id): ?array 517 | { 518 | return $this->get('campaigns/' . $id . '/countries'); 519 | } 520 | 521 | /** 522 | * Get campaign statistic by referrals 523 | * @link https://sendpulse.com/integrations/api/bulk-email#stat-referral 524 | * @param int $id 525 | * @return array|null 526 | * @throws ApiClientException 527 | * @deprecated 528 | * @see ApiClient::get() 529 | */ 530 | public function campaignStatByReferrals(int $id): ?array 531 | { 532 | return $this->get('campaigns/' . $id . '/referrals'); 533 | } 534 | 535 | /** 536 | * Create new campaign 537 | * @link https://sendpulse.com/integrations/api/bulk-email#create-campaign 538 | * @param string $senderName 539 | * @param string $senderEmail 540 | * @param string $subject 541 | * @param $bodyOrTemplateId 542 | * @param int $bookId 543 | * @param string $name 544 | * @param array $attachments 545 | * @param string $type 546 | * @param bool $useTemplateId 547 | * @param string $sendDate 548 | * @param int|null $segmentId 549 | * @param array $attachmentsBinary 550 | * @return array|null 551 | * @throws ApiClientException 552 | * @deprecated 553 | * @see ApiClient::post() 554 | */ 555 | public function createCampaign( 556 | string $senderName, 557 | string $senderEmail, 558 | string $subject, 559 | $bodyOrTemplateId, 560 | int $bookId, 561 | string $name = '', 562 | array $attachments = [], 563 | string $type = '', 564 | bool $useTemplateId = false, 565 | string $sendDate = '', 566 | int $segmentId = null, 567 | array $attachmentsBinary = [] 568 | ): ?array 569 | { 570 | if (empty($senderName) || empty($senderEmail) || empty($subject) || empty($bodyOrTemplateId) || empty($bookId)) { 571 | throw new ApiClientException('Not all data.'); 572 | } 573 | 574 | if ($useTemplateId) { 575 | $paramName = 'template_id'; 576 | $paramValue = $bodyOrTemplateId; 577 | } else { 578 | $paramName = 'body'; 579 | $paramValue = base64_encode($bodyOrTemplateId); 580 | } 581 | 582 | $data = [ 583 | 'sender_name' => $senderName, 584 | 'sender_email' => $senderEmail, 585 | 'subject' => $subject, 586 | $paramName => $paramValue, 587 | 'list_id' => $bookId, 588 | 'name' => $name, 589 | 'type' => $type, 590 | ]; 591 | 592 | if (!empty($attachments)) { 593 | $data['attachments'] = $attachments; 594 | } elseif (!empty($attachmentsBinary)) { 595 | $data['attachments_binary'] = $attachmentsBinary; 596 | } 597 | 598 | if (!empty($sendDate)) { 599 | $data['send_date'] = $sendDate; 600 | } 601 | 602 | if (!empty($segmentId)) { 603 | $data['segment_id'] = $segmentId; 604 | } 605 | 606 | return $this->post('campaigns', $data); 607 | } 608 | 609 | /** 610 | * Cancel campaign 611 | * @link https://sendpulse.com/integrations/api/bulk-email#cancel-send 612 | * @param int $id 613 | * @return array|null 614 | * @throws ApiClientException 615 | * @deprecated 616 | * @see ApiClient::delete() 617 | */ 618 | public function cancelCampaign(int $id): ?array 619 | { 620 | return $this->delete('campaigns/' . $id); 621 | } 622 | 623 | /** 624 | * List all senders 625 | * @link https://sendpulse.com/integrations/api/bulk-email#senders-list 626 | * @throws ApiClientException 627 | * @deprecated 628 | * @see ApiClient::get() 629 | */ 630 | public function listSenders(): ?array 631 | { 632 | return $this->get('senders'); 633 | } 634 | 635 | /** 636 | * List SMS senders 637 | * @link https://sendpulse.com/integrations/api/bulk-sms#get-senders 638 | * @throws ApiClientException 639 | * @deprecated 640 | * @see ApiClient::get() 641 | */ 642 | public function listSMSSenders(): ?array 643 | { 644 | return $this->get('sms/senders'); 645 | } 646 | 647 | /** 648 | * Add new sender 649 | * @link https://sendpulse.com/integrations/api/bulk-email#add-sender 650 | * @param $senderName 651 | * @param $senderEmail 652 | * @return array|null 653 | * @throws ApiClientException 654 | * @deprecated 655 | * @see ApiClient::post() 656 | */ 657 | public function addSender($senderName, $senderEmail): ?array 658 | { 659 | return $this->post('senders', [ 660 | 'email' => $senderEmail, 661 | 'name' => $senderName, 662 | ]); 663 | } 664 | 665 | /** 666 | * Remove sender 667 | * @link https://sendpulse.com/integrations/api/bulk-email#delete-sender 668 | * @param string $email 669 | * @return array|null 670 | * @throws ApiClientException 671 | * @deprecated 672 | * @see ApiClient::delete() 673 | */ 674 | public function removeSender(string $email): ?array 675 | { 676 | return $this->delete('senders', [ 677 | 'email' => $email 678 | ]); 679 | } 680 | 681 | /** 682 | * Activate sender using code 683 | * @link https://sendpulse.com/integrations/api/bulk-email#activate-sender 684 | * @param string $email 685 | * @param string $code 686 | * @return array|null 687 | * @throws ApiClientException 688 | * @deprecated 689 | * @see ApiClient::post() 690 | */ 691 | public function activateSender(string $email, string $code): ?array 692 | { 693 | return $this->post('senders/' . $email . '/code', [ 694 | 'code' => $code, 695 | ]); 696 | } 697 | 698 | /** 699 | * Request mail with activation code 700 | * @link https://sendpulse.com/integrations/api/bulk-email#code 701 | * @param string $email 702 | * @return array|null 703 | * @throws ApiClientException 704 | * @deprecated 705 | * @see ApiClient::get() 706 | */ 707 | public function getSenderActivationMail(string $email): ?array 708 | { 709 | return $this->get('senders/' . $email . '/code'); 710 | } 711 | 712 | /** 713 | * Get global information about email 714 | * @link https://sendpulse.com/integrations/api/bulk-email#email-info 715 | * @param string $email 716 | * @return array|null 717 | * @throws ApiClientException 718 | * @deprecated 719 | * @see ApiClient::get() 720 | */ 721 | public function getEmailGlobalInfo(string $email): ?array 722 | { 723 | return $this->get('emails/' . $email); 724 | } 725 | 726 | /** 727 | * Get global information about list of emails 728 | * @link https://sendpulse.com/integrations/api/bulk-email#emails_info 729 | * @param array $emails Emails list 730 | * @return array|null 731 | * @throws ApiClientException 732 | * @deprecated 733 | * @see ApiClient::post() 734 | */ 735 | public function getEmailsGlobalInfo(array $emails): ?array 736 | { 737 | return $this->post('emails', $emails); 738 | } 739 | 740 | /** 741 | * Remove email from all books 742 | * @link https://sendpulse.com/integrations/api/bulk-email#email-delete 743 | * @param string $email 744 | * @return array|null 745 | * @throws ApiClientException 746 | * @deprecated 747 | * @see ApiClient::delete() 748 | */ 749 | public function removeEmailFromAllBooks(string $email): ?array 750 | { 751 | return $this->delete('emails/' . $email); 752 | } 753 | 754 | /** 755 | * Get email statistic by all campaigns 756 | * @link https://sendpulse.com/integrations/api/bulk-email#email-stat 757 | * @param $email 758 | * @return array|null 759 | * @throws ApiClientException 760 | * @deprecated 761 | * @see ApiClient::get() 762 | */ 763 | public function emailStatByCampaigns($email): ?array 764 | { 765 | return $this->get('emails/' . $email . '/campaigns'); 766 | } 767 | 768 | /** 769 | * Get all emails from blacklist 770 | * @link https://sendpulse.com/integrations/api/bulk-email#view-blacklist 771 | * @throws ApiClientException 772 | * @see ApiClient::get() 773 | * @deprecated 774 | */ 775 | public function getBlackList(): ?array 776 | { 777 | return $this->get('blacklist'); 778 | } 779 | 780 | /** 781 | * Add email to blacklist 782 | * @link https://sendpulse.com/integrations/api/bulk-email#add-blacklist 783 | * @param string $emails string with emails, separator - , 784 | * @param string $comment 785 | * @return array|null 786 | * @throws ApiClientException 787 | * @deprecated 788 | * @see ApiClient::post() 789 | */ 790 | public function addToBlackList(string $emails, string $comment = ''): ?array 791 | { 792 | return $this->post('blacklist', [ 793 | 'emails' => base64_encode($emails), 794 | 'comment' => $comment, 795 | ]); 796 | } 797 | 798 | /** 799 | * Remove emails from blacklist 800 | * @link https://sendpulse.com/integrations/api/bulk-email#delete-blacklist 801 | * @param string $emails string with emails, separator - , 802 | * @return array|null 803 | * @throws ApiClientException 804 | * @deprecated 805 | * @see ApiClient::delete() 806 | */ 807 | public function removeFromBlackList(string $emails): ?array 808 | { 809 | return $this->delete('blacklist', [ 810 | 'emails' => base64_encode($emails) 811 | ]); 812 | 813 | } 814 | 815 | /** 816 | * Get balance 817 | * @link https://sendpulse.com/integrations/api/bulk-email#get-balance 818 | * @param string $currency 819 | * @return array|null 820 | * @throws ApiClientException 821 | * @deprecated 822 | * @see ApiClient::get() 823 | */ 824 | public function getBalance(string $currency = ''): ?array 825 | { 826 | $currency = strtoupper($currency); 827 | $url = 'balance'; 828 | if (!empty($currency)) { 829 | $url .= '/' . strtoupper($currency); 830 | } 831 | 832 | return $this->get($url); 833 | } 834 | 835 | /** 836 | * SMTP: get list of emails 837 | * @link https://sendpulse.com/integrations/api/smtp#get-emails-list-smtp 838 | * @param int $limit 839 | * @param int $offset 840 | * @param string $fromDate 841 | * @param string $toDate 842 | * @param string $sender 843 | * @param string $recipient 844 | * @param string $country 845 | * @return array|null 846 | * @throws ApiClientException 847 | * @deprecated 848 | * @see ApiClient::get() 849 | */ 850 | public function smtpListEmails(int $limit = 0, int $offset = 0, string $fromDate = '', string $toDate = '', string $sender = '', string $recipient = '', string $country = 'off'): ?array 851 | { 852 | return $this->get('smtp/emails', [ 853 | 'limit' => $limit, 854 | 'offset' => $offset, 855 | 'from' => $fromDate, 856 | 'to' => $toDate, 857 | 'sender' => $sender, 858 | 'recipient' => $recipient, 859 | 'country' => $country, 860 | ]); 861 | } 862 | 863 | /** 864 | * Get information about email by id 865 | * @link https://sendpulse.com/integrations/api/smtp#email-info-smtp 866 | * @param $id 867 | * @return array|null 868 | * @throws ApiClientException 869 | * @deprecated 870 | * @see ApiClient::get() 871 | */ 872 | public function smtpGetEmailInfoById($id): ?array 873 | { 874 | return $this->get('smtp/emails/' . $id); 875 | } 876 | 877 | /** 878 | * SMTP: get list of unsubscribed emails 879 | * @link https://sendpulse.com/integrations/api/smtp#unsubscribed 880 | * @param int|null $limit 881 | * @param int|null $offset 882 | * @return array|null 883 | * @throws ApiClientException 884 | * @deprecated 885 | * @see ApiClient::get() 886 | */ 887 | public function smtpListUnsubscribed(int $limit = null, int $offset = null): ?array 888 | { 889 | $data = array(); 890 | if (null !== $limit) { 891 | $data['limit'] = $limit; 892 | } 893 | if (null !== $offset) { 894 | $data['offset'] = $offset; 895 | } 896 | 897 | return $this->get('smtp/unsubscribe', $data); 898 | } 899 | 900 | /** 901 | * SMTP: add emails to unsubscribe list 902 | * @link https://sendpulse.com/integrations/api/smtp#unsubscribe-smtp 903 | * @param array $emails 904 | * @return array|null 905 | * @throws ApiClientException 906 | * @deprecated 907 | * @see ApiClient::post() 908 | */ 909 | public function smtpUnsubscribeEmails(array $emails): ?array 910 | { 911 | return $this->post('smtp/unsubscribe', [ 912 | 'emails' => $emails, 913 | ]); 914 | } 915 | 916 | /** 917 | * SMTP: remove emails from unsubscribe list 918 | * @link https://sendpulse.com/integrations/api/smtp#delete-smtp 919 | * @param $emails 920 | * @return array|null 921 | * @throws ApiClientException 922 | * @deprecated 923 | * @see ApiClient::delete() 924 | */ 925 | public function smtpRemoveFromUnsubscribe($emails): ?array 926 | { 927 | return $this->delete('smtp/unsubscribe', [ 928 | 'emails' => $emails 929 | ]); 930 | 931 | } 932 | 933 | /** 934 | * Get list of IP 935 | * @link https://sendpulse.com/integrations/api/smtp#ip-smtp 936 | * @return array|null 937 | * @throws ApiClientException 938 | * @deprecated 939 | * @see ApiClient::get() 940 | */ 941 | public function smtpListIP(): ?array 942 | { 943 | return $this->get('smtp/ips'); 944 | } 945 | 946 | /** 947 | * SMTP: send mail 948 | * @link https://sendpulse.com/integrations/api/smtp#send-email-smtp 949 | * @param array $email 950 | * @return array|null 951 | * @throws ApiClientException 952 | * @deprecated 953 | * @see ApiClient::post() 954 | */ 955 | public function smtpSendMail(array $email): ?array 956 | { 957 | $emailData = $email; 958 | if (isset($email['html'])) { 959 | $emailData['html'] = base64_encode($email['html']); 960 | } 961 | 962 | return $this->post('smtp/emails', [ 963 | 'email' => $emailData, 964 | ]); 965 | } 966 | 967 | /** 968 | * Get list of push campaigns 969 | * @link https://sendpulse.com/integrations/api/web-push#get-push-list 970 | * @param int|null $limit 971 | * @param int|null $offset 972 | * @return array|null 973 | * @throws ApiClientException 974 | * @deprecated 975 | * @see ApiClient::get() 976 | */ 977 | public function pushListCampaigns(int $limit = null, int $offset = null): ?array 978 | { 979 | $data = []; 980 | if (null !== $limit) { 981 | $data['limit'] = $limit; 982 | } 983 | if (null !== $offset) { 984 | $data['offset'] = $offset; 985 | } 986 | 987 | return $this->get('push/tasks', $data); 988 | } 989 | 990 | /** 991 | * Get list of websites 992 | * @link https://sendpulse.com/integrations/api/web-push#get-websites-list 993 | * @param int|null $limit 994 | * @param int|null $offset 995 | * @return array|null 996 | * @throws ApiClientException 997 | * @deprecated 998 | * @see ApiClient::get() 999 | */ 1000 | public function pushListWebsites(int $limit = null, int $offset = null): ?array 1001 | { 1002 | $data = array(); 1003 | if (null !== $limit) { 1004 | $data['limit'] = $limit; 1005 | } 1006 | if (null !== $offset) { 1007 | $data['offset'] = $offset; 1008 | } 1009 | 1010 | return $this->get('push/websites', $data); 1011 | } 1012 | 1013 | /** 1014 | * Get amount of websites 1015 | * @link https://sendpulse.com/integrations/api/web-push#get-websites-number 1016 | * @return array|null 1017 | * @throws ApiClientException 1018 | * @deprecated 1019 | * @see ApiClient::get() 1020 | */ 1021 | public function pushCountWebsites(): ?array 1022 | { 1023 | return $this->get('push/websites/total'); 1024 | } 1025 | 1026 | /** 1027 | * Get list of all variables for website 1028 | * @link https://sendpulse.com/integrations/api/web-push#get-variables-list 1029 | * @param int $websiteId 1030 | * @return array|null 1031 | * @throws ApiClientException 1032 | * @deprecated 1033 | * @see ApiClient::get() 1034 | */ 1035 | public function pushListWebsiteVariables(int $websiteId): ?array 1036 | { 1037 | return $this->get('push/websites/' . $websiteId . '/variables'); 1038 | } 1039 | 1040 | /** 1041 | * Get list of subscriptions for the website 1042 | * @link https://sendpulse.com/integrations/api/web-push#get-subscribers-list 1043 | * @param int $websiteID 1044 | * @param int|null $limit 1045 | * @param int|null $offset 1046 | * @return array|null 1047 | * @throws ApiClientException 1048 | * @deprecated 1049 | * @see ApiClient::get() 1050 | */ 1051 | public function pushListWebsiteSubscriptions(int $websiteID, int $limit = null, int $offset = null): ?array 1052 | { 1053 | $data = []; 1054 | if (null !== $limit) { 1055 | $data['limit'] = $limit; 1056 | } 1057 | if (null !== $offset) { 1058 | $data['offset'] = $offset; 1059 | } 1060 | 1061 | return $this->get('push/websites/' . $websiteID . '/subscriptions', $data); 1062 | } 1063 | 1064 | /** 1065 | * Get amount of subscriptions for the site 1066 | * @link https://sendpulse.com/integrations/api/web-push#get-subscribers-number 1067 | * @param $websiteID 1068 | * @return array|null 1069 | * @throws ApiClientException 1070 | * @deprecated 1071 | * @see ApiClient::get() 1072 | */ 1073 | public function pushCountWebsiteSubscriptions($websiteID): ?array 1074 | { 1075 | return $this->get('push/websites/' . $websiteID . '/subscriptions/total'); 1076 | } 1077 | 1078 | /** 1079 | * Set state for subscription 1080 | * @link https://sendpulse.com/integrations/api/web-push#activate-subscriber 1081 | * @param int $subscriptionID 1082 | * @param int $stateValue 1083 | * @return array|null 1084 | * @throws ApiClientException 1085 | * @deprecated 1086 | * @see ApiClient::post() 1087 | */ 1088 | public function pushSetSubscriptionState(int $subscriptionID, int $stateValue): ?array 1089 | { 1090 | return $this->post('push/subscriptions/state', [ 1091 | 'id' => $subscriptionID, 1092 | 'state' => $stateValue, 1093 | ]); 1094 | } 1095 | 1096 | /** 1097 | * Get common website info 1098 | * @link https://sendpulse.com/integrations/api/web-push#site_info 1099 | * @param int $websiteId 1100 | * @return array|null 1101 | * @throws ApiClientException 1102 | * @deprecated 1103 | * @see ApiClient::get() 1104 | */ 1105 | public function pushGetWebsiteInfo(int $websiteId): ?array 1106 | { 1107 | return $this->get('push/websites/info/' . $websiteId); 1108 | } 1109 | 1110 | /** 1111 | * Create new push campaign 1112 | * @link https://sendpulse.com/integrations/api/web-push#create-push 1113 | * @param array $taskInfo 1114 | * @param array $additionalParams 1115 | * @return array|null 1116 | * @throws ApiClientException 1117 | * @deprecated 1118 | * @see ApiClient::post() 1119 | */ 1120 | public function createPushTask(array $taskInfo, array $additionalParams = []): ?array 1121 | { 1122 | $data = $taskInfo; 1123 | if (!isset($data['ttl'])) { 1124 | $data['ttl'] = 0; 1125 | } 1126 | if (empty($data['title']) || empty($data['website_id']) || empty($data['body'])) { 1127 | throw new ApiClientException('Not all data'); 1128 | } 1129 | if ($additionalParams) { 1130 | foreach ($additionalParams as $key => $val) { 1131 | $data[$key] = $val; 1132 | } 1133 | } 1134 | 1135 | return $this->post('push/tasks', $data); 1136 | } 1137 | 1138 | /** 1139 | * Get integration code for Push Notifications. 1140 | * @link https://sendpulse.com/integrations/api/web-push#get_js 1141 | * @param int $websiteID 1142 | * @return array|null 1143 | * @throws ApiClientException 1144 | * @deprecated 1145 | * @see ApiClient::get() 1146 | */ 1147 | public function getPushIntegrationCode(int $websiteID): ?array 1148 | { 1149 | return $this->get('push/websites/' . $websiteID . '/code'); 1150 | } 1151 | 1152 | /** 1153 | * Get stats for push campaign 1154 | * @link https://sendpulse.com/integrations/api/web-push#statistics 1155 | * @param int $campaignID 1156 | * @return array|null 1157 | * @throws ApiClientException 1158 | * @deprecated 1159 | * @see ApiClient::get() 1160 | */ 1161 | public function getPushCampaignStat(int $campaignID): ?array 1162 | { 1163 | return $this->get('push/tasks/' . $campaignID); 1164 | } 1165 | 1166 | /** 1167 | * @Author Maksym Dzhym m.jim@sendpulse.com 1168 | * @param string $eventName 1169 | * @param array $variables 1170 | * @return array|null 1171 | * @throws ApiClientException 1172 | * @deprecated 1173 | * @see ApiClient::post() 1174 | */ 1175 | public function startEventAutomation360(string $eventName, array $variables): ?array 1176 | { 1177 | if (!array_key_exists('email', $variables) && !array_key_exists('phone', $variables)) { 1178 | throw new ApiClientException('Email and phone is empty'); 1179 | } 1180 | 1181 | return $this->post('events/name/' . $eventName, $variables); 1182 | } 1183 | 1184 | /** 1185 | * Add phones to addressbook 1186 | * @link https://sendpulse.com/integrations/api/bulk-sms#add-telephone 1187 | * @param int $bookID 1188 | * @param array $phones 1189 | * @return array|null 1190 | * @throws ApiClientException 1191 | * @deprecated 1192 | * @see ApiClient::post() 1193 | */ 1194 | public function addPhones(int $bookID, array $phones): ?array 1195 | { 1196 | return $this->post('sms/numbers', [ 1197 | 'addressBookId' => $bookID, 1198 | 'phones' => $phones 1199 | ]); 1200 | } 1201 | 1202 | /** 1203 | * Add phones with variables to addressbook 1204 | * @link https://sendpulse.com/integrations/api/bulk-sms#add-phone-variable 1205 | * @param int $bookID 1206 | * @param array $phonesWithVariables 1207 | * @return array|null 1208 | * @throws ApiClientException 1209 | * @deprecated 1210 | * @see ApiClient::post() 1211 | */ 1212 | public function addPhonesWithVariables(int $bookID, array $phonesWithVariables): ?array 1213 | { 1214 | return $this->post('sms/numbers/variables', [ 1215 | 'addressBookId' => $bookID, 1216 | 'phones' => $phonesWithVariables 1217 | ]); 1218 | } 1219 | 1220 | /** 1221 | * Update variables for phones 1222 | * @link https://sendpulse.com/integrations/api/bulk-sms#update-variable 1223 | * @param int $bookID 1224 | * @param array $phones 1225 | * @param array $variables 1226 | * @return array|null 1227 | * @throws ApiClientException 1228 | * @deprecated 1229 | * @see ApiClient::put() 1230 | */ 1231 | public function updatePhoneVaribales(int $bookID, array $phones, array $variables): ?array 1232 | { 1233 | return $this->put('sms/numbers', [ 1234 | 'addressBookId' => $bookID, 1235 | 'phones' => $phones, 1236 | 'variables' => $variables 1237 | ]); 1238 | 1239 | } 1240 | 1241 | /** 1242 | * Delete phones from book 1243 | * @link https://sendpulse.com/integrations/api/bulk-sms#delete-telephone 1244 | * @param int $bookID 1245 | * @param array $phones 1246 | * @return array|null 1247 | * @throws ApiClientException 1248 | * @deprecated 1249 | * @see ApiClient::delete() 1250 | */ 1251 | public function deletePhones(int $bookID, array $phones): ?array 1252 | { 1253 | return $this->delete('sms/numbers', [ 1254 | 'addressBookId' => $bookID, 1255 | 'phones' => $phones 1256 | ]); 1257 | } 1258 | 1259 | /** 1260 | * get information about phone number 1261 | * @info https://sendpulse.com/integrations/api/bulk-sms#retrieve-info-number 1262 | * @param int $bookID 1263 | * @param string $phoneNumber 1264 | * @return array|null 1265 | * @throws ApiClientException 1266 | * @deprecated 1267 | * @see ApiClient::get() 1268 | */ 1269 | public function getPhoneInfo(int $bookID, string $phoneNumber): ?array 1270 | { 1271 | return $this->get('sms/numbers/info/' . $bookID . '/' . $phoneNumber); 1272 | } 1273 | 1274 | /** 1275 | * Add phones to blacklist 1276 | * @link https://sendpulse.com/integrations/api/bulk-sms#add-to-blacklist 1277 | * @param array $phones 1278 | * @return array|null 1279 | * @throws ApiClientException 1280 | * @deprecated 1281 | * @see ApiClient::post() 1282 | */ 1283 | public function addPhonesToBlacklist(array $phones): ?array 1284 | { 1285 | return $this->post('sms/black_list', [ 1286 | 'phones' => $phones 1287 | ]); 1288 | } 1289 | 1290 | /** 1291 | * Delete phones from blacklist 1292 | * @link https://sendpulse.com/integrations/api/bulk-sms#delete-from-blacklist 1293 | * @param array $phones 1294 | * @return array|null 1295 | * @throws ApiClientException 1296 | * @deprecated 1297 | * @see ApiClient::delete() 1298 | */ 1299 | public function removePhonesFromBlacklist(array $phones): ?array 1300 | { 1301 | return $this->delete('sms/black_list', [ 1302 | 'phones' => $phones 1303 | ]); 1304 | } 1305 | 1306 | /** 1307 | * Get list of phones from blacklist 1308 | * @link https://sendpulse.com/integrations/api/bulk-sms#view-blacklist 1309 | * @return array|null 1310 | * @throws ApiClientException 1311 | * @deprecated 1312 | * @see ApiClient::get() 1313 | */ 1314 | public function getPhonesFromBlacklist(): ?array 1315 | { 1316 | return $this->get('sms/black_list'); 1317 | } 1318 | 1319 | /** 1320 | * Create sms campaign based on phones in book 1321 | * @link https://sendpulse.com/integrations/api/bulk-sms#create-campaign 1322 | * @param int $bookID 1323 | * @param array $params 1324 | * @param array $additionalParams 1325 | * @return array|null 1326 | * @throws ApiClientException 1327 | * @deprecated 1328 | * @see ApiClient::post() 1329 | */ 1330 | public function sendSmsByBook(int $bookID, array $params, array $additionalParams = []): ?array 1331 | { 1332 | $data = [ 1333 | 'addressBookId' => $bookID 1334 | ]; 1335 | 1336 | $data = array_merge($data, $params); 1337 | 1338 | if ($additionalParams) { 1339 | $data = array_merge($data, $additionalParams); 1340 | } 1341 | 1342 | return $this->post('sms/campaigns', $data); 1343 | } 1344 | 1345 | /** 1346 | * Create sms campaign based on list 1347 | * @link https://sendpulse.com/integrations/api/bulk-sms#create-campaign-to-list 1348 | * @param array $phones 1349 | * @param array $params 1350 | * @param array $additionalParams 1351 | * @return array|null 1352 | * @throws ApiClientException 1353 | * @deprecated 1354 | * @see ApiClient::post() 1355 | */ 1356 | public function sendSmsByList(array $phones, array $params, array $additionalParams): ?array 1357 | { 1358 | $data = [ 1359 | 'phones' => $phones 1360 | ]; 1361 | 1362 | $data = array_merge($data, $params); 1363 | 1364 | if ($additionalParams) { 1365 | $data = array_merge($data, $additionalParams); 1366 | } 1367 | 1368 | return $this->post('sms/send', $data); 1369 | } 1370 | 1371 | /** 1372 | * List sms campaigns 1373 | * @link https://sendpulse.com/integrations/api/bulk-sms#retrieve-campaign-by-date 1374 | * @param array $params 1375 | * @return array|null 1376 | * @throws ApiClientException 1377 | * @deprecated 1378 | * @see ApiClient::get() 1379 | */ 1380 | public function listSmsCampaigns(array $params = []): ?array 1381 | { 1382 | return $this->get('sms/campaigns/list', $params); 1383 | } 1384 | 1385 | /** 1386 | * Get info about sms campaign 1387 | * @link https://sendpulse.com/integrations/api/bulk-sms#retrieve-campaign-info 1388 | * @param int $campaignID 1389 | * @return array|null 1390 | * @throws ApiClientException 1391 | * @deprecated 1392 | * @see ApiClient::get() 1393 | */ 1394 | public function getSmsCampaignInfo(int $campaignID): ?array 1395 | { 1396 | return $this->get('sms/campaigns/info/' . $campaignID); 1397 | } 1398 | 1399 | /** 1400 | * Cancel SMS campaign 1401 | * @link https://sendpulse.com/integrations/api/bulk-sms#cancel-campaign 1402 | * @param int $campaignID 1403 | * @return array|null 1404 | * @throws ApiClientException 1405 | * @deprecated 1406 | * @see ApiClient::put() 1407 | */ 1408 | public function cancelSmsCampaign(int $campaignID): ?array 1409 | { 1410 | return $this->put('sms/campaigns/cancel/' . $campaignID); 1411 | } 1412 | 1413 | /** 1414 | * Get SMS campaign cost based on book or simple list 1415 | * @link https://sendpulse.com/integrations/api/bulk-sms#cost 1416 | * @param array $params 1417 | * @param array $additionalParams 1418 | * @return array|null 1419 | * @throws ApiClientException 1420 | * @deprecated 1421 | * @see ApiClient::get() 1422 | */ 1423 | public function getSmsCampaignCost(array $params, array $additionalParams = []): ?array 1424 | { 1425 | if (!isset($params['addressBookId']) && !isset($params['phones'])) { 1426 | throw new ApiClientException('You mast pass phones list or addressbook ID'); 1427 | } 1428 | 1429 | if ($additionalParams) { 1430 | $params = array_merge($params, $additionalParams); 1431 | } 1432 | 1433 | return $this->get('sms/campaigns/cost', $params); 1434 | } 1435 | 1436 | /** 1437 | * Delete SMS campaign 1438 | * @link https://sendpulse.com/integrations/api/bulk-sms#delete-campaign 1439 | * @param int $campaignID 1440 | * @return array|null 1441 | * @throws ApiClientException 1442 | * @deprecated 1443 | * @see ApiClient::delete() 1444 | */ 1445 | public function deleteSmsCampaign(int $campaignID): ?array 1446 | { 1447 | return $this->delete('sms/campaigns', ['id' => $campaignID]); 1448 | } 1449 | 1450 | 1451 | } -------------------------------------------------------------------------------- /src/ApiClientException.php: -------------------------------------------------------------------------------- 1 | response = $responseBody; 44 | $this->headers = $headers; 45 | $this->curlErrors = $curlErrors; 46 | 47 | parent::__construct($message, $code, $previous); 48 | } 49 | 50 | /** 51 | * @return array 52 | */ 53 | public function getResponse(): array 54 | { 55 | return $this->response; 56 | } 57 | 58 | /** 59 | * @return string|null 60 | */ 61 | public function getHeaders(): ?string 62 | { 63 | return $this->headers; 64 | } 65 | 66 | /** 67 | * @return string|null 68 | */ 69 | public function getCurlErrors(): ?string 70 | { 71 | return $this->curlErrors; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/Contracts/ApiInterface.php: -------------------------------------------------------------------------------- 1 | storageFolder = $storageFolder; 21 | } 22 | 23 | /** 24 | * @param string $key 25 | * @param string $token 26 | * @return bool 27 | */ 28 | public function set(string $key, string $token): bool 29 | { 30 | $tokenFile = fopen($this->storageFolder . $key, 'wb'); 31 | fwrite($tokenFile, $token); 32 | 33 | return fclose($tokenFile); 34 | } 35 | 36 | /** 37 | * @param string $key 38 | * @return string|null 39 | */ 40 | public function get(string $key): ?string 41 | { 42 | $filePath = $this->storageFolder . $key; 43 | 44 | return file_exists($filePath) ? file_get_contents($filePath) : null; 45 | } 46 | 47 | /** 48 | * @param string $key 49 | * @return bool 50 | */ 51 | public function delete(string $key): bool 52 | { 53 | $filePath = $this->storageFolder . $key; 54 | 55 | return file_exists($filePath) && unlink($filePath); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Storage/MemcacheStorage.php: -------------------------------------------------------------------------------- 1 | instance = new Memcache(); 30 | if ($persistent) { 31 | $this->instance->pconnect($host, $port); 32 | } else { 33 | $this->instance->connect($host, $port); 34 | } 35 | } 36 | 37 | /** 38 | * @return MemcacheStorage|null 39 | */ 40 | public function getInstance() 41 | { 42 | return $this->instance; 43 | } 44 | 45 | /** 46 | * @return int 47 | */ 48 | public function getKeyTtl(): int 49 | { 50 | return $this->keyTtl; 51 | } 52 | 53 | /** 54 | * @param int $keyTtl 55 | * 56 | * @return MemcacheStorage 57 | */ 58 | public function setKeyTtl(int $keyTtl): MemcacheStorage 59 | { 60 | $this->keyTtl = $keyTtl; 61 | 62 | return $this; 63 | } 64 | 65 | /** 66 | * @param string $key 67 | * @param string $token 68 | * @return bool 69 | */ 70 | public function set(string $key, string $token): bool 71 | { 72 | return $this->instance->set($key, $token, false, $this->keyTtl); 73 | } 74 | 75 | /** 76 | * @param string $key 77 | * @return string|null 78 | */ 79 | public function get(string $key): ?string 80 | { 81 | $token = $this->instance->get($key); 82 | 83 | return empty($token) ? null : $token; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/Storage/MemcachedStorage.php: -------------------------------------------------------------------------------- 1 | instance = new Memcached($persistentKey); 34 | $this->instance->addServer($host, $port); 35 | } 36 | 37 | /** 38 | * @return MemcachedStorage|null 39 | */ 40 | public function getInstance() 41 | { 42 | return $this->instance; 43 | } 44 | 45 | /** 46 | * @return int 47 | */ 48 | public function getKeyTtl(): int 49 | { 50 | return $this->keyTtl; 51 | } 52 | 53 | /** 54 | * @param int $keyTtl 55 | * 56 | * @return MemcachedStorage 57 | */ 58 | public function setKeyTtl(int $keyTtl): MemcachedStorage 59 | { 60 | $this->keyTtl = $keyTtl; 61 | 62 | return $this; 63 | } 64 | 65 | /** 66 | * @param $key string 67 | * @param string $token 68 | * 69 | * @return void 70 | */ 71 | public function set(string $key, string $token): bool 72 | { 73 | return $this->instance->set($key, $token, $this->keyTtl); 74 | } 75 | 76 | /** 77 | * @param string $key 78 | * @return string|null 79 | */ 80 | public function get(string $key): ?string 81 | { 82 | $token = $this->instance->get($key); 83 | 84 | return empty($token) ? null : $token; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Storage/SessionStorage.php: -------------------------------------------------------------------------------- 1 |