public function create(Horde_Injector $injector) { global $conf; if (empty($conf['twitter']['key']) || empty($conf['twitter']['secret'])) { throw new Horde_Service_Twitter_Exception(Horde_Core_Translation::t("No OAuth Key or Secret found for the Twitter API")); } /* Keys - these are obtained when registering for the service */ $consumer_key = $conf['twitter']['key']; $consumer_secret = $conf['twitter']['secret']; /* Parameters required for the Horde_Oauth_Consumer */ $params = array('key' => $consumer_key, 'secret' => $consumer_secret, 'requestTokenUrl' => Horde_Service_Twitter::REQUEST_TOKEN_URL, 'authorizeTokenUrl' => Horde_Service_Twitter::USER_AUTHORIZE_URL, 'accessTokenUrl' => Horde_Service_Twitter::ACCESS_TOKEN_URL, 'signatureMethod' => new Horde_Oauth_SignatureMethod_HmacSha1(), 'callbackUrl' => $GLOBALS['registry']->getServiceLink('twitter')); /* Create the Consumer */ $auth = new Horde_Service_Twitter_Auth_Oauth(new Horde_Oauth_Consumer($params)); $request = new Horde_Service_Twitter_Request_Oauth($injector->getInstance('Horde_Controller_Request')); $twitter = new Horde_Service_Twitter($auth, $request); //$twitter->setCache($injector->getInstance('Horde_Cache')); $twitter->setLogger($injector->getInstance('Horde_Log_Logger')); $twitter->setHttpClient($injector->getInstance('Horde_Core_Factory_HttpClient')->create()); return $twitter; }
/** * Factory method to easily build a working Twitter client object. * * @param array $params Configuration parameters, with the following keys: * - 'oauth' (required): * - 'consumer_key' (required): The application's * consumer key * - 'consumer_secret' (required): The application's * consumer secret * - 'access_token' (optional): The user's access * token * - 'access_token_secret' (optional): The user's * access token secret. * - 'http' (optional): any configuration parameters * for Horde_Http_Client, e.g. proxy settings. * * @return Horde_Service_Twitter A twitter object that can be used * immediately to update and receive * statuses etc. */ public static function create($params) { if (!isset($params['oauth'])) { throw new Horde_Service_Twitter_Exception('Only OAuth authentication is supported.'); } /* Parameters required for the Horde_Oauth_Consumer */ $consumer_params = array('key' => $params['oauth']['consumer_key'], 'secret' => $params['oauth']['consumer_secret'], 'requestTokenUrl' => self::REQUEST_TOKEN_URL, 'authorizeTokenUrl' => self::USER_AUTHORIZE_URL, 'accessTokenUrl' => self::ACCESS_TOKEN_URL, 'signatureMethod' => new Horde_Oauth_SignatureMethod_HmacSha1()); /* Create the Consumer */ $oauth = new Horde_Oauth_Consumer($consumer_params); /* Create the Twitter client */ $twitter = new Horde_Service_Twitter(new Horde_Service_Twitter_Auth_Oauth($oauth), new Horde_Service_Twitter_Request_Oauth(new Horde_Controller_Request_Http())); /* Create HTTP client. */ $http_params = isset($params['http']) ? $params['http'] : array(); $twitter->setHttpClient(new Horde_Http_Client($http_params)); /* Check for an existing token */ if (!empty($params['oauth']['access_token']) && !empty($params['oauth']['access_token_secret'])) { $auth_token = new Horde_Oauth_Token($params['oauth']['access_token'], $params['oauth']['access_token_secret']); $twitter->auth->setToken($auth_token); } return $twitter; }
/** * Create the twitter recipient. * * @param array $conf The configuration. * * @return Horde_Push_Recipient_Twitter The twitter recipient. */ private function _createTwitter($conf) { $params = array('key' => $conf['twitter']['key'], 'secret' => $conf['twitter']['secret'], 'requestTokenUrl' => Horde_Service_Twitter::REQUEST_TOKEN_URL, 'authorizeTokenUrl' => Horde_Service_Twitter::USER_AUTHORIZE_URL, 'accessTokenUrl' => Horde_Service_Twitter::ACCESS_TOKEN_URL, 'signatureMethod' => new Horde_Oauth_SignatureMethod_HmacSha1()); /* Create the Consumer */ $auth = new Horde_Service_Twitter_Auth_Oauth(new Horde_Oauth_Consumer($params)); $request = new Horde_Service_Twitter_Request_Oauth($this->_createControllerRequest($conf)); $twitter = new Horde_Service_Twitter($auth, $request); $http = $this->_createHttpClient($conf); $twitter->setHttpClient($http); $auth_token = new Horde_Oauth_Token($conf['twitter']['token_key'], $conf['twitter']['token_secret']); $twitter->auth->setToken($auth_token); return new Horde_Push_Recipient_Twitter($twitter, $http); }
#!/usr/bin/env php <?php /** * Simple Twitter client. * * Copyright 2009-2015 Horde LLC (http://www.horde.org/) * * @author Jan Schneider <*****@*****.**> * @author Michael J. Rubinsky <*****@*****.**> * @license http://www.horde.org/licenses/bsd BSD * @category Horde * @package Service_Twitter */ /* Keys - these are obtained when registering for the service */ $keys = array('consumer_key' => '*****', 'consumer_secret' => '*****', 'access_token' => '*****-*****', 'access_token_secret' => '*****'); /* Enable autoloading. */ require 'Horde/Autoloader/Default.php'; /* Create the Twitter client */ $twitter = Horde_Service_Twitter::create(array('oauth' => $keys)); /* Do something cool.... */ try { $result = $twitter->statuses->update('Testing Horde/Twitter integration 2'); print_r(Horde_Serialize::unserialize($result, Horde_Serialize::JSON)); } catch (Horde_Service_Twitter_Exception $e) { $error = Horde_Serialize::unserialize($e->getMessage(), Horde_Serialize::JSON); echo "{$error->error}\n"; }