PHP OOP - setting class parameter, after an external class callback from Zebra_Curl

I'm using this lib: http://stefangabos.ro/php-libraries/zebra-curl/

Within my own class structure to retrieve a token.

<?php
class getToken 
{
    private $url =  "";
    private $customHeaders = array('Content-Type: application/x-www-form-urlencoded',);
    private $parameters = array();
    private $cacheTime = 600;
    private $curl;
    public  $token = false;

    public function __construct(){
        require_once('lib/Zebra_cURL.php');
        $this->curl = new Zebra_cURL();
        $this->curl->cache('cache', $this->cacheTime);
        $this->curl->option(CURLOPT_HTTPHEADER, $this->customHeaders);
        $this->curl->post($this->url, $this->parameters, __CLASS__.'::tokenCallback');
    }    

    public function returnToken(){
        return $this->token;
    }

    public function tokenCallback($result){
        if ($result->response[1] == CURLE_OK) {
            $body = str_replace(array('\\/','\"'),array('/','"'), html_entity_decode($result->body)); 
            $tokenStart = stripos($body,"<EncryptedAssertion");
            $tokenEnd= stripos($body,"EncryptedAssertion>");
            $token = substr($body, $tokenStart, $tokenEnd-$tokenStart);
            $this->$token = $token;
        }else{
            $this->token = false;
        }
    }
}

In the method tokenCallback, I'm trying to set the value of the classes $token parameter, but get the following error: Fatal error: Using $this when not in object context in ....

I've tried setting this using self, but get this error: Fatal error: Access to undeclared static property: getToken::$token

I'm guessing that the way the callback from Zebra_Curl is instantiated is the problem - any one got any ideas on how to get this working?

Answers


From what I can tell you are calling a static function in the call back for Zebra curl. Your method is using an instantiated variable "token". The problem with the static call is it will not maintain current state and will set any static variables you may define global for the class. I would try the following: array($this, 'tokenCallback').

I haven't looked at Zebra curl, but if he is calling call_user_func, this should work for you.

public function __construct(){
    require_once('lib/Zebra_cURL.php');
    $this->curl = new Zebra_cURL();
    $this->curl->cache('cache', $this->cacheTime);
    $this->curl->option(CURLOPT_HTTPHEADER, $this->customHeaders);
    $this->curl->post($this->url, $this->parameters, array($this,'tokenCallback') );
}    

public function returnToken(){
    return $this->token;
}

public function tokenCallback($result){
    if ($result->response[1] == CURLE_OK) {
        $body = str_replace(array('\\/','\"'),array('/','"'), html_entity_decode($result->body)); 
        $tokenStart = stripos($body,"<EncryptedAssertion");
        $tokenEnd= stripos($body,"EncryptedAssertion>");
        $token = substr($body, $tokenStart, $tokenEnd-$tokenStart);
        $this->$token = $token;
    }else{
        $this->token = false;
    }
}

}


Need Your Help

how to convert HTML form into a clickable link?

html html5

I have a form with a button which submit a form and open the page in a new window, my requirement is to get rid of this button in the page and add that into a menu navigation which make up of "&lt;...