PageRenderTime 74ms CodeModel.GetById 45ms RepoModel.GetById 0ms app.codeStats 0ms

/app/views/page/tw_search.blade.php

https://bitbucket.org/funisimo/funisimo_cms
PHP | 203 lines | 100 code | 47 blank | 56 comment | 6 complexity | aed207207d0a0283e4eada7abfc56b68 MD5 | raw file
  1. @extends('start')
  2. @section('content')
  3. <script type="text/javascript" src="js/2-jquery.js"></script>
  4. <h1>twitter page</h1>
  5. <?php
  6. /*
  7. * Author: Robert Danklefsen
  8. * Website: http://www.catchylabs.com
  9. * Email: bobby@catchylabs.com
  10. *
  11. * This simple widget returns a simple twitter feed using standard OAuth now
  12. * required by Twitter's 1.1 API.
  13. *
  14. * To use this, you must first set up an app in the developer tools at http://dev.twitter.com/apps
  15. * Just sign in with your twitter account.
  16. * Use the website that is going to access the feed as the app name and website.
  17. * Once created, create Token keys and insert them in the appropriate fields at line 61.
  18. */
  19. //function takes the time from the tweet and computes a "time ago"
  20. function time_elapsed_string($ptime) {
  21. $etime = time() - $ptime;
  22. if ($etime < 1) {
  23. return '0 seconds';
  24. }
  25. $a = array( 12 * 30 * 24 * 60 * 60 => 'year',
  26. 30 * 24 * 60 * 60 => 'month',
  27. 24 * 60 * 60 => 'day',
  28. 60 * 60 => 'hour',
  29. 60 => 'minute',
  30. 1 => 'second'
  31. );
  32. foreach ($a as $secs => $str) {
  33. $d = $etime / $secs;
  34. if ($d >= 1) {
  35. $r = round($d);
  36. return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
  37. }
  38. }
  39. }
  40. //makes urls out of links, @usernames, and hash tags.
  41. function makeURLs($text) {
  42. // Match URLs
  43. $text = preg_replace('`\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))`', '<a href="$0">$0</a>', $text);
  44. // Match @name
  45. $text = preg_replace('/(@)([a-zA-Z0-9\_]+)/', '@<a href="https://twitter.com/$2">$2</a>', $text);
  46. // Match #hashtag
  47. $text = preg_replace('/(#)([a-zA-Z0-9\_]+)/', '<a href="https://twitter.com/search/?q=%23$2">#$2</a>', $text);
  48. return $text;
  49. }
  50. //the magic. Returns the feed in a unorder listed.
  51. //to use - echo getTwitterFeed();
  52. function getTwitterFeed () {
  53. //need to create an app in twitter dev and get OAuth codes. Put them here
  54. $token = '1529970560-5s7M7VefaAv06gHmXCsC7I1JQyInF9Ep4Mf5eM9';
  55. $token_secret = 'KRnxK8g7p5WN9UA8oqoHft1xXFZ12VOL5BVpRgHAQ';
  56. $consumer_key = 'Z6HIziHA3jEZ9cwGOs2zMQ';
  57. $consumer_secret = '0Wfj7NO8N6nIb6iQk67OUiPhsbFSHD9D6BWpBGaSQo';
  58. $host = 'api.twitter.com';
  59. $method = 'GET';
  60. $path = '/1.1/search/tweets.json'; // api call path
  61. //edit these too.
  62. $query = array( // query parameters
  63. //'screen_name' => 'TLC',
  64. //'text' => 'lol',
  65. //'count' => '5' //0 returns all (limit is 200 i think),
  66. 'q' => 'noradio'
  67. );
  68. $oauth = array(
  69. 'oauth_consumer_key' => $consumer_key,
  70. 'oauth_token' => $token,
  71. 'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
  72. 'oauth_timestamp' => time(),
  73. 'oauth_signature_method' => 'HMAC-SHA1',
  74. 'oauth_version' => '1.0'
  75. );
  76. $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
  77. $query = array_map("rawurlencode", $query);
  78. $arr = array_merge($oauth, $query); // combine the values THEN sort
  79. asort($arr); // secondary sort (value)
  80. ksort($arr); // primary sort (key)
  81. // http_build_query automatically encodes, but our parameters
  82. // are already encoded, and must be by this point, so we undo
  83. // the encoding step
  84. $querystring = urldecode(http_build_query($arr, '', '&'));
  85. $url = "https://$host$path";
  86. // mash everything together for the text to hash
  87. $base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
  88. // same with the key
  89. $key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
  90. // generate the hash
  91. $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
  92. // this time we're using a normal GET query, and we're only encoding the query params
  93. // (without the oauth params)
  94. $url .= "?".http_build_query($query);
  95. $url=str_replace("&amp;","&",$url); //Patch by @Frewuill
  96. echo $url.'<br>';
  97. //echo $url;
  98. //$url = 'https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4';
  99. $oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
  100. ksort($oauth); // probably not necessary, but twitter's demo does it
  101. // also not necessary, but twitter's demo does this too
  102. function add_quotes($str) { return '"'.$str.'"'; }
  103. $oauth = array_map("add_quotes", $oauth);
  104. // this is the full value of the Authorization line
  105. $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
  106. // if you're doing post, you need to skip the GET building above
  107. // and instead supply query parameters to CURLOPT_POSTFIELDS
  108. $options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
  109. //CURLOPT_POSTFIELDS => $postfields,
  110. CURLOPT_HEADER => false,
  111. CURLOPT_URL => $url,
  112. CURLOPT_RETURNTRANSFER => true,
  113. CURLOPT_SSL_VERIFYPEER => false);
  114. // do our business
  115. $feed = curl_init();
  116. curl_setopt_array($feed, $options);
  117. $json = curl_exec($feed);
  118. curl_close($feed);
  119. $twitter_data = json_decode($json,true);
  120. //print_r($twitter_data);
  121. if (empty($twitter_data)) {
  122. $code = 'There was an error';
  123. } else {
  124. $code = '<ul id="twitterFeed">';
  125. // print_r($twitter_data);
  126. d($twitter_data);
  127. echo $tw_url = $twitter_data['search_metadata']['refresh_url'];
  128. d($twitter_data['statuses']);
  129. foreach ($twitter_data['statuses'] as $keys ) {
  130. foreach ($keys as $key => $value) {
  131. if($key == 'text'){
  132. echo $value.'<br>';
  133. }
  134. }
  135. }
  136. // if(isset($tweets['metadata']['id'])){
  137. // echo $tweets['metadata']['id'];
  138. // }
  139. // $text = makeURLs($tweets['text']);
  140. // $time = strtotime($tweets['created_at']);
  141. // $url = 'http://twitter.com/'.$tweets['user']['screen_name'].'/status/'.$tweets['id'];
  142. // $agoTime = time_elapsed_string($time);
  143. // $code .= '<li>';
  144. // $code .= '<span class="twitter-date"><a href="'.$url.'">'.$agoTime .'</a></span>';
  145. // $code .= '<br />';
  146. // $code .= '<span class="twitter-text">'. $text . '</span>';
  147. // $code .= '</li>';
  148. $code .= '</ul>';
  149. }
  150. return $code;
  151. }
  152. //for testing
  153. echo getTwitterFeed();
  154. ?>
  155. {{HTML::script('js/tw.js')}}
  156. @stop