8
8
exit ;
9
9
}
10
10
11
+ // Catches realtime updates from Instagram
11
12
if ($ _SERVER ['REQUEST_METHOD ' ]==='POST ' ) {
13
+
12
14
// Instantiates Pusher PHP API
13
15
require 'lib/Pusher.php ' ;
14
16
17
+ // Retrieves the POST data from Instagram
15
18
$ update = file_get_contents ('php://input ' );
16
-
17
19
$ photos = json_decode ($ update );
18
- if (is_array ($ photos )) {
19
- $ length = count ($ photos );
20
- }
21
20
22
- if ($ length >0 ) {
21
+ // If one or more photos has been posted, notify all clients
22
+ if (is_array ($ photos ) && ($ length =count ($ photos ))>0 ) {
23
23
$ pusher = new Pusher ($ pusher_key , $ pusher_secret , $ pusher_app_id );
24
24
$ pusher ->trigger (
25
25
'photos ' ,
29
29
)
30
30
);
31
31
}
32
+
32
33
}
33
34
34
- // Get most recent IG photos
35
+ // Retrieves the access token from the query string
35
36
$ token = isset ($ _GET ['access_token ' ]) ? $ _GET ['access_token ' ] : NULL ;
36
- $ tag = isset ($ _GET ['tag ' ]) ? $ _GET ['tag ' ] : 'instacat ' ;
37
-
38
- $ api_url = 'https://api.instagram.com/v1/tags/ '
39
- . $ tag . '/media/recent?count=16&access_token= ' . $ token ;
40
-
41
- $ page_url = 'http:// ' . $ _SERVER ['SERVER_NAME ' ]
42
- . dirname ($ _SERVER ['REQUEST_URI ' ])
43
- . '/?access_token= ' . $ token
44
- . '&tag= ' . $ tag ;
45
-
46
- $ ch = curl_init ($ api_url );
47
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
48
- $ response = json_decode (curl_exec ($ ch ));
49
- $ photos = property_exists ($ response , 'data ' ) ? $ response ->data : array ();
50
37
51
- /*
52
-
53
- https://api.instagram.com/v1/tags/nofilter/media/recent
54
-
55
- curl -F 'client_id=6019cd09a31d488eab0a8e072f408415' \
56
- -F 'client_secret=d1fcd47c601e4d80912f5168fc8efaa7' \
57
- -F 'object=tag' \
58
- -F 'aspect=media' \
59
- -F 'object_id=catstagram' \
60
- -F 'callback_url=http://demo.copterlabs.com/filive/workshop/' \
61
- https://api.instagram.com/v1/subscriptions/
62
-
63
- curl -X DELETE 'https://api.instagram.com/v1/subscriptions?client_secret=d1fcd47c601e4d80912f5168fc8efaa7&client_id=6019cd09a31d488eab0a8e072f408415&object=all'
64
-
65
- */
38
+ // Generates the login URL to authorize the app
39
+ $ login_url = 'https://api.instagram.com/oauth/authorize/ '
40
+ . '?client_id= ' . $ ig_client_id
41
+ . '&redirect_uri= ' . $ ig_login_uri
42
+ . '&response_type=code '
66
43
67
44
?>
68
45
<!doctype html>
80
57
81
58
<body>
82
59
83
- <?php if ($ token !==NULL ): ?>
60
+ <?php if ($ token ===NULL ): ?>
61
+
62
+ <header>
63
+ <h1>Log in to start playing with realtime!</h1>
64
+ </header>
65
+
66
+ <article>
67
+ <a href="<?= $ login_url?> "
68
+ class="login button">Login →</a>
69
+ </article>
70
+
71
+ <?php else : ?>
84
72
85
73
<header>
86
74
<h1>Photos tagged with #<?= $ tag?> </h1>
@@ -98,147 +86,41 @@ class="button"
98
86
</div>
99
87
100
88
<ul id="photos">
101
-
102
- <?php //foreach ($photos as $photo): ?>
103
89
<li class="loading">Loading…</li>
104
- <!-- <li>
105
- <a href="<?= $ photo ->link ?> ">
106
- <img src="<?= $ photo ->images ->thumbnail ->url ?> "
107
- alt="<?= (empty ($ photo ->caption )) ? $ photo ->caption ->text : NULL ?> "
108
- data-id="<?= $ photo ->id ?> " />
109
- <strong>Photo by <?= $ photo ->user ->username ?> </strong>
110
- </a>
111
- </li> -->
112
- <?php //endforeach; ?>
113
-
114
90
</ul><!--/#photos-->
115
91
116
92
</article>
117
93
118
- <?php else : ?>
119
-
120
- <header>
121
- <h1>Log in to start playing with realtime!</h1>
122
- </header>
123
-
124
- <article>
125
- <a href="https://api.instagram.com/oauth/authorize/?client_id=<?= $ ig_client_id?> &redirect_uri=http://demo.copterlabs.com/filive/workshop/login.php&response_type=code"
126
- class="login button">Login →</a>
127
- </article>
128
-
129
94
<?php endif ; ?>
130
95
131
96
<footer>
132
97
<p>
133
98
This demo was created by
134
- <a href="http://www.lengstorf.com/">Jason Lengstorf</a> for use at the
135
- realtime workshop at Future Insights Live 2013. It is released under the
136
- MIT License .
99
+ <a href="http://www.lengstorf.com/">Jason Lengstorf</a> for use in the
100
+ realtime workshop at Future Insights Live 2013. All photos belong to
101
+ their respective owners .
137
102
</p>
138
103
</footer>
139
104
140
105
<script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
141
106
<script src="http://js.pusher.com/2.0/pusher.min.js"></script>
142
107
<script>
143
108
144
- jQuery(function($){
145
-
146
- // Enable pusher logging - don't include this in production
109
+ // Enables pusher logging - don't include this in production
147
110
Pusher.log = function(message) {
148
111
if (window.console && window.console.log) window.console.log(message);
149
112
};
150
113
151
114
// Flash fallback logging - don't include this in production
152
115
WEB_SOCKET_DEBUG = true;
153
116
154
- var newcount = 0,
155
- pusher = new Pusher('<?= $ pusher_key?> '),
117
+ // Initializes Pusher (done inline to use PHP for config variables)
118
+ var pusher = new Pusher('<?= $ pusher_key?> '),
156
119
channel = pusher.subscribe('photos'),
157
- photos = $('photos'),
158
- min_ID = photos.data('next-min-ID'),
159
- load_photos = function() {
160
- // Hides the notification
161
- $("#count-bar").addClass("hidden");
162
-
163
- $.getJSON(
164
- 'https://api.instagram.com/v1/tags/<?= $ tag?> /media/recent?callback=?',
165
- {
166
- 'access_token': '<?= $ token?> ',
167
- 'count': 16,
168
- 'min_id': min_ID
169
- }
170
- )
171
- .done(function(response){
172
- var new_photos = response.data,
173
- pagination = response.pagination,
174
- delay = 0,
175
- anim_speed = 200;
176
-
177
- // Removes the loading LI if present
178
- $("#photos").find('.loading').hide(400).delay(400).remove();
179
-
180
- // Resets the new photo count
181
- newcount = 0;
182
-
183
- // Sets the new min ID for loading images
184
- min_ID = pagination.next_min_id;
185
-
186
- for (x in new_photos) {
187
- var photoCont = $("#photos"),
188
- photo = new_photos[x],
189
- caption = (photo.caption!==null) ? photo.caption.text : '';
190
-
191
- //TODO: Make the captions show up and fix delay
192
-
193
- $('<img />', {
194
- src: photo.images.thumbnail.url,
195
- alt: caption,
196
- load: function(){
197
- var link = $('<a />', {
198
- href: photo.link,
199
- html: this
200
- })
201
- .css({opacity: 0})
202
- .delay(delay)
203
- .prependTo($("#photos"))
204
- .append($('<strong />'), {
205
- text: 'Photo by ' + photo.user.username
206
- })
207
- .wrap('<li />')
208
- .animate({ opacity: 1 }, anim_speed);
209
- }
210
- });
211
-
212
- delay += anim_speed;
213
- }
214
- })
215
- .error(function(data){
216
- console.log(data);
217
- });
218
- };
219
-
220
- channel.bind('new-photo', function(data){
221
-
222
- newcount += data.newcount;
223
-
224
- var plural = (newcount===1) ? 'photo' : 'photos';
225
- phrase = newcount+' new '+plural+' uploaded.';
226
-
227
- $('#count-bar').removeClass('hidden').find('#count').text(phrase);
228
-
229
- });
230
-
231
- $("#image-loader").bind('click', function(event){
232
- event.preventDefault();
233
-
234
- load_photos();
235
- });
236
-
237
- load_photos();
238
-
239
- });
120
+ tag = '<?= $ tag?> '; // For PHP-powered tag selection
240
121
241
122
</script>
123
+ <script src="js/main.js"></script>
242
124
243
125
</body>
244
126
0 commit comments