Skip to content

Instantly share code, notes, and snippets.

@kahagon
Last active December 29, 2015 09:59
Show Gist options
  • Select an option

  • Save kahagon/7654349 to your computer and use it in GitHub Desktop.

Select an option

Save kahagon/7654349 to your computer and use it in GitHub Desktop.
これで投票しようぜ!
<?php
$characters = array(
'eren' => 'エレン・イェーガー',
'mikasa' => 'ミカサ・アッカーマン',
'armin' => 'アルミン・アルレルト',
'jean' => 'ジャン・キルシュタイン',
'reiner' => 'ライナー・ブラウン',
'levi' => 'リヴァイ',
'zoe' => 'ハンジ・ゾエ',
'erwin' => 'エルヴィン・スミス',
'annie' => 'アニ・レオンハート',
'sasha' => 'サシャ・ブラウス',
'krista' => 'クリスタ・レンズ',
'ymir' => 'ユミル',
'bertolt' => 'ベルトルト・フーバー',
'marco' => 'まるこ・ボット',
'connie' => 'コニー・スプリンガー',
'oluo' => 'オルオ・ボザド',
'petra' => 'ペトラ・ラル',
'mike' => 'ミケ・ザカリアス',
'hannes' => 'ハンネス',
'rico' => 'リコ・ブレンチェスカ');
$url = 'http://shingeki-jra.jp/api/vote/';
$limit = 10;
$interval = 1000000;
if ($argc < 2) {
help();
return 1;
}
switch ($argv[1]) {
case 'post':
return post();
case 'list':
return list_characters();
case 'help':
default:
return help();
}
function help() {
print <<<EOT
使用例:
php shingeki-jra.php [コマンド名];
以下のコマンド使用可能です。
help:
このヘルプを表示します。
list:
投票可能なキャラクターの一覧を表示します。
post:
指定したキャラクターに票を投じます。
例えば次のようにコマンドを実行します。
php shingeki-jra.php post hannes
EOT;
return 0;
}
function post() {
global $argc, $argv, $characters, $url, $limit, $interval;
if ($argc < 3) {
help();
return 1;
}
$name = $argv[2];
if (!array_key_exists($name, $characters)) {
print '多分キャラクターの名前が間違っています。' . PHP_EOL;
return 1;
}
if ($argc > 3 && is_numeric($argv[3])) {
$limit = $argv[3];
}
if ($argc > 4 && is_numeric($argv[4])) {
$interval = $argv[4];
}
if (!function_exists('curl_init')) {
print 'cURL関数が使用できません' . PHP_EOL;
return 2;
}
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_POST, true);
curl_setopt($resource, CURLOPT_POSTFIELDS, array('key'=>$name));
curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
for ($i=0; $i<$limit; $i++) {
$response = json_decode(curl_exec($resource));
if ($response->status) {
print $characters[$name] . 'の得票数: ' . $response->data . PHP_EOL;
} else {
print '投票に失敗しました。' . PHP_EOL;
foreach ($response->errors as $k -> $v) {
print ' ' . $v . PHP_EOL;
}
}
usleep($interval);
}
curl_close($resource);
return 0;
}
function list_characters() {
global $characters;
foreach ($characters as $key => $name) {
print $name . ':' . $key . PHP_EOL;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment