0

My class connects to an external site and stores the result in the $connectArray variable. A timeout value is returned so I added the CheckTime function to check that. But $connectArray is empty on reload of the class. Would someone please explain why that variable is not staying set?

    class myclass {
       public $connectArray;
       public $sessionTime;
       
       public function __construct() {
          $this->GetAccessToken();  
       }
     
       public function CheckTime() {
           if (! empty($this->connectArray->accessToken)) {
               echo 'cmp '.$this->sessionTime . ' - ' . time() + $this->connectArray->expiresIn. '<br>';
               return true;
           }

           echo 'missing details<br>';
           return false;     
       }
       
       public function GetAccessToken() {
        
           $this->CheckTime();
        
           $url = "https://example.com";
           $headers = ['userName' => "xxxx",
                       'password' => "xxxx"               
                      ];  

           $ch = curl_init();
           curl_setopt ($ch, CURLOPT_URL, $url);
           curl_setopt ($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
           curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($headers));
           curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));        
           curl_setopt ($ch, CURLOPT_SSLVERSION, 6);
           curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 2);  
           curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);                                
           curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
           curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);      
           $result = curl_exec($ch);
           if (curl_error($ch)) {
               echo 'Curl error: ' . curl_error($ch);
               flush();
               ob_flush();                      
           }
           curl_close($ch);
           $this->connectArray = json_decode($result);
           
           if (! empty($this->connectArray->errorCode)) {
               $this->sessionTime = '';
               return false;
           } elseif (! empty($this->connectArray->accessToken)) {
               $this->sessionTime = time() + $this->connectArray->expiresIn;
               return true;
           }
           return false;        
       }    

    }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.