如何在zend框架中使用openinviter

时间:2010-11-26 12:45:16

标签: php zend-framework

您好 任何人都可以告诉我如何在zend框架中使用openinviter脚本

1 个答案:

答案 0 :(得分:2)

我已经在我的一个项目中使用Open Invitor了解了一些教程。不幸的是,我找不到链接。以下是主要步骤:

(a)创建自定义库。为此,请在库(library / openinvitor)中创建一个文件夹

(b)在此库中安装Open Invitor Package(即library / openinvitor / install.php)。我是通过评论索引和放大来做到这一点的。安装过程中的htaccess。

(c)在Boot Strap中注册新库。 (复制Open Invitor Loading所需的行)

 protected function _initAutoload ()
{

    // Add autoloader empty namespace
    $autoLoader = Zend_Loader_Autoloader::getInstance();

    $autoLoader->registerNamespace('openinviter_');

      $resourceLoader = new Zend_Loader_Autoloader_Resource(array('basePath' => APPLICATION_PATH , 'namespace' => '' , 'resourceTypes' => array('form' => array('path' => 'forms/' , 'namespace' => 'Form_') , 'model' => array('path' => 'models/' , 'namespace' => 'Model_'))));
    // Return it so that it can be stored by the bootstrap
    return $autoLoader;
}

(d)库文件的文件名是:Service.php还要注意步骤(e)中类的名称

(e)服务文件的内容是:

    <?php

require_once 'openinviter.php';

/**
* This class is the connection between OpenInviter service and Zend Framework.
* The class is implemented only for extracting contacts.
*
* @tutorial
*
*         $inviter = new OpenInviter_Service();
*
*       p($inviter->getPlugins());// get all services
*      p($inviter->getPlugins('email'));// get all services
*       p($inviter->getPlugins('email', 'gmail'));// get gmail plugin properties
*       p($inviter->getPlugins('email', 'gmail', 'version'));// get gmail plugin version
*
*       // get contacts
*       p($inviter->getContacts('me@example.com', 'mypass', 'example'));
*
*
* @author stoil
* @link http://openinviter.com/
* @uses OpenInviter 1.7.6
*
*/
class openinviter_Service
{
const PATH_PLUGINS = 'plugins/';

protected $_messages = array();
protected $_plugins;
protected $_openInviter;

/*~~~~~~~~~~ private methods ~~~~~~~~~~*/
private function _loadPlugins()
{
if($this->_plugins === null) {
$this->_plugins = $this->getOpenInviter()->getPlugins(false);
}
}

/*~~~~~~~~~~ protected methods ~~~~~~~~~~*/

protected function _addMessage($code, $message, $type = 'error')
{
$this->_messages[$type][$code] = $message;
}

protected function _initAutoload()
{
set_include_path(
dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . self::PATH_PLUGINS.
PATH_SEPARATOR.
get_include_path()
);
}

/*~~~~~~~~~~ constructor ~~~~~~~~~~*/

public function __construct()
{
$this->_initAutoload();
$this->_openInviter = new openinviter();
$this->_loadPlugins();
}

/*~~~~~~~~~~ public methods ~~~~~~~~~~*/

/**
* Update plugins
*/
public function updatePlugins()
{
$this->_plugins = $this->getOpenInviter()->getPlugins(true);
}

/**
* Get plugin(s), provider(s) or provider details
* @param $type
* @param $provider
* @param $detail
* @return unknown_type
*/
public function getPlugins($type = null, $provider = null, $detail = null)
{
if ($type !== null) {
if ($provider !== null) {
if ($detail !== null) {
return $this->_plugins[$type][$provider][$detail];
} else {
return $this->_plugins[$type][$provider];
}
} else {
return $this->_plugins[$type];
}
} else {
return $this->_plugins;
}
}

/**
* @return openinviter
*/
protected function getOpenInviter()
{
return $this->_openInviter;
}

/**
* Get system messages
* @param string $type
* @return array
*/
public function getMessages($type = null)
{
if($type !== null) {
return $this->_messages[$type];
} else {
return $this->_messages;
}
}

/**
* Get email clients
* @param string $email
* @param string $password
* @param string $provider
* @return array
*/
public function getContacts($email, $password, $provider)
{
$contacts = array();

$this->getOpenInviter()->startPlugin($provider);

$internalError = $this->getOpenInviter()->getInternalError();
if ($internalError) {
$this->_addMessage('inviter', $internalError);
} elseif (! $this->getOpenInviter()->login($email, $password)) {
$internalError = $this->getOpenInviter()->getInternalError();
$this->_addMessage(
'login',
($internalError
? $internalError
: 'Login failed. Please check the email and password you have provided and try again later !'
)
);
} elseif (false === $contacts = $this->getOpenInviter()->getMyContacts()) {
$this->_addMessage('contacts', 'Unable to get contacts');
}

return $contacts;
}

}

(f)然后在所需的控制器/函数中,创建Open Invitor librray的对象并访问任何函数。

    $inviter    =   new openinviter_Service();          // Create the Object of Opne Invitor
                                    $plugins    =   $inviter->getPlugins('email');      // Read all Available Plugins.