首先,我能够使用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());
}
}
所以我的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
答案 0 :(得分: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"
然后,我的新模型是:
<?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()));
}
}
使用它看起来像这样:
$oauth = new Zend_Session_Namespace('Twitter_Oauth');
$token = unserialize($oauth->twitter_access_token);
$this->_twitter = new Mytwitterapp_Twitteroauth($token);
答案 1 :(得分:3)
我们已经为Twitgoo.com完成了这项工作(运行完整的zend框架和twitter oauth登录集成)。
为了概括,我们创建了两个Zend_Auth_Identity
模型 - 一个用于密码登录,一个用于oauth登录。 (实际上是3,其中1是'anon',并且所有Zend_Auth
都失败了)。身份至少保存用户名和本地用户ID - 对于oauth,它保存Zend_Oauth_Token
,对于密码,它保存密码(我们从不存储密码)。这些身份模型是Twitter返回Zend_Auth_Adapter
的内容,以及我们传递给Zend_Service_Twitter
扩展名的内容。
然后我们的Twitter接受身份模型,并处理为其设置推特。
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;
}
}
$ username需要通过某些Service_Twitter函数从登录(Twitter在获取访问令牌期间给您回复)中设置。
如果需要,我可以添加更多细节。
答案 2 :(得分:0)
男人,谢谢你。只是在一个已经有很多Zend_Service_Twitter
次调用的新项目中失去了理智。