現在、Twitter APIを使って、とあるWEBサービスを作っているのだが、初めてTwitter APIを使うので、その都度メモってみることにする。
まずは、検索系APIを使うことになったので、検索系APIをご紹介。
URL
リクエストURLは以下のアドレスとなる。
http://search.twitter.com/search.format
format部分には、jsonかatomを指定する。
私はjsonを使わないので、XML形式を前提として話を続けます。
atom形式を使うので、以下のURLになります。
http://search.twitter.com/search.atom
諸条件
- 認証:不要
- Method: GET
- API制限:対象
引数
q=検索文字列 (必須)
例
http://search.twitter.com/search.atom?q=twitter
- twitterという文字列を含むつぶやきを取得
- 日本語を検索する場合は、UTF-8でURLエンコードした文字列を渡す
lang (オプション) – 言語指定
検索対象となる言語を指定する。ISO639-1に準拠した文字列を指定します。ちなみに日本語は「ja」。
例
http://search.twitter.com/search.atom?q=twitter&lang=ja
- 日本語ユーザーで、twitterが含まれるつぶやきを検索
rpp (オプション) – 発言数指定
検索ページ1ページ分に含まれる発言数を指定する。
例
http://search.twitter.com/search.atom?q=twitter&rpp=20
- twitterという文字列を含んだものを1ページあたり20件取得する
- 最大100まで指定可能です
page (オプション) – ページ番号
ページ番号を指定する。(rpp単位で取得することになる)
例
http://search.twitter.com/search.atom?q=twitter&rpp=20&page=3
- twitterと言う文字列を含むつぶやきの1ページあたり20件表示の場合の3ページ目を取得する
- rpp × page は最大で1500まで
- ページ始まりは0ではなく、1
since_id (オプション)
指定したメッセージIDより新しいつぶやきを取得する。
例
http://search.twitter.com/search.atom?q=twitter&since_id=29384756
- twitterと言う文字列を含むつぶやきのうち、メッセージID「29384756」以降を取得する。
取得結果
以上のように、リスエスとすると、以下のようなATOM形式で結果を取得できます。
<?xml version="1.0" encoding="UTF-8" ?> - <feed xmlns:google="http://base.google.com/ns/1.0" xml:lang="en-US" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.w3.org/2005/Atom" xmlns:twitter="http://api.twitter.com/"> <id>tag:search.twitter.com,2005:search/twitter</id> <link type="text/html" href="http://search.twitter.com/search?q=twitter" rel="alternate" /> <link type="application/atom+xml" href="http://search.twitter.com/search.atom?q=twitter&lang=ja" rel="self" /> <title>twitter - Twitter Search</title> <link type="application/opensearchdescription+xml" href="http://search.twitter.com/opensearch.xml" rel="search" /> <link type="application/atom+xml" href="http://search.twitter.com/search.atom?lang=ja&q=twitter&since_id=4651181949" rel="refresh" /> <twitter:warning>adjusted since_id to 4465534659 (2009-09-29 08:00:00 UTC), requested since_id was older than allowedsince_id removed for pagination.</twitter:warning> <updated>2009-10-06T08:53:24Z</updated> <openSearch:itemsPerPage>15</openSearch:itemsPerPage> <openSearch:language>ja</openSearch:language> <link type="application/atom+xml" href="http://search.twitter.com/search.atom?lang=ja&max_id=4651181949&page=2&q=twitter" rel="next" /> - <entry> <id>tag:search.twitter.com,2005:4651181949</id> <published>2009-10-06T08:53:24Z</published> <link type="text/html" href="http://twitter.com/rai_lyco/statuses/4651181949" rel="alternate" /> <title>Twitterのブログウィジェット新しいの出てるんだったな。でもJavaカスタム出来る方のが好き…</title> <content type="html">Twitterのブログウィジェット新しいの出てるんだったな。でもJavaカスタム出来る方のが好き…</content> <updated>2009-10-06T08:53:24Z</updated> <link type="image/png" href="http://a1.twimg.com/profile_images/406428214/9485207_normal.jpg" rel="image" /> <twitter:source><a href="http://d.hatena.ne.jp/Kiri_Feather/20071121" rel="nofollow">Tween</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>rai_lyco (來)</name> <uri>http://twitter.com/rai_lyco</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651179634</id> <published>2009-10-06T08:53:10Z</published> <link type="text/html" href="http://twitter.com/kingken77/statuses/4651179634" rel="alternate" /> <title>おいしそう! 美味しそう!器も斜めになっていてオシャレp(^^)q http://f.hatena.ne.jp/twitter/20091006175306</title> <content type="html">おいしそう! 美味しそう!器も斜めになっていてオシャレp(^^)q <a href="http://f.hatena.ne.jp/twitter/20091006175306">http://f.hatena.ne.jp/<b>twitter</b>/20091006175306</a></content> <updated>2009-10-06T08:53:10Z</updated> <link type="image/png" href="http://a1.twimg.com/profile_images/427419152/PICT0021_normal.jpg" rel="image" /> <twitter:source><a href="http://movatwitter.jp/" rel="nofollow">movatwitter</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>kingken77 (Kenichi KANEDA)</name> <uri>http://twitter.com/kingken77</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651178970</id> <published>2009-10-06T08:53:07Z</published> <link type="text/html" href="http://twitter.com/widtaria/statuses/4651178970" rel="alternate" /> <title>twitterで秋篠宮殿下とか小沢一郎とか鳩山邦夫の偽アカウントが作られてるけど、ねいばーまとめも同じことできるよね。</title> <content type="html">twitterで秋篠宮殿下とか小沢一郎とか鳩山邦夫の偽アカウントが作られてるけど、ねいばーまとめも同じことできるよね。</content> <updated>2009-10-06T08:53:07Z</updated> <link type="image/png" href="http://a1.twimg.com/profile_images/23089322/SANY0788_normal.jpg" rel="image" /> <twitter:source><a href="http://twitter.com/">web</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>widtaria (Saitama Saito)</name> <uri>http://twitter.com/widtaria</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651178414</id> <published>2009-10-06T08:53:04Z</published> <link type="text/html" href="http://twitter.com/atsuko7/statuses/4651178414" rel="alternate" /> <title>今日 優勝か!? 間もなく プレイボール! 札幌ドーム http://f.hatena.ne.jp/twitter/20091006175300</title> <content type="html">今日 優勝か!? 間もなく プレイボール! 札幌ドーム <a href="http://f.hatena.ne.jp/twitter/20091006175300">http://f.hatena.ne.jp/<b>twitter</b>/20091006175300</a></content> <updated>2009-10-06T08:53:04Z</updated> <link type="image/png" href="http://a3.twimg.com/profile_images/265486155/1245164593623_normal.jpg" rel="image" /> <twitter:source><a href="http://movatwitter.jp/" rel="nofollow">movatwitter</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>atsuko7 (Atsuko Ozawa)</name> <uri>http://twitter.com/atsuko7</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651177369</id> <published>2009-10-06T08:52:58Z</published> <link type="text/html" href="http://twitter.com/soraplus/statuses/4651177369" rel="alternate" /> <title>何かいいアプリがあったら報告よろしく。 なんかwebの記事古いのばっかりで。@lindenbaum_ RT iphone+twitterの魅力に負けてみました!</title> <content type="html">何かいいアプリがあったら報告よろしく。 なんかwebの記事古いのばっかりで。@lindenbaum_ RT iphone+twitterの魅力に負けてみました!</content> <updated>2009-10-06T08:52:58Z</updated> <link type="image/png" href="http://a3.twimg.com/profile_images/454289733/3028725311_2aaf206108_s_normal.jpg" rel="image" /> <twitter:source><a href="http://twitter.com/">web</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>soraplus (sora+)</name> <uri>http://twitter.com/soraplus</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651174080</id> <published>2009-10-06T08:52:38Z</published> <link type="text/html" href="http://twitter.com/cdnair/statuses/4651174080" rel="alternate" /> <title>忙しいのを理由に妙な生き甲斐が RT @dcn: twitterにハマれる人は、余程忙しいか、余程暇かの何れかではないかと思う今日この頃。</title> <content type="html">忙しいのを理由に妙な生き甲斐が RT <a href="http://twitter.com/dcn">@dcn</a>: twitterにハマれる人は、余程忙しいか、余程暇かの何れかではないかと思う今日この頃。</content> <updated>2009-10-06T08:52:38Z</updated> <link type="image/png" href="http://a3.twimg.com/profile_images/373993925/new_2_normal.png" rel="image" /> <twitter:source><a href="http://echofon.com/" rel="nofollow">Echofon</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>cdnair (すがぬま まこと)</name> <uri>http://twitter.com/cdnair</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651172579</id> <published>2009-10-06T08:52:29Z</published> <link type="text/html" href="http://twitter.com/himoji/statuses/4651172579" rel="alternate" /> <title>偽者でないならあまりにお粗末。つまり偽者以外考えられない。 http://twitter.com/HatoyamaYukio</title> <content type="html">偽者でないならあまりにお粗末。つまり偽者以外考えられない。 <a href="http://twitter.com/HatoyamaYukio">http://<b>twitter</b>.com/HatoyamaYukio</a></content> <updated>2009-10-06T08:52:29Z</updated> <link type="image/png" href="http://a1.twimg.com/profile_images/417971122/avator_normal.jpg" rel="image" /> <twitter:source><a href="http://hp.vector.co.jp/authors/VA013135/twigadge.htm" rel="nofollow">twigadge</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>himoji (Tetsuya Ikeda)</name> <uri>http://twitter.com/himoji</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651171803</id> <published>2009-10-06T08:52:24Z</published> <link type="text/html" href="http://twitter.com/mahit0/statuses/4651171803" rel="alternate" /> <title>「SD アリーナとマーニャ」/「眞人」のイラスト [pixiv] http://bit.ly/3iX7zx #pixivtweet twitter始めたので登録してみます。</title> <content type="html">「SD アリーナとマーニャ」/「眞人」のイラスト [pixiv] <a href="http://bit.ly/3iX7zx">http://bit.ly/3iX7zx</a> <a href="http://search.twitter.com/search?q=%23pixivtweet">#pixivtweet</a> twitter始めたので登録してみます。</content> <updated>2009-10-06T08:52:24Z</updated> <link type="image/png" href="http://a3.twimg.com/profile_images/453948807/twitter_____normal.jpg" rel="image" /> <twitter:source><a href="http://twitter.com/">web</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>mahit0 (眞人)</name> <uri>http://twitter.com/mahit0</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651171781</id> <published>2009-10-06T08:52:24Z</published> <link type="text/html" href="http://twitter.com/greenjamtea4two/statuses/4651171781" rel="alternate" /> <title>晩御飯は 筍ご飯、じゃがいもの味噌汁、がめ煮、イカとピーマンの味噌 煮。@foodmemo http://f.hatena.ne.jp/twitter/20091006175222</title> <content type="html">晩御飯は 筍ご飯、じゃがいもの味噌汁、がめ煮、イカとピーマンの味噌 煮。@foodmemo <a href="http://f.hatena.ne.jp/twitter/20091006175222">http://f.hatena.ne.jp/<b>twitter</b>/20091006175222</a></content> <updated>2009-10-06T08:52:24Z</updated> <link type="image/png" href="http://a3.twimg.com/profile_images/362839693/___normal.JPG" rel="image" /> <twitter:source><a href="http://www.movatwitter.jp" rel="nofollow">モバツイッター</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>greenjamtea4two (2violet)</name> <uri>http://twitter.com/greenjamtea4two</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651168653</id> <published>2009-10-06T08:52:06Z</published> <link type="text/html" href="http://twitter.com/_tsuruchan/statuses/4651168653" rel="alternate" /> <title>仕事で使える-「Twitter」超入門 読みました! Twitterの凄さがわかった気がする★とても勉強になりました。 Tweetie買っちゃお。影響されやすいな〜(笑)</title> <content type="html">仕事で使える-「Twitter」超入門 読みました! Twitterの凄さがわかった気がする★とても勉強になりました。 Tweetie買っちゃお。影響されやすいな〜(笑)</content> <updated>2009-10-06T08:52:06Z</updated> <link type="image/png" href="http://s.twimg.com/a/1254440757/images/default_profile_6_normal.png" rel="image" /> <twitter:source><a href="http://twitter.com/">web</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>_tsuruchan (tsuruta)</name> <uri>http://twitter.com/_tsuruchan</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651168431</id> <published>2009-10-06T08:52:04Z</published> <link type="text/html" href="http://twitter.com/bsiyo/statuses/4651168431" rel="alternate" /> <title>Twitter: [N] 指定した新商品が出たら教えてくれるツイッターbot「mitsukap(みつかっぷ)」 : http://netafull.net/twitter/032281.html</title> <content type="html"><b>Twitter</b>: [N] 指定した新商品が出たら教えてくれるツイッターbot「mitsukap(みつかっぷ)」 : <a href="http://netafull.net/twitter/032281.html">http://netafull.net/<b>twitter</b>/032281.html</a></content> <updated>2009-10-06T08:52:04Z</updated> <link type="image/png" href="http://a1.twimg.com/profile_images/358539900/67f53414_normal.jpg" rel="image" /> <twitter:source><a href="http://apiwiki.twitter.com/" rel="nofollow">API</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>bsiyo (bsiyo)</name> <uri>http://twitter.com/bsiyo</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651167827</id> <published>2009-10-06T08:52:01Z</published> <link type="text/html" href="http://twitter.com/kyubokun/statuses/4651167827" rel="alternate" /> <title>接客アルバイトしかしたことない人向けの事務、実験補助用職務経歴書の書き方 [ http://twitter.com/Yagik0/status/4651155154 ]</title> <content type="html">接客アルバイトしかしたことない人向けの事務、実験補助用職務経歴書の書き方 [ <a href="http://twitter.com/Yagik0/status/4651155154">http://<b>twitter</b>.com/Yagik0/status/4651155154</a> ]</content> <updated>2009-10-06T08:52:01Z</updated> <link type="image/png" href="http://a1.twimg.com/profile_images/337652430/kyubokun_normal.png" rel="image" /> <twitter:source><a href="http://apiwiki.twitter.com/" rel="nofollow">API</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>kyubokun (急募くん)</name> <uri>http://twitter.com/kyubokun</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651167300</id> <published>2009-10-06T08:51:58Z</published> <link type="text/html" href="http://twitter.com/ilovetshirts/statuses/4651167300" rel="alternate" /> <title>Twitterって何で便利なのか理解しようとしている。</title> <content type="html">Twitterって何で便利なのか理解しようとしている。</content> <updated>2009-10-06T08:51:58Z</updated> <link type="image/png" href="http://s.twimg.com/a/1254440757/images/default_profile_0_normal.png" rel="image" /> <twitter:source><a href="http://twitter.com/">web</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>ilovetshirts (goodluck kim)</name> <uri>http://twitter.com/ilovetshirts</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651165913</id> <published>2009-10-06T08:51:50Z</published> <link type="text/html" href="http://twitter.com/yugolin/statuses/4651165913" rel="alternate" /> <title>はじめましてtwitter</title> <content type="html">はじめましてtwitter</content> <updated>2009-10-06T08:51:50Z</updated> <link type="image/png" href="http://a1.twimg.com/profile_images/455612252/______normal.png" rel="image" /> <twitter:source><a href="http://twitter.com/">web</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>yugolin (佐藤 由吾)</name> <uri>http://twitter.com/yugolin</uri> </author> </entry> - <entry> <id>tag:search.twitter.com,2005:4651164464</id> <published>2009-10-06T08:51:41Z</published> <link type="text/html" href="http://twitter.com/swisssss/statuses/4651164464" rel="alternate" /> <title>今、クラリネット13本植わってる http://f.hatena.ne.jp/twitter/20091006175132</title> <content type="html">今、クラリネット13本植わってる <a href="http://f.hatena.ne.jp/twitter/20091006175132">http://f.hatena.ne.jp/<b>twitter</b>/20091006175132</a></content> <updated>2009-10-06T08:51:41Z</updated> <link type="image/png" href="http://a3.twimg.com/profile_images/442106225/05_normal.JPG" rel="image" /> <twitter:source><a href="http://movatwitter.jp/" rel="nofollow">movatwitter</a></twitter:source> <twitter:lang>ja</twitter:lang> - <author> <name>swisssss (スイス)</name> <uri>http://twitter.com/swisssss</uri> </author> </entry> </feed>
あとはこれをXMLのパーサに通せばできあがり。
以下のサイトを参考にさせて頂きました。
関連記事
- [Twitter] ホームページにRetweetリンクを付ける方法
- [空目アワー] の bot作りました
- Twitterでの「RT」の意味がようやく分かった(^^;)
- [空目アワー] Mashup Award 5 にエントリーしてみました
- Google Font APIの使い方
- Twitterで出てくる「#」マークは何なの??
- TwitterでRTされた発言の読者数を調べるサービス「ReTweeter」
- TwitterでフォローOKなユーザを探すサイト「follow ok」
Sponsored Link
Google+
zenback
- Newer: [CakePHP] レコードからランダムに1件取得する方法
- Older: [WP] WP Super Cache と DB Cache Reloaded を一緒に入れたら、WordPressがクラッシュした
Comments:0
Trackbacks:1
- Trackback URL for this entry
- http://www.multiburst.net/sometime-php/2009/10/twitter-search-api/trackback/
- Listed below are links to weblogs that reference
- [Twitter] 検索系APIの使い方 from Sometime PHP
- pingback - phpでTwitter APIの検索結果を取得してみる練習 / synapse28.com より 2011/05/12
[...] ■今回参考にさせていただいたサイト ・[Twitter] 検索系APIの使い方 [Sometime PHP] ・Twitter API 仕様書 ・PHPとTwitterAPIを利用して、自分のタイムラインを表示してみる@デザイナー [caraldo.net] [...]
Additional comments powered by BackType