Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Infoflot;
  4.  
  5. class GetResponse
  6. {
  7.     const X_AUTH_TOKEN = 'hvjldi9exj0huhjpgiloq2zp0ujp3i4n';
  8.     const X_DOMAIN     = 'email.infoflot.com';
  9.     const API_LINK     = 'https://api3.getresponse360.pl/v3';
  10.  
  11.     private $ch;
  12.  
  13.     public function __construct() {
  14.  
  15.         $this->ch = curl_init();
  16.  
  17.         $http_headers = [
  18.             'Content-type: application/json',
  19.             sprintf('X-Auth-Token: api-key %s', self::X_AUTH_TOKEN),
  20.             sprintf('X-Domain: %s', self::X_DOMAIN)
  21.         ];
  22.  
  23.         curl_setopt($this->ch, CURLOPT_HTTPHEADER, $http_headers);
  24.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  25.     }
  26.  
  27.     public function addContact(array $params) {
  28.  
  29.         if ($default_campaign = $this->getDefaultCampaign()) {
  30.  
  31.             $params['campaign'] = ['campaignId' => $default_campaign];
  32.  
  33.             curl_setopt($this->ch, CURLOPT_URL, sprintf('%s/contacts', self::API_LINK));
  34.             curl_setopt($this->ch, CURLOPT_POST, true);
  35.             curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($params));
  36.  
  37.             return $this->executeRequest(202);
  38.         }
  39.         return false;
  40.     }
  41.  
  42.     public function editContact() {
  43.         /**
  44.          * @todo write code)
  45.          */
  46.     }
  47.  
  48.     private function getDefaultCampaign() {
  49.  
  50.         curl_setopt($this->ch, CURLOPT_URL, sprintf('%s/campaigns', self::API_LINK));
  51.         curl_setopt($this->ch, CURLOPT_POST, false);
  52.         $campaigns = json_decode(curl_exec($this->ch), true);
  53.         $http_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
  54.  
  55.         if ($http_code == 200 && !empty($campaigns)) {
  56.  
  57.             foreach ($campaigns as $campaign) {
  58.                 if ($campaign['isDefault']) {
  59.                     return $campaign['campaignId'];
  60.                 }
  61.             }
  62.         }
  63.  
  64.         return false;
  65.     }
  66.  
  67.     private function executeRequest($response_code) {
  68.  
  69.         curl_exec($this->ch);
  70.         $http_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
  71.  
  72.         return ($http_code == $response_code) ? true : false;
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement