今回緊急で研究室のweb勉強会に参加しました。(一人抜けたため)
で、自分が出来るスクリプト言語はphpなのでphpをやることに決まりました。
スライド
http://sdrv.ms/LfBff0
完成品
http://iplab.u-aizu.ac.jp/about/twitter_info.php
勉強会templateはこちら https://docs.google.com/open?id=0B1ThAuM5WTrHX0E4X2IzQ0c5Nkk
内容はtwitter APIを叩く内容です。
JSとphpとHTMLを使用しています。
一番難しかったのはhtmlのボタンでenter送信を防ぐこと。ここでjsをつかいました。
ソース(HTML組み込みです)
<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <!--twiiterはUTF-8のため --> <title>twitterの個人情報取得サイト</title> </head> <body> <form method="POST" action="twitter_info.php"/> <p>名前を入れてくださいね。</p> <p>TLは200件取得</p> <p> 今後取得量等を変更します</p> <input type="text" name="text_field" onKeyDown="javascript: return event.keyCode==13?false:true;"> <!-- enter防止のため3つめに追加 --> <input type="submit" value="information" name="Method"> <input type="submit" value="get_TL" name="Method"> <p> <?php function url_exists($url) { $header = @get_headers($url); if(preg_match('#^HTTP/.*\s+[200|302]+\s#i', $header[0])) { return true; } return false; } if(isset($_POST["Method"])){ if($_POST["text_field"] !== NULL){ $screen_name = trim($_POST["text_field"]); if($_POST["Method"] === "information"){ $url1 = "http://api.twitter.com/1/users/show.xml?screen_name={$screen_name}"; if(url_exists($url1) === true){ $contents1 = file_GET_contents($url1); $info = simplexml_load_string($contents1); echo "<img src=\"$info->profile_image_url\"/><br />"; echo "id: $info->id <br />"; echo "name: $info->name<br />"; echo "screen_name: $info->screen_name<br />"; echo "profile: $info->description<br />"; echo "friends: $info->friends_count<br />"; echo "followers: $info->followers_count<br />"; echo "favourites: $info->favourites_count<br />"; echo "tweet: $info->statuses_count<br />"; } else echo "そのユーザーは存在しないよ!<br />"; } } if($_POST["Method"] === "get_TL"){ $url2 = "http://twitter.com/statuses/user_timeline.xml?screen_name={$screen_name}&count=200"; if(url_exists($url2) === true){ $contents2 = file_GET_contents($url2); $tweet = simplexml_load_string($contents2); foreach ($tweet->status as $i) { $val = $i->text; $day = date("m月H時i分",strtotime( $i->created_at ) ); echo "$day $val<br />"; } for($i = 0;$i < count($tweet->status);$i++){ echo $tweet->status[$i]->text; } } else{ echo "取得できませんでした(´・ω・`) <br />"; echo "原因: そのユーザーが存在しないか鍵垢です"; } } } ?> </form> </body> </html>