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

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

    What to do with api when internet connection dies?


    Hi;

    Just curious! I run this site on a local machine. One par communicates with a few apis.

    I noticed at some points I am getting invalid argument foreach.

    I noticed that happens when internet dies. What is the best thing to do to stop the script from running when conneciton dies or when api doesn't respond?

    Here is the code.

    Thanks
    PHP Code:
    $client = new Ontraport("***","***");    

                
    $requestParams = [ 
                    
    'condition' => json_encode([[
                        
    'field' => ['field' => 'email']
                        , 
    'op' => '='
                        
    'value' => ['value' => $_SESSION['email']]
                    ]])
                ];
                
    $response $client->contact()->retrieveMultiple($requestParams);
                
    $response json_decode($response);
                foreach(
    $response as $val=>$row)   <----- This is the death scene
                   
    {
                        if(isset(
    $row[0]->id))
                            {
                                
    $contact_id $row[0]->id;
                            }
                   } 
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    2
    Rep Power
    0
    when you new object Ontraport should be include time to live (TTL). And when call api should by use try catch.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    10
    Rep Power
    0
    If you are worried about foreach part, maybe adding an extra validation would help? Like:

    PHP Code:
    if (!empty($response) && is_object($response))
        {
        foreach(
    $response as $val=>$row)
        
    your_further_code_goes_here
        
    }
    else 
    //optional
        
    {
        
    //abort execution, send some email, etc
        

  6. #4
  7. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,218
    Rep Power
    220
    Originally Posted by CarlPham
    when you new object Ontraport should be include time to live (TTL). And when call api should by use try catch.
    TTL seems like what I want. Tell me more plz.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    2
    Rep Power
    0
    Sorry I just check again and see it hard code 60 seconds: https://github.com/Ontraport/SDK-PHP...lient.php#L200
    So I think you should be wrap your code in try catch

IMN logo majestic logo threadwatch logo seochat tools logo