# Topsyから特定のURLに関するつぶやきをWordPressっぽく取得できるClassを書いてみた。

- リンク: https://torounit.com/blog/topsy%e3%81%a7%e7%89%b9%e5%ae%9a%e3%81%aeurl%e3%81%ab%e9%96%a2%e3%81%99%e3%82%8b%e3%81%a4%e3%81%b6%e3%82%84%e3%81%8d%e3%81%aa%e3%81%a9%e3%82%92wordpress%e7%9a%84%e3%81%ab%e4%bd%bf%e3%81%88%e3%82%8bcla/
- 公開日: 2012-01-27T10:00:14+09:00
- 作成者: Toro\_Unit

このサイトのリニューアルこの間リニューアルしたときに作りました。晒そうと思ってすっかり
忘れてました。このブログの個別記事の下の方のTwitterタイムラインがそれです。
 Topsy
で特定のURLに関するつぶやきや、そのユーザーなどを取得します。

functions.phpにでも貼っておくと良いと思います。いちおうPHP5用。PHP4系で使いたい場合
は__cunstructをGet_Topsy_Trackbackに変えてあげて下さい。

# コード

[php]
 class Get_Topsy_Trackback { private $counter = 0; public $response; public
$trackback;

 function __construct($permalink){
 $topsy = json_decode(file_get_contents("http://
otter.topsy.com/trackbacks.json?url=".$permalink)); $this->response = $topsy->response;}

 function get_all_topsy(){
 return count($this->response->list); }

 function have_topsy(){
 if(isset($this->response->list[$this->counter])){ return
true; }else{ return false; } }

 function the_topsy(){
 $this->trackback = $this->response->list[$this->counter];
$this->counter ++; }

 function get_topsy(){
 return $this->trackback; }

 function get_the_content(){
 $content = $this->trackback->content; return preg_replace(‘/(
https?:\/\/[a-zA-Z0-9\.\/:%,!#~*@&_-]+)/’,'<a href="\0" title="\0">\0</a>’,$content);}
function the_content(){ echo "<p>".$this->get_the_content()."</p>"; }

 function get_the_author(){
 return $this->trackback->author; }

 function get_the_author_nick(){
 return $this->get_the_author()->nick; } function
the_author_nick(){ echo $this->get_the_author_nick(); }

 function get_the_author_link(){
 return $this->get_the_author()->url; } function
the_author_link(){ echo $this->get_the_author_link(); }

 function the_author(){
 echo ‘<a href="’.$this->get_the_author_link().’" class="
author">’.$this->get_the_author_nick().'</a>’; }

 function get_the_author_avator(){
 return $this->get_the_author()->photo_url; }
function the_author_avator( $size = 48 ){ echo ‘<img src="’.$this->get_the_author_avator().’"
alt="’.$this->get_the_author_nick().’" width="’.$size.’" height="’.$size.’" />’;}

 function get_the_time( $format = "Y-m-d" ){
 return date($format,$this->trackback-
>date); } function the_time( $format = "Y-m-d" ){ echo $this->get_the_time( $format);}

 function get_the_permalink(){
 return $this->trackback->permalink_url; } function
the_permalink(){ echo $this->get_the_permalink(); } } [/php]

基本的にgetから始まるメソッドはreturnで返し、theから始まるメソッドはechoします。
実にWordPressっぽいですよね？

# 使い方

こんな感じで使います。
 [php] <?php $topsy = new Get_Topsy_Trackback($URL); if($
topsy->have_topsy()):?> <p><?php echo $topsy->get_all_topsy();?>回つぶやかれました。
</p> <ul> <?php while($topsy->have_topsy()):$topsy->the_topsy(); ?> <li> <a href
="<?php $topsy->the_author_link();?>"><?php $topsy->the_author_avator()?></a> <div
class="content"><?php $topsy->the_author();?><?php $topsy->the_content();?></div
> <div class="meta">Post:<a href="<?php $topsy->the_permalink();?>"><?php $topsy-
>the_time("Y-m-d");?></a></div> </li> <?php endwhile; ?> </ul> <?php else:?> <p>
まだだれもつぶやいていない様子です。</p> <?php endif;?> [/php]

インスタンス化するときに、URLを引数に入れてあげましょう。WordPressで使うのならば
get_permalink(get_the_ID())とかですね。
 あとはWordPressっぽいループタグを書いて、
WordPressっぽいテンプレートタグを書いてあげればオーケーです。

需要があるかどうか解らないけれど結構便利だと思うので、Topsy使ってみたいけど・・・
って人は使ってみて下さい！

## コメント

### 投稿者: Takuyamori 0203

- リンク: https://torounit.com/blog/topsy%e3%81%a7%e7%89%b9%e5%ae%9a%e3%81%aeurl%e3%81%ab%e9%96%a2%e3%81%99%e3%82%8b%e3%81%a4%e3%81%b6%e3%82%84%e3%81%8d%e3%81%aa%e3%81%a9%e3%82%92wordpress%e7%9a%84%e3%81%ab%e4%bd%bf%e3%81%88%e3%82%8bcla/#comment-910
- 公開日: 2012-06-05T18:12:00+09:00

 はじめまして。
 ちょうどサイト内に特定URLに関するつぶやきを表示したく検索してい
たら、こちらにたどりつきました。 このような記事を書いていただきありがとうございます。

さっそく上記のコードを実装したのですが、何点かご教授お願いします。
 ■質問1 「●●回
つぶやかれました」の部分ですが、 仮に100件つぶやかれても「10件」と表示されるよう
です。 最大件数が表示されるようするにはどこを修正したら良いのでしょうか？

■質問2
 現在のままだと表示件数は10件となりますが、 10件以上のつぶやきがある場合は、「
全てのつぶやき見る」ボタンを設置し、 TOPSYの検索結果ページリンクしたいです。

大変あつかましい質問ですが、
 よろしくお願いいたします。
