「新宿駅」など、入力された場所と範囲からツイートを検索します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>場所を指定してツイートを検索</title> </head> <body> <?php // 場合の名前を取得 if (isset( $_POST [ "q" ]) and ! empty ( $_POST [ "q" ])) { $q = htmlspecialchars( $_POST [ "q" ], ENT_QUOTES); $dis = htmlspecialchars( $_POST [ "dis" ], ENT_QUOTES); } else { $skip = true; } ?> <p>場所を指定してツイートを検索</p> <!-- 検索キーワード --> <form method= "POST" action= "<?php echo $_SERVER[" SCRIPT_NAME "]; ?>" > 場所: <input type= "text" value= "<?php echo $q; ?>" name= "q" > <select name= "dis" > <option>1km</option> <option>3km</option> <option>5km</option> <option>10km</option> </select> <input type= "submit" value= "検索" name= "sub1" > </form> <?php // 検索の実行 if (! $skip ) { // 処理用のパラメータ $param = "q=" . rawurlencode( $q ); // 緯度経度を取得 $c = curl_init( "{$url}?{$param}" ); curl_setopt( $c , CURLOPT_RETURNTRANSFER, 1); $result = curl_exec( $c ); curl_close( $c ); $xml = simplexml_load_string( $result ); $lat = $xml ->coordinate->lat; $lng = $xml ->coordinate->lng; // Twitter APIのURLとパラメータ $param = "geocode={$lat},{$lng},{$dis}" ; // 検索を実行 $c = curl_init( "{$url}?{$param}" ); curl_setopt( $c , CURLOPT_RETURNTRANSFER, 1); $result = curl_exec( $c ); curl_close( $c ); $json = json_decode( $result ); // 結果を一覧表示 echo "緯度経度({$lat},{$lng})からの検索結果<hr />" ; ##del## foreach ( $json ->results as $res ) { echo $res ->text . "<hr />" ; } } ?> </body> </html> |
○実行結果