【Twitter bot 開発】柴犬botを作成(自動でつぶやかない版)

柴犬botを作りました。(推定知能指数:1)

 

参考サイト:

http://d.hatena.ne.jp/hirataka522/20080126/1201300282

http://blog.goo.ne.jp/wack-php/e/1f170daf1ad5149dca887127d129427c

 

上記サイトのコードを丸パクリしたソース(PHP)

//Twitter認証
$url = "http://twitter.com/statuses/update.xml?";
$username = "ユーザ名";
$password = "パスワード";

//鳴き声
$mes[0] = "ワン!";
$mes[1] = "ウー!";
$mes[2] = "ワオーン!!";
$mes[3] = "ウウー!";

//鳴く回数乱数の設定
$max = rand(0, 9);

//鳴く回数を$messageに代入
for( $i = 0; $i <= $max; $i++ ){
	$voice = rand(0, 3); //鳴き声乱数
	$message .= $mes[$voice];
}

//rawurlencode() でエンコード
$params = "status=". rawurlencode($message);

//Twitterにポストする
$result = file_get_contents($url.$params , false, stream_context_create(array(
	"http" => array(
		"method" => "POST",
		"header" => "Authorization: Basic ". base64_encode($username. ":". $password)
	)
)));

//リダイレクト
header('Location: http://twitter.com/shiba_inu_bot/') ;

 

次はcronで定期的に叩いて自動化します。

IT起業アカデミア