Advertisement
Guest User

ConstantContact bad example

a guest
May 7th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1.  
  2. class ConstantContact{
  3.    
  4.     private $api_key;
  5.     private $token;
  6.    
  7.     private $is_valid = false;
  8.    
  9.     private $url = 'https://api.constantcontact.com/v2/';
  10.    
  11.     # constructor simply sets some internal vars
  12.     function __construct($api_key, $token){
  13.        
  14.         if(!is_string($api_key) || empty($api_key)) return;
  15.         $this->api_key = $api_key;
  16.        
  17.         if(!is_string($token) || empty($token)) return;
  18.         $this->token = $token;
  19.        
  20.         $this->is_valid = true;
  21.     }
  22.    
  23.     # returns available contact lists
  24.     function get_lists(){
  25.         if(!$this->is_valid) return false;
  26.         $url = $this->url.'lists/?api_key='.$this->api_key;
  27.        
  28.         $ctx = stream_context_create($this->get_auth_context());
  29.         $result = json_decode(file_get_contents($url,0,$ctx));
  30.        
  31.         if(false===$result) return false;
  32.         return (array)$result;
  33.        
  34.     }
  35.    
  36.     # adds a contact to specified list.
  37.     # extra data is appended with array_merge_recursive()
  38.    
  39.     function add_contact($email, $list_id, $extra_data=array()){
  40.        
  41.         # standard input validation
  42.         if(!is_email($email)) return false;
  43.         if(!is_numeric($list_id) ) return false;
  44.         if(!is_array($extra_data)) $extra_data = array();
  45.        
  46.         # prepare the data
  47.         $d = array_merge_recursive($extra_data, array(
  48.             'lists'=>array(array('id'=>"".$list_id)),
  49.             'email_addresses'=>array( array(
  50.                 'email_address'=>$email,
  51.                 'opt_in_source'=>'ACTION_BY_OWNER',
  52.                 'confirm_status'=>'NO_CONFIRMATION_REQUIRED'
  53.             ))
  54.         ));
  55.        
  56.         # create the context
  57.         $url = $this->url."contacts?api_key=".$this->api_key;
  58.         $ctx = array_merge_recursive($this->get_auth_context(),array(
  59.             'http'=>array(
  60.                 'content'=>''.json_encode($d)
  61.             )
  62.         ));
  63.         $ctx['http']['method']="POST";
  64.         $ctx['http']['header'].="\r\n".implode("\r\n", array(
  65.             "Content-type: application/json",
  66.             "Action-By: ACTION_BY_OWNER"
  67.         ));
  68.        
  69.         $ctx = stream_context_create($ctx);
  70.         return file_get_contents($url,0,$ctx);         
  71.     }
  72.    
  73.     # returns authentication context template for file_get_contents
  74.     private function get_auth_context(){
  75.        
  76.         return array('http'=>array(
  77.             'method'=>'GET','header'=>implode("\r\n",array(
  78.                 "Authorization: Bearer ".$this->token,
  79.                 "X-Originating-Ip: ".$_SERVER['REMOTE_ADDR']
  80.             ))
  81.         ));
  82.     }
  83.    
  84. }
  85.  
  86.  
  87.  
  88. $coco = new ConstantContact("api_key", "token");
  89. echo var_dump($coco->add_contact('test_'.time().'@test.ca',1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement