Edited at

Facebook の API に何かあったときに IRC で通知すると便利じゃないかと思いまして

More than 5 years have passed since last update.

Qiita が頑張っているので僕も頑張ろうとおもいました。

まずは log.sqlite とかに DB つくりますよ。


install.sql

CREATE TABLE IF NOT EXISTS log (

current TEXT,
push TEXT,
created_at DATETIME
);

で、こんなスクリプトを cron とかで動かしますよ。

勢いで書いたから美しくないけど、許してください。


fbhealth.php

<?php

require_once 'Net/Socket/Tiarra.php';

$api_uri = 'http://www.facebook.com/feeds/api_status.php';
$db = __DIR__ . '/log.sqlite';
$pdo = new PDO("sqlite:$db");
$tiarra_socket = 'riaf';
$irc_channel = '#nequal@freenode';

$last_row = $pdo->query('SELECT * FROM log ORDER BY created_at DESC')->fetch(PDO::FETCH_ASSOC);

if ($last_row) {
$last = array(
'current' => json_decode($last_row['current'], true),
'push' => json_decode($last_row['push'], true),
'created_at' => new DateTime($last_row['created_at']),
);
} else {
$last = null;
}

$now = new DateTime;
$response = json_decode(file_get_contents($api_uri), true);

try {
if (!isset($response['current'])) {
throw new Exception('`current` is not found');
}

if (!isset($response['push'])) {
throw new Exception('`push` is not found');
}

if ($last) {
if ($last['current']['health'] != $response['current']['health']) {
$tiarra = new Net_Socket_Tiarra($tiarra_socket);
$tiarra->message($irc_channel, sprintf('10Facebook Status: %s (%s)', $response['current']['subject'], $response['current']['health']));
}
}

if (is_null($last) || $last['current']['health'] != $response['current']['health'] || $last['push']['id'] != $response['push']['id']) {
$insert = $pdo->prepare('INSERT INTO `log` (current, push, created_at) VALUES (:current, :push, :created_at);');
$insert->execute(array(
'current' => json_encode($response['current']),
'push' => json_encode($response['push']),
'created_at' => $now->format('Y-m-d H:i:s'),
));
}
} catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}


Net_Socket_Tiarra が必要なので、入ってない人は

pear channel-discover openpear.org

pear install Net_Socket_Tiarra

とかで入ると思うよ!