Mobile-Menu iFuturz Infoweb Inc. Contact Portfolio

22

Jul

Display Latest Tweets with API v1.1 without using Plugin

Display Latest Tweets with API v1.1 without using Plugin

Posted On : July 22, 2013

| No Comment

We always think to display the tweets in our website and we are dependent of plugin for this ,we can do this without using the plugin here we have providing the full code how we can display tweets using the code.

Code 1:

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
//Add this below code in theme function.php file.
function buildBaseString($baseURI, $method, $params) {
    $r = array();
    ksort($params);
    foreach ($params as $key => $value) {
        $r[] = "$key=" . rawurlencode($value);
    } return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
 
function buildAuthorizationHeader($oauth) {
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach ($oauth as $key => $value)
        $values[] = "$key=\"" . rawurlencode($value) . "\""; $r .= implode(', ', $values);
    return $r;
}
 
function tweetslatest() {
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
 
$oauth_access_token = "enter your twitter account "Access token"
$oauth_access_token_secret = "enter your twitter account "Access token secret"
$consumer_key = "enter your twitter account "Consumer key:"
$consumer_secret = "enter your twitter account "Consumer secret:"
 
$oauth = array('oauth_consumer_key' => $consumer_key, 'oauth_nonce' => time(), 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => $oauth_access_token, 'oauth_timestamp' => time(), 'oauth_version' => '1.0');
 
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
 
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array(
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_HEADER => false, CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json);
$tweetsdata = "
<ul>";
for($i=0;$i&lt;2;$i++) {
     $tweetsdata.="
	<li>
 
".$twitter_data[$i]-&gt;text."</li>
"; // 2 tweets will be displayed
}
$tweetsdata.="</ul>
";
return $tweetsdata;
}

Code 2:

1
2
//Add below code in theme file where you want to display latest tweets
<!--?php echo tweetslatest();?-->

Screenshot

Comment