Zend Framework:Zend_Oauth和Zend_Service_Twitter

wen*_*ert 4 twitter zend-framework oauth

首先,我能够使用Oauth成功进行身份验证.我正在使用Padraic的教程:http://blog.astrumfutura.com/archives/411-Writing-A-Simple-Twitter-Client-Using-the-PHP-Zend-Frameworks-OAuth-Library-Zend_Oauth.html

现在,我的问题是我已经有一个使用Zend_Service_Twitter的Twitter模型.但由于Zend_Service_Twitter需要密码,我决定扩展它.我的新课程是这样的:

<?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());
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我的Model_Twitter看起来像这样:

<?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...
}
Run Code Online (Sandbox Code Playgroud)

最后,我希望使用这样的东西:

$twitter_model = new Twitter_Model_Twitter();
$this->view->friendsTimeline = $twitter_model->friendsTimeline();
Run Code Online (Sandbox Code Playgroud)
  1. 我做得对吗?(在扩展我的Zend_Service_Twitter类方面).

  2. 你会如何实现这样的东西?

我也得到这个错误:

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
Run Code Online (Sandbox Code Playgroud)

wen*_*ert 5

好的,我在这个问题上放了一笔赏金,我不知道如何删除它.我自己会回答.让其他人知道他们是否遇到同样的问题.

首先,在我的application.ini中我有这样的事情:

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"
Run Code Online (Sandbox Code Playgroud)

然后,我的新模型是这样的:

<?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()));
    }
}
Run Code Online (Sandbox Code Playgroud)

使用它看起来像这样:

$oauth = new Zend_Session_Namespace('Twitter_Oauth');
$token = unserialize($oauth->twitter_access_token);
$this->_twitter = new Mytwitterapp_Twitteroauth($token);
Run Code Online (Sandbox Code Playgroud)