views:

2939

answers:

3

First of all, I am able to successfully authenticate using Oauth. I am using Padraic's tutorial found here: http://blog.astrumfutura.com/archives/411-Writing-A-Simple-Twitter-Client-Using-the-PHP-Zend-Frameworks-OAuth-Library-Zend%5FOauth.html

Now, my problem is that I already have a Twitter model using Zend_Service_Twitter. But since Zend_Service_Twitter requires a password, I decide to extend it. My new class is like this:

<?php

/**
 * I'm trying to extend Zend_Service_Twitter to use Oauth instead
 */

require_once 'Zend/Service/Twitter.php';

class Mytwitterapp_Twitteroauth extends Zend_Service_Twitter
{
    /**
     * Oauth
     * @var Zend_Oauth_Token_Access
     */
    protected $_token;

    /**
     * Array for Zend_Oauth_Consumer (i think)
     * @var Zend_Config_Ini
     */
    protected $_config;

    /**
     * @param object $token
     * @return void
     */
    public function __construct(Zend_Oauth_Token_Access $token)
    {
     $this->_config = new Zend_Config_Ini(APPLICATION_INI, APPLICATION_ENV);

     $this->_token = $token;
     $this->setUri('http://twitter.com');

     $client = $this->_token->getHttpClient($this->_config->oauth->toArray());
    }

    public function _init()
    {
     $client = $this->_token->getHttpClient($this->_config->oauth->toArray());
    }
}

And so my Model_Twitter, looks something like this:

<?php

require_once 'Mytwitterapp/Twitteroauth.php';

class Twitter_Model_Twitter 
{
    private $_twitter;
    private $_username;
    private $_password;

    public function __construct(array $options = null) 
    {        
         $oauth = new Zend_Session_Namespace('Twitter_Oauth');
         $token = unserialize($oauth->twitter_access_token);
         $this->_twitter = new Mytwitterapp_Twitteroauth($token);
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $pieces = explode('_', $key);
            foreach($pieces AS $piece_key => $piece_value) {
                $pieces[$piece_key] = ucfirst($piece_value);
            }
            $name = implode('',$pieces);
            $method = 'set' . $name;
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

    public function setTwitter($obj)
    {
        $this->_twitter = $obj;
        return $this;
    }


    public function verifyCredentials()
    {
        return $this->_twitter->account->verifyCredentials();
    }

    public function userTimeline()
    {
        return $this->_twitter->status->userTimeline();
    }
//...more code here...
}

And so finally, I am expecting to use these with something like this:

$twitter_model = new Twitter_Model_Twitter();
$this->view->friendsTimeline = $twitter_model->friendsTimeline();
  1. Am I doing it right? (In terms of extending my Zend_Service_Twitter class).

  2. How would you implement something like this?

I also get this error:

Zend_Rest_Client_Result_Exception: REST Response Error: fopen(/htdocs/twitter/application/views/helpers/Layout.php) [function.fopen]: failed to open stream: No such file or directory in /Applications/MAMP/bin/php5/lib/php/Zend/Rest/Client/Result.php on line 67
+2  A: 

Okay, I placed a bounty on this question and I do not know how to remove it. I am going to answer it myself. For others to know if they are encountering the same problem.

First, in my application.ini I have something like this:

oauth.version           = "1.0"
oauth.signatureMethod   = "HMAC-SHA1"
oauth.requestScheme     = "header"
oauth.siteUrl           = "http://mysite.com/twitter/public"
oauth.callbackUrl       = "http://mysite.com/twitter/public/login/callback"
oauth.requestTokenUrl   = "http://twitter.com/oauth/request_token"
oauth.authorizeUrl      = "http://twitter.com/oauth/authorize"
oauth.accessTokenUrl    = "http://twitter.com/oauth/access_token"
oauth.consumerKey       = "xxxxxx"
oauth.consumerSecret    = "xxxxxxxxxx"

Then, my new model is this:

<?php

/**
 * I'm trying to extend Zend_Service_Twitter to use Oauth instead
 */

require_once 'Zend/Service/Twitter.php';

class Mytwitterapp_Twitteroauth extends Zend_Service_Twitter
{
    /**
     * Oauth
     * @var Zend_Oauth_Token_Access
     */
    protected $_token;

    /**
     * Array for Zend_Oauth_Consumer (i think)
     * @var Zend_Config_Ini
     */
    protected $_config;

    /**
     * @param object $token
     * @return void
     */
    public function __construct(Zend_Oauth_Token_Access $token)
    {
     $this->_config = new Zend_Config_Ini(APPLICATION_INI, APPLICATION_ENV);
     $this->_token = $token;
     $this->setUri('http://twitter.com');
     //$this->_authInitialized = true;

        self::setHttpClient($token->getHttpClient($this->_config->oauth->toArray()));
    }
}

Using it would look something like this:

$oauth = new Zend_Session_Namespace('Twitter_Oauth');
$token = unserialize($oauth->twitter_access_token);
$this->_twitter = new Mytwitterapp_Twitteroauth($token);
wenbert
+2  A: 

We've done almost exactly this for Twitgoo.com (runnining full zend framework and twitter oauth login integration).

To generalize, we created two Zend_Auth_Identity models - one for password login one for oauth login. (actually 3 where 1 is 'anon' and fails all Zend_Auth). The identity holds the username and our local userid at minimum - for oauth it holds the Zend_Oauth_Token, for password it holds the password (we never store it). These identity models are what the Zend_Auth_Adapter for twitter returns, and what we pass to our Zend_Service_Twitter extension.

our twitter then takes in a Identity model, and handles setting up twitter for it.

class Tg_Service_Twitter extends Zend_Service_Twitter {

    public function __construct(Tg_Auth_Identity $login) {
        if ($login instanceof Tg_Auth_Identity_Password) {
            $password = $login->getPassword();
        } else if ($login instanceof Tg_Auth_Identity_Oauth) {
            $password = null;
            $httpClient = $login->getOauthToken()
                ->getHttpClient(Tg_Service_Twitter_Helper::getOauthOptions());
            self::setHttpClient($httpClient);
        } else {
            throw new Exception('not done yet');
        }

        $username = $login->getUsername();

        self::setupHttpClient();
        parent::__construct($username, $password);

        if ($login instanceof Tg_Auth_Identity_Oauth) {
            //makes it skip basic auth
            $this->_authInitialized = true;
        }
    }

The $username is required to be set from the login (which twitter gives back to you during getting access token) by some of the Service_Twitter functions.

I can add more specifics if needed.

Justin
Yes, I remember Twitgoo from Padriac's post. I am now able to GET/POST to twitter.com using Zend_Oauth. My problem is that I get this error: Catchable fatal error: Argument 2 passed to Zend_Oauth_Token_Access::toQueryString() must implement interface Zend_Oauth_Config_Interface, instance of Zend_Oauth_Client given, called in /htdocs/twitter/library/Zend/Oauth/Client.php on line 131 and defined in /htdocs/twitter/library/Zend/Oauth/Token/Access.php on line 55Have you encountered this error when you developed Twitgoo?
wenbert
I got it working! Padraic Brady replied to my thread in the ZF mailing list... It was in fact a bug. I just updated Oauth from the Incubator and the problem went away.
wenbert
Ah yeah, I forgot to mention that :-) - I had a temporary patch before incubator was updated with the fix.
Justin
A: 

Man, thanks for this. Was just about losing my mind on a new project that already had a bunch of Zend_Service_Twitter calls in it.

Phil Nelson