forked from subdesign/laravel-bundle-twitterfeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.php
85 lines (65 loc) · 2.11 KB
/
twitter.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Twitterfeed - for Laravel Framework
*
* @author Boris Strahija (boris@creolab.hr)
* @author Barna Szalai (info@subdesign.hu)
* @copyright Copyright (c) 2012 Barna Szalai
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 1.0.0
*
*/
class Twitter {
protected static $base_url = 'http://api.twitter.com/1/';
public static function timeline($username = null, $num = 5)
{
if ( ! $username) $username = Config::get('bundle::twitter.default_user');
if ($tweets = Cache::get('twitter_timeline_'.$username.'_'.$num))
{
return $tweets;
}
else
{
$call_url = self::$base_url.'statuses/user_timeline.json?screen_name='.$username.'&count='.$num;
$tweets = json_decode(file_get_contents($call_url));
if($tweets)
{
foreach ($tweets as $key=>$tweet)
{
$tweets[$key]->text = self::build_link((string) $tweet->text);
$tweets[$key]->when = strtotime((string) $tweet->created_at);
$tweets[$key]->author = (string) $tweet->user->name;
}
Cache::put('twitter_timeline_'.$username.'_'.$num, $tweets, Config::get('bundle::twitter.cache_ttl'));
return $tweets;
}
}
return null;
}
public static function timeline_list($username = null, $num = 5, $return = false)
{
$tweets = self::timeline($username, $num);
if ($tweets)
{
$html = '<ul class="twitter">';
foreach ($tweets as $tweet)
{
$html .= '<li><p>'.$tweet->text.'</p>';
$html .= '</li>';
}
$html .= '</ul>';
if ($return) return $html;
else echo $html;
}
return null;
}
public static function build_link($string = '')
{
$search = array('|#([\w_]+)|', '|@([\w_]+)|');
$replace = array('<a href="http://search.twitter.com/search?q=%23$1" target="_blank">#$1</a>', '<a href="http://twitter.com/$1" target="_blank">@$1</a>');
$string = preg_replace($search, $replace, $string);
$string = " " . $string . " ";
$string = preg_replace('/\s(http|https)\:\/\/(.+?)\s/m', ' <a href="$1://$2" target="_blank">$1://$2</a>', $string);
return $string;
}
}