PHP で Google Calendar API から日本の祝日データを取得

Google Calendarから日本の祝日データを取得する方法についての調査メモ。

ポイント

  • GDataを使う場合はデベロッパーKEYが必要だが、GDataを使わなくても日本の祝日カレンダーは取得できる。
  • 祝日名と日付を取得したい場合、Projection valueは”full-noattendees”で。(fullより少しだけデータが小さくなる。)
  • 利用規約はGoogleカレンダー利用規約。

via. floatingdays: Google Calendar API で日本の祝日データを取得

PHP でやるなら…

$holidays_url = sprintf(
        'http://www.google.com/calendar/feeds/%s/public/full-noattendees?start-min=%s&start-max=%s&max-results=%d&alt=json' ,
        'outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar.google.com' , // 'japanese@holiday.calendar.google.com' ,
        '2012-01-01' ,  // 取得開始日
        '2012-12-31' ,  // 取得終了日
        50              // 最大取得数
        );
if ( $results = file_get_contents($holidays_url) ) {
        $results = json_decode($results, true);
        $holidays = array();
        foreach ($results['feed']['entry'] as $val ) {
                $date  = $val['gd$when'][0]['startTime'];
                $title = $val['title']['$t'];
                $holidays[$date] = $title;
        }
        ksort($holidays);
}

PHP で Embed.ly の API から、なんか取ってきて表示

$url = 'http://twitter.com/#!/wokamoto/status/149621327832158208';
$max_width = 500;
$api = 'http://api.embed.ly/1/oembed?url='.rawurlencode($url).'&maxwidth='.$max_width.'&format=json';
$result = json_decode(file_get_contents($api), true);
echo $result['html'];

エラー対応とかは、自分で組み込んでね。

PHP で csv ファイルを連想配列に読み込む

function parse_csv( $csv_file, $title_row = -1, $start_row = 0, $key_cal = -1, $length = 0, $delimiter = ',', $enclosure = '"' ) {
    $results = FALSE;

    if ( file_exists($csv_file) && ($fp = fopen($csv_file, 'r')) ) {
        $row = 0;
        $results  = array();
        $col_name = array();
        while ( $field_array = fgetcsv($fp, $length, $delimiter, $enclosure) ) {
            if ( $row === $title_row ) {
                $col = 0;
                foreach ($field_array as $value) {
                    $col_name[$col] = !empty($value) ? $value : $col;
                    $col++;
                }
            }
            if ( $row >= $start_row ) {
                $result = array();
                $col = 0;
                foreach ($field_array as $value) {
                    $key = isset($col_name[$col]) ? $col_name[$col] : $col;
                    $result[$key] = $value;
                    $col++;
                }

                $key = $row;
                if ( is_numeric($key_cal) ) {
                    $key =
                        ( $key_cal >= 0 && isset($col_name[$key_cal]) )
                        ? $result[$col_name[$key_cal]]
                        : $row;
                } else {
                    foreach ($col_name as $value) {
                        if ( $key_cal === $value ) {
                            $key = $result[$value];
                            break;
                        }
                    }
                }
                $results[$key] = $result;
            }
            $row++;
        }
        fclose($fp);
    }

    return $results;
}

Topsy から Google+ の検索結果を JSON で取得

今朝(米国時間10/11)、リアルタイム検索・分析プラットフォームを提供するTopsy LabsがGoogle+を検索対象に含めたことを発表した。

Google+の公開投稿専用の検索エンジン(ベータ版)はこちら〔日本語版〕。ユーザーはGoogle+への公開投稿をリアルタイムでも過去に遡っても検索できる。

via. Topsy、Google+一般公開投稿のリアルタイム検索エンジンをローンチ〔日本語にも対応ずみ〕

Topsy から特定 URL の Google+ 検索結果を JSON で取得するには

http://plus.otter.topsy.com/trackbacks.json?url={url}&tracktype=googleplus__trackback

ちなみに特定 URL の Twitter の Tweet を取得するには

http://otter.topsy.com/trackbacks.json?url={url}&tracktype=tweet

PHP でサブディレクトリ内のファイル名をすべて取得

function get_files($dir, $pre = '', $excluded = '') {
    $result = array();
    $excluded = empty($excluded)
        ? array('.'.DIRECTORY_SEPARATOR, '..'.DIRECTORY_SEPARATOR)
        : (array)$excluded;
    if ( is_dir($dir) ) {
        if ( $dh = opendir($dir) ) {
            while ( ($file = readdir($dh)) !== FALSE ) {
                if ( is_dir($dir.$file) ) {
                    $file .= DIRECTORY_SEPARATOR;
                    if ( !(in_array($file, $excluded) || in_array($pre.$file, $excluded)) ) {
                        $result = array_merge($result, get_files($dir.$file, $pre.$file, $excluded));
                    }
                } else if ( !(in_array($file, $excluded) || in_array($pre.$file, $excluded)) ) {
                    $result[] = $pre.$file;
                }
            }
            closedir($dh);
        }
    }
    return $result;
}
$files = get_files('/path/to/');

PHP で IP アドレスをチェックする

function is_ip_range($ip, $range) {
	if (!($ip = ip2long($ip)))
		return FALSE;

	if(strchr($range, '/')){				//マスク指定
		list($net, $mask) = split('/', $range);

		if (!($net = ip2long($net)))
			return FALSE;
		if (is_numeric($mask))
			$mask = long2ip(bindec(str_repeat('1', $mask) . str_repeat('0', 32 - $mask)));
		if (!($mask = ip2long($mask)))
			return FALSE;

		return (($ip & $mask) === ($net & $mask)) ? TRUE : FALSE;

	} else if (strchr($range, '-')){		//範囲指定
		$range = split('-', $range);
		if (!($range[0] = ip2long($range[0])))
			return FALSE;
		if (!($range[1] = ip2long($range[1])))
			return FALSE;
		return ($range[0] <= $ip && $ip <= $range[1]) ? TRUE : FALSE;

	} else if ($range = ip2long($range)) {
		return ($range === $ip) ? TRUE : FALSE;
	}

	return FALSE;
}

is_ip_range('192.168.0.1', '192.168.0.0/24') とか is_ip_range('192.168.0.1', '192.168.0.0/255.255.255.0') とか is_ip_range('192.168.0.1', '192.168.0.1-192.168.0.254') とかすれば良いよ。

フォロー

Get every new post delivered to your Inbox.

現在1,816人フォロワーがいます。