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

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

    API calls take a while to run. Is this the right way?


    Hi;

    Is this the right way of using apis?

    It seems to take a while for these api calls to run!


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

    use 
    OntraportAPI\Ontraport;

    include 
    'SDK-PHP/src/Ontraport.php';

    class 
    Sequence extends CI_Controller {

        public function 
    index()
            {
                if(!isset(
    $_POST['email']))
                    {
                        
    redirect(base_url('opt'));
                    }
                if(
    filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
                    {
    ####update our database (which is fast)                      
    $this->ontraport_contact_and_sequence();    
                    }
                else
                    {
                        
    redirect(base_url('opt'));
                    }    

            }
        

        private function 
    ontraport_contact_and_sequence()
            {
                
    $client = new Ontraport("******","******");    
                
    $requestParams = [ 
                    
    'condition' => json_encode([[
                        
    'field' => ['field' => 'email']
                        , 
    'op' => '='
                        
    'value' => ['value' => $_POST['email']]
                    ]])
                ];
                
    $response $client->contact()->retrieveMultiple($requestParams);
                
    $response json_decode($response);
                foreach(
    $response as $val=>$row)
                   {
                        if(isset(
    $row[0]->id))
                            {
                                
    $contact_id $row[0]->id;
                            }
                   }
                if(isset(
    $contact_id))
                    {
                        
    $this->add_contact_to_sequence($contact_id);
                    }
                else
                    {
                        
    $contact_id $this->create_contact_in_ontraport();
                        
    $this->add_contact_to_sequence($contact_id);
                    }    
            }

            private function 
    add_contact_to_sequence($contact_id)
                {
                    
    $client = new Ontraport("******","******");    
                    
    $requestParams = array(
                        
    "objectID"  => 0
                        
    "ids"       => $contact_id,
                        
    "add_list"  => 57
                        
    );
                        
    $response $client->object()->addToSequence($requestParams);
                        
    print_r($response);
                        
    //redirect(base_url('confirm-email'));
                
    }
            private function 
    create_contact_in_ontraport()
                {
                    
    $client = new Ontraport("******","******");    
                    
    $requestParams = array("email"     => $_POST['email']
                    );
                
    $response $client->contact()->saveOrUpdate($requestParams);
                
    $response json_decode($response);
                foreach(
    $response as $val=>$row)
                   {
                          
    $contact_id $row->id;
                   }
                  return 
    $contact_id
                }            
    }
    Thanks
    Last edited by English Breakfast Tea; June 26th, 2017 at 10:44 AM.
  2. #2
  3. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,108
    Rep Power
    220
    I have an idea.

    How about I do only the first sql (that interact with my database), then on the action page, I use ajax and send those 3 api calls to Ontraport after 1 second of page load with something like jquery .load?

    Would that be reliable?

    Thanks
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,744
    Rep Power
    4536
    I would expect that you'd only have to create a single Ontraport "$client" connection and then use that everywhere and send it the different actions you need done.

    I don't know if that'd be any faster or not, although I'd expect it to be better.
    -- Cigars, whiskey and wild, wild women. --
  6. #4
  7. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,108
    Rep Power
    220
    Yeh I am not sure how to do that.. create one and reuse through the class or app! Please show me.

    That could be the issuse? Or the api calls (CROD stuff).
  8. #5
  9. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,108
    Rep Power
    220
    I tried doing it like $client = new Ontraport("******","******"); so I could reuse it as a class property but it doesnt like it
  10. #6
  11. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,744
    Rep Power
    4536
    You can create $client once in the index() method and then pass it to the other two functions. Or maybe create it in the constructor for this class, but only if that's all this class is used for are these API calls.

    -John
    -- Cigars, whiskey and wild, wild women. --
  12. #7
  13. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,108
    Rep Power
    220
    I ended up doing this. Looks like a good solution
    PHP Code:
    <script>
    $( document ).ready(function() {
      setTimeout( function()
        { 
          var email = 'email@yahoo.com';
           $( "#pass_to_ontraport" ).load( "<?php echo base_url('pass_to_ontraport/index');?>", { email: email} );  
          }  , 2000 );
           
          }); 
     </script>
  14. #8
  15. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,108
    Rep Power
    220
    This way no one has to wait and everyone gets candy and bubble gums unless Requinix points out something I did wrong. Please approve!
  16. #9
  17. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,744
    Rep Power
    4536
    That has nothing to do with PHP, but okay....
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo