class-twitter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. include 'RestApi.php';
  3. /**
  4. * Twitter
  5. *
  6. * with help of the API this class delivers all kind of tweeted images from twitter
  7. *
  8. * @package socialstreams
  9. * @subpackage socialstreams/twitter
  10. * @author ThemePunch <info@themepunch.com>
  11. */
  12. class TP_twitter {
  13. /**
  14. * Consumer Key
  15. *
  16. * @since 1.0.0
  17. * @access private
  18. * @var string $consumer_key Consumer Key
  19. */
  20. private $consumer_key;
  21. /**
  22. * Consumer Secret
  23. *
  24. * @since 1.0.0
  25. * @access private
  26. * @var string $consumer_secret Consumer Secret
  27. */
  28. private $consumer_secret;
  29. /**
  30. * Access Token
  31. *
  32. * @since 1.0.0
  33. * @access private
  34. * @var string $access_token Access Token
  35. */
  36. private $access_token;
  37. /**
  38. * Access Token Secret
  39. *
  40. * @since 1.0.0
  41. * @access private
  42. * @var string $access_token_secret Access Token Secret
  43. */
  44. private $access_token_secret;
  45. /**
  46. * Initialize the class and set its properties.
  47. *
  48. * @since 1.0.0
  49. * @param string $api_key flickr API key.
  50. */
  51. public function __construct($consumer_key,$consumer_secret,$access_token,$access_token_secret) {
  52. $this->consumer_key = $consumer_key;
  53. $this->consumer_secret = $consumer_secret;
  54. $this->access_token = $access_token;
  55. $this->access_token_secret = $access_token_secret;
  56. }
  57. /**
  58. * Get Tweets
  59. *
  60. * @since 1.0.0
  61. * @param string $twitter_account Twitter account without trailing @ char
  62. */
  63. public function get_public_photos($twitter_account){
  64. $twitter = new \TwitterPhp\RestApi($this->consumer_key,$this->consumer_secret,$this->access_token,$this->access_token_secret);
  65. /*
  66. * Connect as application
  67. * https://dev.twitter.com/docs/auth/application-only-auth
  68. */
  69. $connection = $twitter->connectAsApplication();
  70. /*
  71. * Collection of the most recent Tweets posted by the user indicated by the screen_name, without replies
  72. * https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
  73. */
  74. $tweets = $connection->get('/statuses/user_timeline',array('screen_name' => $twitter_account, 'entities' => 1, 'trim_user' => 0 , 'exclude_replies' => 'true'));
  75. //var_dump($tweets);
  76. return $tweets;
  77. }
  78. /**
  79. * Find Key in array and return value (multidim array possible)
  80. *
  81. * @since 1.0.0
  82. * @param string $key Needle
  83. * @param array $form Haystack
  84. */
  85. public static function array_find_element_by_key($key, $form) {
  86. if (array_key_exists($key, $form)) {
  87. $ret =& $form[$key];
  88. return $ret;
  89. }
  90. foreach ($form as $k => $v) {
  91. if (is_array($v)) {
  92. $ret =TP_twitter::array_find_element_by_key($key, $form[$k]);
  93. if ($ret) {
  94. return $ret;
  95. }
  96. }
  97. }
  98. return FALSE;
  99. }
  100. /**
  101. * Prepare output array $stream
  102. *
  103. * @since 1.0.0
  104. * @param string $tweets Twitter Output Data
  105. */
  106. public static function makeClickableLinks($s) {
  107. return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
  108. }
  109. }
  110. ?>