Maki92
Maki92

Reputation: 441

Session, PHP Incomplete Class

I am using cakePHP 2.x . Currently doing about the twitter OAuth, http://code.42dh.com/oauth/.

function twitter_authentication()
{
            //assume above coding is all correct.   
    $this->Session->write('twitter_request_token', ($requestToken));
    $this->redirect('http://api.twitter.com/oauth/authenticate?force_login=true&oauth_token='.$requestToken->key); //I able to get $requestToken.
}

function twitter_login()
{
        $requestToken = $this->Session->read('twitter_request_token');
        $accessToken = $this->OAuthConsumer->getAccessToken('Twitter','https://api.twitter.com/oauth/access_token', $requestToken);

At function_login(), I failed to read session and ended up with PhP Incomplete Class. If I do $this->Session->write('twitter_request_token', serialize($requestToken)); and $requestToken = $this->Session->read(unserialize('twitter_request_token'); it will work, but I will ended up error at other places which caused by using serialize and unserialize session.

Upvotes: 3

Views: 1805

Answers (2)

Plywood
Plywood

Reputation: 899

OAuth.php's OauthToken class is quite simple with just two properties: key and secret. When you get the login url, you can store it to the session as an array:

CakeSession::write('Twitter.requestToken', array(
    'key' => $requestToken->key,
    'secret' => $requestToken->secret
));

Then, instantiate your own OAuthToken when calling OAuthClient->getAccessToken() like so:

$sessionRequestToken = CakeSession::read('Twitter.requestToken');
$accessToken = $twitterClient->getAccessToken('https://api.twitter.com/oauth/access_token', 
    new OAuthToken($sessionRequestToken['key'], $sessionRequestToken['secret']));

Should be ready to go:

if ($accessToken) {
    $twitterClient->post($accessToken->key, $accessToken->secret, 
        'https://api.twitter.com/1/statuses/update.json', array('status' => 'My balls smells like A-1 sauce. #science'));
}

Upvotes: 0

user1233508
user1233508

Reputation:

"PHP Incomplete Class" means PHP doesn't have a class definition for the object you're loading.

Option A: figure out what class that object is when you write it into the session and ensure that class's definition is loaded before loading the object.

Option B: convert the object to an stdClass or array before writing it, and convert back after loading. This might be more complex than the first option.

Upvotes: 2

Related Questions