-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocialparser.php
More file actions
executable file
·123 lines (107 loc) · 4.37 KB
/
socialparser.php
File metadata and controls
executable file
·123 lines (107 loc) · 4.37 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
Class Name: Social Parser
Author: Andrea Morone
Author URI: http://andreamorone.github.io
Project URI: http://andreamorone.github.io/social-parser
Description: This class can retrieve latest posts from facebook, twitter, youtube and google plus. This is base implementation, you can add yuor own function or extend existing. For example you can check is response is empty or add more social networks.
*/
class Socialparser {
/** FACEBOOK PARSE **/
public function facebook($id,$num){
$loader = 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num='.$num.'&q=';
$UriEncoded = $this->encodeURIComponent('https://www.facebook.com/feeds/page.php?id='.$id.'&format=rss20');
$url = $loader.$UriEncoded;
$response = $this->curl_call($url);
$entries = $response['responseData']['feed']['entries'];
$posts = array();
foreach($entries as $post){
array_push($posts,$post);
}
return $posts;
}
/** END FACEBOOK PARSE **/
/** YOUTUBE PARSE **/
public function youtube($id,$num){
$loader = 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num='.$num.'&q=';
$UriEncoded= $this->encodeURIComponent('https://gdata.youtube.com/feeds/base/users/'.$id.'/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile');
$data = $loader.$UriEncoded;
$response = $this->curl_call($data);
$entries = $response['responseData']['feed']['entries'];
$videos = array();
foreach($entries as $video){
$videocontent = array(
"title" => $video['title'],
"id" => substr( $video['link'], strrpos( $video['link'], '?v=' )+3, -22)
);
array_push($videos,$videocontent);
}
return $videos;
}
/** END YOUTUBE PARSE **/
/** TWITTER PARSE **/
public function twitter($id,$num){
require_once("twitter_library/twitteroauth.php");
$consumerkey = "YOUR API KEY";
$consumersecret = "YOUR API SECRET";
$accesstoken = "YOUR API ACCESS TOKEN";
$accesstokensecret = "YOUR API ACCESS TOKEN SECRET";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$returnedtweets = $connection->get('statuses/user_timeline', array('screen_name' => $id, 'exclude_replies' => 'false', 'include_rts' => 'true', 'count' => $num));
$tweets = array();
if(!empty($returnedtweets)) {
foreach($returnedtweets as $tweet) {
$tweetImage = $tweet->entities->media[0]->media_url;
$tweetText = $tweet->text;
$tweetText = preg_replace("/\B#(.\w+)\w*/", '<a href="https://twitter.com/search?q=$1" target="_blank">$0</a>', $tweetText);
$tweetText = preg_replace("#(http://|(www.))(([^s<]{4,68})[^s<]*)#", '<a href="https://$2$3" target="_blank">$1$2$4</a>', $tweetText);
$tweetText = preg_replace("/\B@(.\w+)\w*/", '<a href="https://www.twitter.com/$1" target="_blank">@$1</a>', $tweetText);
$tweetcontent = array(
'text' => $tweetText ,
'image' => $tweetImage
);
array_push($tweets, $tweetcontent);
}
}
return $tweets;
}
/** END TWITTER PARSE **/
/** GOOGLE PLUS PARSE **/
public function google($id,$num){
$key = 'INSERT YOUR GOOGLE API KEY';
$url = 'https://www.googleapis.com/plus/v1/people/'.$id.'/activities/public?key='.$key.'&maxResults='.$num;
$response = $this->curl_call($url);
$entries = $response['items'];
$posts = array();
foreach($entries as $post){
$postcontent = array(
'title' => $post['title'],
'url' => $post['url'],
'image' => $post['object']['attachments'][0]['fullImage']['url']
);
array_push($posts,$postcontent);
}
return $posts;
}
/** END GOOGLE PLUS PARSE **/
/** MISC **/
private function curl_call($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$response = json_decode($output, true);
return $response;
curl_close($ch);
}
private function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}
/** END MISC **/
}
?>