首先,我能夠使用Oauth成功進行身份驗證。我使用的帕德里克的教程在這裏找到:http://blog.astrumfutura.com/archives/411-Writing-A-Simple-Twitter-Client-Using-the-PHP-Zend-Frameworks-OAuth-Library-Zend_Oauth.htmlZend Framework:Zend_Oauth和Zend_Service_Twitter
現在,我的問題是,我使用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());
}
}
所以我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...
}
所以最後,我期待像這樣的東西用這些:
$twitter_model = new Twitter_Model_Twitter();
$this->view->friendsTimeline = $twitter_model->friendsTimeline();
我在做對吧? (關於擴展我的Zend_Service_Twitter類)。
你將如何實現這樣的事情?
我也收到此錯誤:
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
帖子中的鏈接已遺失。 – Ben 2010-02-15 21:24:19