Zend 框架:Zend_Oauth 和 Zend_Service_Twitter [英] Zend Framework: Zend_Oauth and Zend_Service_Twitter
问题描述
首先,我能够使用 Oauth 成功进行身份验证.我正在使用在这里找到的 Padraic 教程:http://blog.astrumfutura.com/archives/411-Writing-A-Simple-Twitter-Client-Using-the-PHP-Zend-Frameworks-OAuth-Library-Zend_Oauth.html
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_Oauth.html
现在,我的问题是我已经有一个使用 Zend_Service_Twitter 的 Twitter 模型.但是由于 Zend_Service_Twitter 需要密码,我决定扩展它.我的新课是这样的:
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());
}
}
所以我的 Model_Twitter 看起来像这样:
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();
我做对了吗?(就扩展我的 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
推荐答案
好的,我悬赏这个问题,但我不知道如何删除它.我自己来回答.让其他人知道他们是否遇到了同样的问题.
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.
首先,在我的 application.ini 中,我有这样的内容:
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()));
}
}
使用它看起来像这样:
$oauth = new Zend_Session_Namespace('Twitter_Oauth');
$token = unserialize($oauth->twitter_access_token);
$this->_twitter = new Mytwitterapp_Twitteroauth($token);
这篇关于Zend 框架:Zend_Oauth 和 Zend_Service_Twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!