Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

GMailFeedクラス

ちょっと今更なネタですが、個人的には最近になって知ったので。。。;-)
GMailのFeedはhttpsBASIC認証なので、取得する仕組みとしてはさほど難しくはありません。phpclassesではGmAtomが登録されていますが、curl拡張モジュールが必要です。まあ、最近はbuildするようにしているので問題はないのですが、curlを使わずにできないかなぁ、と。
ということで、PHPマニュアルにあったStream拡張のサンプルを使って、新着メールを取り出すクラスを作ってみました。結構便利ですよ > Stream拡張

<?php
/**
 * GMailFeedクラス
 *
 * @author SHIMOOKA Hideyuki <shimooka@doyouphp.jp>
 * @create 2006/11/12
 */
class GmailFeed {
    private static $GMAIL_FEED_URL = 'https://mail.google.com/mail/feed/atom';
    private $authorization;
    private $label;

    /**
     * コンストラクタ
     * @param string GMailのユーザー名
     * @param password パスワード
     */
    public function __construct($username, $password) {
        $this->authorization = base64_encode($username . ':' . $password);
        $this->label = '';
    }

    /**
     * ラベル名を取得する
     * @return string ラベル名。初期値は空文字
     */
    public function getLabel() {
        return $this->label = $label;
    }

    /**
     * ラベル名を取得する
     * @param string ラベル名。文字列のみ有効
     * @throws RuntimeException
     */
    public function setLabel($label) {
        if (!is_string($label)) {
            throw new RuntimeException('Invalid value for label');
        }
        $this->label = $label;
    }

    /**
     * Feedを取得する
     * @throws RuntimeException
     */
    public function getFeed() {
        return $this->_get();
    }

    /**
     * FeedをSimpleXMLElementオブジェクトとして取得する
     * @throws RuntimeException
     */
    public function getFeedAsSimpleXml() {
        return new SimpleXMLElement($this->_get());
    }

    private function _get() {
        $options = array(
                       'http' => array(
                           'method' => "GET",
                           'header' => "Authorization: Basic ". $this->authorization
                        )
                   );

        $context = stream_context_create($options);

        $url = self::$GMAIL_FEED_URL;
        if ($this->label !== '') {
            $url .= '/' . $this->label;
        }
        $fp = @fopen($url, 'rb', false, $context);
        if ($fp === false) {
            throw new RuntimeException('Failed to connect');
        }

        $contents = @stream_get_contents($fp);
        if ($contents === false) {
            throw new RuntimeException('Failed to get contents');
        }
        fclose($fp);

        return $contents;
    }
}

クライアント側は次のような感じ。

<?php
require 'GmailFeed.class.php';

$username = 'YOUR USERNAME';
$password = 'YOUR PASSWORD';

/**
 * 未読メールのFeedを取得
 */
$gmail = new GmailFeed($username, $password);
var_dump(mb_convert_encoding($gmail->getFeed(), mb_internal_encoding(), 'utf-8'));

/**
 * 未読メールのFeedをSimpleXMLElementオブジェクトを取得
 */
$elements = $gmail->getFeedAsSimpleXml();
echo '<ul>';
foreach ($elements->entry as $entry) {
    $title = mb_convert_encoding($entry->title, mb_internal_encoding(), 'utf-8');
    printf('<li>[%s] <a href="%s" title="%s">%s</a></li>',
           $entry->issued,
           $entry->link['href'],
           $title,
           $title);
}
echo '</ul>';

/**
 * ラベル「phpsec」が付いた未読メールのFeedを
 * SimpleXMLElementオブジェクトを取得
 */
$gmail->setLabel('phpsec');
$elements = $gmail->getFeedAsSimpleXml();
echo '<ul>';
foreach ($elements->entry as $entry) {
    $title = mb_convert_encoding($entry->title, mb_internal_encoding(), 'utf-8');
    printf('<li>[%s] <a href="%s" title="%s">%s</a></li>',
           $entry->issued,
           $entry->link['href'],
           $title,
           $title);
}
echo '</ul>';

try {
    /**
     * RuntimeExceptionが発生する
     */
    $gmail->setLabel(null);
}
catch (Exception $e) {
    var_dump($e->getMessage());
}

動作確認はPHP5.2.0のみです。SimpleXMLをサポートしてるなら、PHP5.1.xでも動作すると思います。あと、クラス側はちょこっと書き換えれば、PHP4向けにもなると思います。
やっぱ、SimpleXML便利すぎ! :-D

こういう企画、いいッスね

当然ディナーで ;-)


と言うことで、MOONGIFTとランチ、またはディナー(呑みとも言う)をご一緒して下さる方を募集します。オープンソースに限らず、最新のネットニュースや技術周りならそこそこ話題があると踏んでいますので、1時間や2時間くらい楽しませられるのではないかと。ああ、基幹システムやERP、会計/物流、クレジットカードシステムを熱く語ることもできます。

Joining .WAVs with PHPを読んで思ったこと

実はネタにしようと思っていたのですが、phpspotさんに先を越されちゃいました。。。;-)


I’m currently working on a CAPTCHA plugin for DokuWiki and thought about providing audio output for users not able to see the image. This is pretty simple for CAPTCHAs – there is no need for complicated speech synthesis because you only need recordings of the 26 possible letters. But you need a way of joining those recordings on the fly...

最近Ajax・DHTMLを中心とした「見た目重視」な技術・テクニックがどんどん出てきてますが、上のような発想が出てくる技術者って(自分を含め)どれくらいいるのだろうか?と。そういう意味でも、上のblogエントリはテクニックよりもその発想が重要と思います。

Callicore Desktop - A PHP-GTK2 Framework

前回の勉強会でsakamotoさんが「gtkで作ってみたい」的な話をしていたので、とりあえず取り上げてみる、と。
publicリリースがまだ無いなど、まだまだ始まったばかりのプロジェクトのようですが、今後化けるかも知れない雰囲気はあります(かね?)。現時点のコードはsvnで取り出せます。


I’m currently working on some heavy duty dual treeview widgets that deal with drag and drop, and I still have a lot of code lying around that needs to be integrated into the system, but you can take a look at the Callicore site or directly at the Code. Suggestions are always welcome.

しかし、PHP-GTKってGTK+2.6.9以上が必要なのね。。。CentOS4.4じゃ検証できないわ :-(
Windowsで試してみるかなぁ。。。

xdebug.var_display_max_depth


Xdebug2.0.0RC1を使っていて、再帰的なデータ構造が2階層目までしか表示されない。何でかな〜と思っていると、php.iniに

xdebug.var_display_max_depth=2

という設定が。。。こんなのやったっけ?と思い調べると、RC1のChangelogにしっかりと記載が。。。

  • Partially implemented FR #50: Resource limiting for variable display. By default only two levels of nested variables and max string lengths of 512 are shown. This can be changed by setting the ini settings

xdebug.var_display_max_depth and xdebug.var_display_max_data.

確かに

xdebug.var_display_max_data=102400
xdebug.var_display_max_depth=100

とすると、それなりに表示されたので良しとするも、一体いつこの設定やったんだろ?
ちなみに、Xdebug2.0.0RC1のini設定項目は次のように大量です。。。