0

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?

1 Answer 1

1

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;
    }
}

}

Sign up to request clarification or add additional context in comments.

Comments

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.