#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,213
    Rep Power
    220

    Is this the right way of working with APIs?


    Hi;

    I know it might be a little too much to ask, but I wanna make sure I am doing it right since I am doing a big launch relying on this code.

    When contact opts in, I send them to Thank you page and periodically update this script (Did Ioose Requnix already ?)..

    What I want this script to do, is to... if not already, add the contact to Ontraport system, if there already, update.

    It looks safe and functioning well.

    I know it's not perfect by no means, but can you find any dedly miustakes that might brinkg down my empire? I don't want to call the api or pressure the database unless it's very neccessary.

    PHP Code:
    <?php
    defined
    ('BASEPATH') OR exit('No direct script access allowed');

    use 
    OntraportAPI\Ontraport;
    include 
    'SDK-PHP/src/Ontraport.php';


    class 
    Update_views extends CI_Controller {

        public 
    $contact_details;
        public function 
    index($plc=NULL)
            {
                if(!isset(
    $_SESSION['email']) || $plc==NULL)
                    {
                        exit();
                    }
                
    $this->contact_details $this->db->get_where('pum_sequence', array('email' => $_SESSION['email']));
                if(
    $this->contact_details->num_rows()==1)
                    {
                        if(!
    $this->already_passed_to_ontraport())
                            {
                                
    $this->ontraport_contact_and_sequence();
                            }    
                    }    
            }

        public function 
    already_passed_to_ontraport()
            {
                
    $results $this->db->get_where('pum_sequence', array('passed_to_ontraport' => 1,'email'=>$_SESSION['email']));
                
    $results $results->result_array();
                if(
    count($results)>0)
                    {
                        return 
    true;
                    }
                else 
                    {
                        return 
    false;
                    }    
            }    
        public function 
    pass_to_ontraport()
            {
                
    $data = array(
                   
    'passed_to_ontraport' => 1,
                );
                
    $this->db->where('email'$_SESSION['email']);
                
    $this->db->update('pum_sequence'$data); 
            }

        private function 
    update_ontraport_user_id($ontraport_user_id)
            {
                
    $data = array(
                   
    'ontraport_user_id' => $ontraport_user_id,
                );

                
    $this->db->where('email'$_SESSION['email']);
                
    $this->db->update('pum_sequence'$data); 
            }        
        private function 
    ontraport_contact_and_sequence()
            {
                
    $client = new Ontraport("***","***");    

                
    $requestParams = [ 
                    
    'condition' => json_encode([[
                        
    'field' => ['field' => 'email']
                        , 
    'op' => '='
                        
    'value' => ['value' => $_SESSION['email']]
                    ]])
                ];
                
    $response $client->contact()->retrieveMultiple($requestParams);
                
    $response json_decode($response);
                if(isset(
    $response))
                    {
                        foreach(
    $response as $val=>$row)
                           {
                                if(isset(
    $row[0]->id))
                                    {
                                        
    $contact_id $row[0]->id;
                                    }
                           }
                        if(isset(
    $contact_id))
                            {
                                
    $this->update_contact_base64_email($contact_id);
                            }
                        else
                            {
                                
    $contact_id $this->create_contact_in_ontraport();
                            }
                        
    //Update OP user id
                        
    $this->update_ontraport_user_id($contact_id);

                        
    $contact_details $this->db->get_where('pum_sequence', array('email' => $_SESSION['email']));
                        
                        if(
    $contact_details->result_array()[0]['passed_to_ontraport']==0)
                            {
                                
    $this->add_contact_to_pum_main_equence($contact_id);
                                
    $this->pass_to_ontraport();
                            }
                    }            
            }

            private function 
    update_contact_base64_email($contact_id)
                {
                    
    $client = new Ontraport("****","****");    
                    
    //This updates the value of email base 64 in Ontraport f1481
                    
    $requestParams = array(
                        
    "id"        => $contact_id,
                        
    'f1481'=>$this->custom_functions->base64url_encode($_SESSION['email'])
                    );
                    
    $response $client->contact()->update($requestParams);
                }
            private function 
    add_contact_to_pum_main_equence($contact_id)
                {
                    
    $client = new Ontraport("****","****");    
                    
    $requestParams = array(
                        
    "objectID" => 0// Contact object
                        
    "ids"      => $contact_id,
                        
    "add_list" => 65,
                        
    "sub_type" => "Sequence"
                    
    );
                    
    $response $client->object()->subscribe($requestParams);
                   
                }
            private function 
    create_contact_in_ontraport()
                {
                    
    $client = new Ontraport("****","****");    
                    
    $requestParams = array("email" => $_SESSION['email'], 'f1481'=>$this->custom_functions->base64url_encode($_SESSION['email'])
                    );
                
    $response $client->contact()->saveOrUpdate($requestParams);
                
    $response json_decode($response);
                
                foreach(
    $response as $val=>$row)
                   {
                          if(isset(
    $row->id))
                              {
                                  
    $contact_id $row->id;
                              }    
                   }
                  return 
    $contact_id
                }    
    }
    Last edited by English Breakfast Tea; October 9th, 2017 at 11:04 PM.

IMN logo majestic logo threadwatch logo seochat tools logo