ZendTwitterComponent

Twitter ツイート Hatena Bookmark ブックマーク

この記事はcakephp1.3向けに書かれています。

TwitterAPIを扱うcakePHPのコンポーネントでいい感じのやつが見つからなかったので、Zend_Service_TwitterをcakePHPから扱うためのコンポーネントを作成してみました。

http://github.com/polidog/ZendTwitterComponent

エラー処理とか全然できていないんですが、とりあえず作ってみました。

使う際はZend_Autoloaderを使えるようにしておかないとうごきません。
ちなみに対応するバージョンは以下の通りです。
PHP5.2
cakePHP1.3.3
ZendFramework1.10

Zend Framework自体は、cakePHPのvendors/の中にZendFramework/library/Zendのディレクトリを入れておいてもらえれば大丈夫です。

cakePHPのindex.phpには以下のようにしてください。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
	if (!defined('DS')) {
		define('DS', DIRECTORY_SEPARATOR);
	}
/**
 * These defines should only be edited if you have cake installed in
 * a directory layout other than the way it is distributed.
 * When using custom settings be sure to use the DS and do not add a trailing DS.
 */

/**
 * The full path to the directory which holds "app", WITHOUT a trailing DS.
 *
 */
	if (!defined('ROOT')) {
		define('ROOT', dirname(dirname(dirname(__FILE__))));
	}
/**
 * The actual directory name for the "app".
 *
 */
	if (!defined('APP_DIR')) {
		define('APP_DIR', basename(dirname(dirname(__FILE__))));
	}
/**
 * The absolute path to the "cake" directory, WITHOUT a trailing DS.
 *
 */
	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
		define('CAKE_CORE_INCLUDE_PATH', ROOT);
	}

/**
 * Editing below this line should NOT be necessary.
 * Change at your own risk.
 *
 */
	if (!defined('WEBROOT_DIR')) {
		define('WEBROOT_DIR', basename(dirname(__FILE__)));
	}
	if (!defined('WWW_ROOT')) {
		define('WWW_ROOT', dirname(__FILE__) . DS);
	}
	if (!defined('CORE_PATH')) {
		if (function_exists('ini_set') && ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'))) {
			define('APP_PATH', null);
			define('CORE_PATH', null);
		} else {
			define('APP_PATH', ROOT . DS . APP_DIR . DS);
			define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
		}
	}
	if (!include(CORE_PATH . 'cake' . DS . 'bootstrap.php')) {
		trigger_error("CakePHP core could not be found.  Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php.  It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
	}
	if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
		return;
	} else {
		//Zend Autoloaderの設定
		ini_set('include_path', ini_get('include_path').PATH_SEPARATOR . APP.DS.'vendors');
		App::import('Vendor','Zend_Loader_Autoloader',array('file' => 'Zend'.DS.'Loader'.DS.'Autoloader.php'));
		$loader = Zend_Loader_Autoloader::getInstance();

		$Dispatcher = new Dispatcher();
		$Dispatcher->dispatch();
	}

Twitterの設定情報に関しては「config/tiwtter.ini」を用意し以下のようにします

1
2
3
4
5
[twitter_oauth]
siteUrl = http://twitter.com/oauth
callbackUrl = http://localhost/test/testcallback
consumerKey = xxxxxxxxxxxxxx
consumerSecret = xxxxxxxxxxxxxxxxxxxxxxxxxxx

siteUrlはtwitterのOAuth認証のURLを指定します。
callbackUrlはOAuth認証後の戻り先URLを指定します。
consumerKey,consumerSecretはTwitterから配布されたものを記載してください。

あとは適当にコントローラを用意すればおkです。

comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。