-
Notifications
You must be signed in to change notification settings - Fork 0
/
back.php
251 lines (233 loc) · 8.21 KB
/
back.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
require_once './config.php';
if (!isset($_GET['code']) || !isset($_GET['state']) ) {
die('invalid request');
}
//请求authorization code
$params = array(
'grant_type' => 'authorization_code',
'client_id' => APP_KEY,
'client_secret' => APP_ID,
'code' => $_GET['code'],
'state' => $_GET['state'],
'redirect_uri' => REDIRECT_URL
);
$requestUrl = GET_TOKEN_URL.'?'.http_build_query($params);
$result = file_get_contents($requestUrl);
if (strpos($result,'error')) {
die('require expired');
}
list($access_key,$expire_time) = explode('&',$result);
list(,$access_token)= explode('=',$access_key);
//求证open id
$getOpenIDUrl = GET_OPENID_URL.'?'.$access_key;
$getOpenIdResult = file_get_contents($getOpenIDUrl);
$result = explode(')',strtr($getOpenIdResult,'(',')'));
$resultObject = json_decode($result[1]);
$openId = $resultObject->openid;
//构造请求个人信息参数
$getinfohost = GET_INFO_URL.'?'.$access_key;
$params = array(
'oauth_consumer_key' => APP_KEY,
'openid' => APP_ID,
'format' => 'json'
);
$getinfoRequestURL = $getinfohost.'&'.http_build_query($params);
//构造求证听众列表参数
$host = GET_FANSLIST_URL.'?'.$access_key;
$params = array(
'oauth_consumer_key' => APP_KEY,
'openid' => APP_ID,
'format' => 'json',
'reqnum' => '20',
'mode' => '0'
);
$requestURL = $host.'&'.http_build_query($params);
//构造发送微博参数
$sentTweetURL = SEND_T_URL.'?'.$access_key;
// $sendTweetParams = array(
// 'oauth_consumer_key' => APP_KEY,
// 'openid' => APP_ID,
// 'format' => 'json',
// );
// $requestURL = $host.'&'.http_build_query($params);
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../css/min.css" />
<style type="text/css">
#loading{
display:absolute;
left:100px;
top:100px;
width:54px;
height:55px;
background:url(../images/ajax-loader.gif) no-repeat;
}
</style>
<title>我的听众</title>
<script type="text/javascript">
var page = 1;//请求页面数
var request = '<?php echo $requestURL;?>';//原始请求地址
var findfans=[];//符合条件的听众
var fans;//每次请求返回的 听众
var fanstimer;//请求定时器
var userinfo;//当前登陆用户的个人信息
var stop=0;//请求是否停止
function toQueryString(obj) {
var parts = [];
for(var each in obj) if (obj.hasOwnProperty(each)) {
parts.push(encodeURIComponent(each) + '=' + encodeURIComponent(obj[each]));
}
return parts.join('&');
}
function YQLQuery(query, callback) {
this.query = query;
this.callback = callback || function(){};
this.fetch = function() {
if (!this.query || !this.callback) {
throw new Error('YQLQuery.fetch(): Parameters may be undefined');
}
var scriptEl = document.createElement('script'),
uid = 'yql' + +new Date(),
// encodedQuery = encodeURIComponent(this.query),
encodedQuery = this.query,
instance = this;
YQLQuery[uid] = function(json) {
instance.callback(json);
delete YQLQuery[uid];
document.body.removeChild(scriptEl);
};
scriptEl.src = 'http://query.yahooapis.com/v1/public/yql?q=' + encodedQuery + '&format=json&callback=YQLQuery.' + uid;
// scriptEl.src = 'http://query.yahooapis.com/v1/public/yql?q=' + encodedQuery + '&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'
console.log(scriptEl.src);
document.body.appendChild(scriptEl);
};
}
function getfans() {
console.log(page);
startindex=20*(page -1);
page ++;
url = request+'&startindex='+startindex;
console.log(url);
$.ajax({
url:url,
type:'GET',
success:function(res){
data = res.responseText;
fans = JSON.parse($(data).text());
$.each(fans.data.info,function(i,item){
//听众过滤条件,此处设定为与用户所在地相同
if(item.city_code == userinfo.data.city_code){
$('<li>').html('姓名:'+item.nick).appendTo('#fanslist');
findfans.push(item.nick);
}
});
//如果后面无记录停止向服务器发送请求
if (fans.data.hasnext) {
stop = 1;
$('#loading').hide();
clearTimeout(fanstimer);
}
//else { }
}
});
if (!stop) {
fanstimer = setTimeout(getfans,10000);
}
}
// This utility function creates the query string
// to be appended to the base URI of the YQL Web
// service.
function toQueryString(obj) {
var parts = [];
for(var each in obj) if (obj.hasOwnProperty(each)) {
parts.push(encodeURIComponent(each) + '=' + encodeURIComponent(obj[each]));
}
return parts.join('&');
}
// Store the anonymous function that wraps
// the OpenSocial function makeRequest
var runQuery = function(ws_base_uri,query, handler) {
gadgets.io.makeRequest(ws_base_uri, handler, {
METHOD: 'POST',
POST_DATA: toQueryString({q: query, format: 'json'}),
CONTENT_TYPE: 'JSON',
AUTHORIZATION: 'OAuth'
});
};
// Callback function for handling response data
function handler(rsp) {
if(rsp.data){
yql_results = rsp.data;
}
}
</script>
</head>
<body>
<a href="#" id ='getfans'>获取我的同城听众</a>
<a href="#" id ='send'>将结果发到我的微博</a>
<br />
<br />
<ul id='myinfo'></ul>
<ul id='fanslist'></ul>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<!-- <script type="text/javascript" src='jqueryjsonp.js'></script> -->
<script type="text/javascript" src='jp.js'></script>
<script type="text/javascript">
$(function(){
//获取个人信息
$.ajax({
url:'<?php echo $getinfoRequestURL;?>',
type:'GET',
success:function(res){
$('#loading').hide();
data = (res.responseText);
userinfo = JSON.parse($(data).text());
$('<li>').html('姓名:'+userinfo.data.nick).appendTo('#myinfo');
}
});
$('#send').click(function(){
var yql_base_uri = "http://query.yahooapis.com/v1/yql";
var requesturl = '<?php echo SEND_T_URL;?>';
var params={};
// params.content = Date();
params.content = findfans.join('、').substr(0,140);
params.oauth_consumer_key = '<?php echo APP_KEY;?>';
params.access_token = '<?php echo $access_token;?>';
params.client_id = '<?php echo APP_KEY;?>';
postdata = toQueryString(params);
console.log(postdata);
var sql = "select * from htmlpost where url='"+'<?php echo SEND_T_URL;?>'+"'and postdata='"+postdata+"' and xpath ='*'";
// var queryString =
//querystrin = ToqueryStrin(params);
/*
$.ajax({
type:'POST',
url:'<?php echo SEND_T_URL;?>',
data:{
// 'oauth_consumer_key':'<?php echo APP_KEY;?>',
// 'access_token':'<?php echo $access_token;?>',
// 'client_id':'<?php echo APP_KEY;?>'
},
success:function(response){console.log(response)}
});
*/
});
//获取听众列表
$('#getfans').click(function(){
if (!userinfo) {
alert('正在加载您的个人信息,请稍后再试!');
return false;
}
$('#loading').show();
getfans();
return false;
});
});
</script>
<div id="loading"></div>
</body>
</html>