-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstagram.class.php
More file actions
78 lines (60 loc) · 2.48 KB
/
Instagram.class.php
File metadata and controls
78 lines (60 loc) · 2.48 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
<?php
class Instagram extends SocialMedia
{
private $app_key;
private $app_secret;
private $instagram_id;
private $endpoint;
protected $cache_key = "instagram";
function __construct($app_key, $app_secret, $instagram_id, $endpoint = "users")
{
$this->app_key = $app_key;
$this->app_secret = $app_secret;
$this->instagram_id = $instagram_id;
$this->endpoint = $endpoint;
$this->cache_key = $this->cache_key . $instagram_id . $endpoint;
}
public function loadPosts($start = 0, $count = 1)
{
$latest_posts = array();
$url = "https://api.instagram.com/v1/" . $this->endpoint . "/" . $this->instagram_id . "/media/recent/?client_id=" . $this->app_key . "&count=" . ($start + $count);
$data = @json_decode(file_get_contents($url), true);
if (isset($data["data"])) {
$i = 0;
$j = 0;
foreach ($data["data"] as $item) {
if ($i >= $start && $j < $count) {
$latest_posts[] = array(
"date" => $item["created_time"],
"text" => isset($item["caption"]) ? $this->addUrls($item["caption"]["text"]) : false,
"url" => $item["link"],
"picture" => isset($item["images"]) ? $item["images"]["standard_resolution"]["url"] : false
);
$j++;
}
$i++;
}
}
return $latest_posts;
}
/*
* Adds links to usernames but not hashtags
*/
protected function addUrls($text)
{
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
// force http: on www.
$text = preg_replace("@www\.@", "http://www.", $text);
// eliminate duplicates after force
$text = preg_replace("@http://http://www\.@", "http://www.", $text);
$text = preg_replace("@https://http://www\.@", "https://www.", $text);
if (preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
$text = preg_replace($reg_exUrl, '<a href="' . $url[0] . '" rel="nofollow" target="_blank">' . $url[0] . '</a>', $text);
}
$text = preg_replace("/@(\w+)/", '<a href="http://instagram.com/$1" target="_blank">@$1</a>', $text);
return $text;
}
}