Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 169 lines (140 sloc) 6.0 kB
2a3fd51 sample code for fetching yahoo profile data asynchronously using stor…
erik authored
1 <?php
b3da6db added link to license in all code files
erik authored
2 /*
3 * Copyright: (c) 2009, Yahoo! Inc. All rights reserved.
4 * License: code licensed under the BSD License. See [license.markdown](http://github.com/ydn/async_profile_fetch/blob/master/license.markdown)
5 */
2a3fd51 sample code for fetching yahoo profile data asynchronously using stor…
erik authored
6
2d829e5 abstracted out test guids for comments
erik authored
7 //define guids for testing here
8 $yahooGuids = array('BG5BMUK24OOYGHWKTJBCX2TN5E', 'VEGCGDXJ7FSR2PHSWPIR7EXMAQ');
9
2a3fd51 sample code for fetching yahoo profile data asynchronously using stor…
erik authored
10 //comments from some db query
11 $comments = array(
12 array(
13 'provider' => 'yahoo',
14
15 //yahoo user 1
2d829e5 abstracted out test guids for comments
erik authored
16 'id' => $yahooGuids[0],
2a3fd51 sample code for fetching yahoo profile data asynchronously using stor…
erik authored
17
18 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
19 'date' => '1264631647'
20 ),
21 array(
22 'provider' => 'twitter',
23 'id' => '2',
24 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
25 'date' => '1264631647'
26 ),
27 array(
28 'provider' => 'facebook',
29 'id' => '3',
30 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
31 'date' => '1264631647'
32 ),
33 array(
34 'provider' => 'facebook',
35 'id' => '4',
36 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
37 'date' => '1264631647'
38 ),
39 array(
40 'provider' => 'yahoo',
41
42 //yahoo user 2
2d829e5 abstracted out test guids for comments
erik authored
43 'id' => $yahooGuids[1],
2a3fd51 sample code for fetching yahoo profile data asynchronously using stor…
erik authored
44
45 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
46 'date' => '1264631647'
47 ),
48 array(
49 'provider' => 'facebook',
50 'id' => '6',
51 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
52 'date' => '1264631647'
53 ),
54 array(
55 'provider' => 'google',
56 'id' => '7',
57 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
58 'date' => '1264631647'
59 ),
60 array(
61 'provider' => 'twitter',
62 'id' => '8',
63 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
64 'date' => '1264631647'
65 ),
66 array(
67 'provider' => 'yahoo',
68
69 //yahoo user 1
2d829e5 abstracted out test guids for comments
erik authored
70 'id' => $yahooGuids[0],
2a3fd51 sample code for fetching yahoo profile data asynchronously using stor…
erik authored
71
72 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
73 'date' => '1264631647'
74 ),
75 );
76
77 ?>
78
79 <ul id="comments">
80
81 <? // use index to disambiguate two comments from same user ?>
82 <? foreach($comments as $index => $comment): ?>
83
84 <li id="comment-<?= $index ?>-<?= $comment['provider'] ?>-<?= $comment['id'] ?>">
85
86 <!-- reveal profile markup id data avail -->
87 <div class="profile" style="display:none">
88 <div class="name"></div>
89 <img/>
90 </div>
91
92 <?= $comment['text'] ?><br/>
93 <?= $comment['date'] ?>
94 </li>
95 <? endforeach ?>
96 </ul>
97
98 <script type="text/javascript" src="http://yui.yahooapis.com/3.0.0b1/build/yui/yui-min.js"></script>
99 <script>
100 var Y = YUI();
101 Y.use('node', 'io-base', 'json-parse', 'css3-selector', function (Y) {
102
103 //loop through all the comments (ref: http://developer.yahoo.com/yui/3/api/NodeList.html#method_each)
104 Y.all('#comments li').each(function (node, index, list) {
105
106 //comment id
107 var id = node.get('id');
108
109 //to keep demo simple, profile.php only handles yahoo profiles
110 if (-1 === id.indexOf('yahoo')) {
111 return;
112 }
113
114 //proxy url + yahoo guid
115 var uri = 'profile.php?guid=' + id.split('-')[3],
116
117 //xhr req callback
118 handleComplete = function (reqId, o, args) {
119
120 //extract req data
121 var json = Y.JSON.parse(o.responseText),
122 liNodeId = args.complete.split('=')[1];
123
124 //bail if error
125 if (json.error) {
126 Y.log(json);
127 return;
128 }
129
130 //get profile data
131 var familyName = json.success.query.results.profile.familyName,
132 givenName = json.success.query.results.profile.givenName,
133 picUrl = json.success.query.results.profile.image.imageUrl,
134
135 //get dom nodes
136 profileNode = Y.Node.get('#' + liNodeId + ' .profile'),
137 nameNode = Y.Node.get('#' + liNodeId + ' .name'),
138 imgNode = Y.Node.get('#' + liNodeId + ' img');
139
140 //show the name and/or pic if avail
141 if (familyName) {
142 nameNode.append(givenName + ' ' + familyName);
143 profileNode.setStyle('display', 'block');
144 }
145
146 if (picUrl) {
147 imgNode.set('src', picUrl);
148 profileNode.setStyle('display', 'block');
149 }
150 },
151 config = {
152
153 //define fn to call when req completes
154 'on' : {
155 'complete' : handleComplete
156 },
157
158 //pass the comment node's id to the callback so it can add data to it
159 'arguments' : {
160 'complete' : 'id=' + id
161 },
162
163 //bind callback to each request not to window, which would only call fn for last req
164 'context' : this
165 };
166 Y.io(uri, config);
167 });
168 });
169 </script>
Something went wrong with that request. Please try again.