foreach foutmelding bij weergave tweets
foutmelding:
Warning: Invalid argument supplied for foreach() in /blabla/public_html/index.php on line 203
script:
Code (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
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
<?php
// Format datetime to relative time
function relativetime($sDate)
{
$timestamp = strtotime($sDate);
$now = time();
$timediff = floor($now - $timestamp);
switch(true)
{
case ($timediff < 60):
return $timediff.' seconden geleden';
case($timediff >= 60 && $timediff < 120):
return floor($timediff/60).' minuut geleden';
case($timediff >= 120 && $timediff < 3600):
return floor($timediff/60).' minuten geleden';
case($timediff >= 3600 && $timediff < 86400):
return floor($timediff/3600).' uur geleden';
case($timediff >= 86400 && $timediff < 172800):
return floor($timediff/86400).' dag geleden';
case($timediff >= 172800 && $timediff < 602800):
return floor($timediff/86400).' dagen geleden';
case($timediff >= 602800 && $timediff < 1209600):
return floor($timediff/602800).' week geleden';
case($timediff >= 1209600):
return floor($timediff/602800).' weken geleden';
}
}
// Format text to links
function formatlinks($text)
{
$text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $text);
$text = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);
$text = preg_replace("/@(\w+)/", "<a href=\"http://twitter.com/#!/\\1\" target=\"_blank\">@\\1</a>", $text);
$text = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $text);
return $text;
}
// Set parameters
$userid = '@blabla';
$feed = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$userid;
$limit = 3;
$c = 0;
$ch = curl_init();
$timeout = 5;
// CURL performance optimalisation
curl_setopt ($ch, CURLOPT_URL, $feed);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$feed = curl_exec($ch);
curl_close($ch);
// Unload XML feed and display
$twitter = new SimpleXMLElement($feed);
foreach($twitter->channel->item as $entry)
{
$c++;
$tweet = explode(':', $entry->title, 2);
echo ' <div class="tweet">
<div class="content">
<a href="http://twitter.com/#!/'.$tweet[0].'">'.$tweet[0].'</a> '.formatlinks($tweet[1]).'
</div>
<div class="bottom">
'.relativetime($entry->pubDate).'
</div>
</div>';
if($c == $limit) break;
}
?>
// Format datetime to relative time
function relativetime($sDate)
{
$timestamp = strtotime($sDate);
$now = time();
$timediff = floor($now - $timestamp);
switch(true)
{
case ($timediff < 60):
return $timediff.' seconden geleden';
case($timediff >= 60 && $timediff < 120):
return floor($timediff/60).' minuut geleden';
case($timediff >= 120 && $timediff < 3600):
return floor($timediff/60).' minuten geleden';
case($timediff >= 3600 && $timediff < 86400):
return floor($timediff/3600).' uur geleden';
case($timediff >= 86400 && $timediff < 172800):
return floor($timediff/86400).' dag geleden';
case($timediff >= 172800 && $timediff < 602800):
return floor($timediff/86400).' dagen geleden';
case($timediff >= 602800 && $timediff < 1209600):
return floor($timediff/602800).' week geleden';
case($timediff >= 1209600):
return floor($timediff/602800).' weken geleden';
}
}
// Format text to links
function formatlinks($text)
{
$text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $text);
$text = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);
$text = preg_replace("/@(\w+)/", "<a href=\"http://twitter.com/#!/\\1\" target=\"_blank\">@\\1</a>", $text);
$text = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $text);
return $text;
}
// Set parameters
$userid = '@blabla';
$feed = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$userid;
$limit = 3;
$c = 0;
$ch = curl_init();
$timeout = 5;
// CURL performance optimalisation
curl_setopt ($ch, CURLOPT_URL, $feed);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$feed = curl_exec($ch);
curl_close($ch);
// Unload XML feed and display
$twitter = new SimpleXMLElement($feed);
foreach($twitter->channel->item as $entry)
{
$c++;
$tweet = explode(':', $entry->title, 2);
echo ' <div class="tweet">
<div class="content">
<a href="http://twitter.com/#!/'.$tweet[0].'">'.$tweet[0].'</a> '.formatlinks($tweet[1]).'
</div>
<div class="bottom">
'.relativetime($entry->pubDate).'
</div>
</div>';
if($c == $limit) break;
}
?>
doe is een print_r op twitter en kijk wat er uitkomt :)