include_once $dbConfFile; include_once $webRoot . 'foundation/debug.php'; $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['stoken']); $dbo = new dbex($dbServs); /* 获取关注任务的图标url */ $sql = "select task_id, task_sina_uid from task where type='follow' and (task_icon_url IS NULL OR task_screen_name IS NULL OR task_location IS NULL) limit 2"; $sql_res = $dbo->getRs($sql); foreach ($sql_res as $user) { $user_info = $c->show_user_by_id($user['task_sina_uid']); if_weiboapi_fail($user_info); $icon_url = $user_info['avatar_large']; $screen_name = $user_info['screen_name']; $location = $user_info['location']; // echo '<p><img src="'.$icon_url.'" />'.$location.$screen_name.'</p>'; $sql = "update task set task_icon_url='{$icon_url}', task_screen_name='{$screen_name}', task_location='{$location}' where task_id = {$user['task_id']} limit 1"; echo '<p>' . $sql . '</p>'; $dbo->query($sql); } /* 获取转发任务的小图标,微博内容,微博图片大url和小url */ $sql = "select task_id, task_sina_wid from task where type='repost' and (task_icon_url IS NULL OR task_thumbnail_pic_url IS NULL OR task_bmiddle_pic_url IS NULL OR task_text IS NULL) limit 50"; $sql_res = $dbo->getRs($sql); foreach ($sql_res as $user) { $weibo = $c->show_status($user['task_sina_wid']); if_weiboapi_fail($weibo); $task_icon_url = $weibo['user']['profile_image_url']; $task_thumbnail_pic_url = $weibo['thumbnail_pic_url']; $task_bmiddle_pic_url = $weibo['bmiddle_pic_url']; $task_text = $weibo['text']; echo '<img src="' . $task_icon_url . '">' . "<p>{$task_text}</p>" . '<a href="' . $task_bmiddle_pic_url . '"><img src="' . $task_thumbnail_pic_url . '"></a>'; } $dbo->close();
// 更新token失败, // 此处并不影响微博登录的用户继续操作,因为该类用户使用的token不从数据库中获得 // 受影响的是使用微动力帐号登录的用户,和我们的检测程序(如果我们的帐号出错的话) debug('更新数据库中用户token时,updte操作出错,SQL[' . $sql . ']。', __FILE__, __LINE__, FALSE, 'fatal'); } } } // 写session $sql = "select user_id, role, realtime_money as rm, level, sina_level from user join user_info_sina using(user_id) where sina_uid = {$sid} limit 1"; $res = $dbo->getRow($sql); $_SESSION['uid'] = $res['user_id']; $_SESSION['role'] = user_role_switch($res['role'], FALSE); $_SESSION['sid'] = $sid; $_SESSION['name'] = $name; $_SESSION['user_realtime_money'] = price_db_to_user($res['rm']); $_SESSION['stoken'] = $token['access_token']; $_SESSION['level'] = $res['level']; $_SESSION['slevel'] = $res['sina_level']; $_SESSION['is_bind_weibo'] = TRUE; header("Location:my.php"); // 后台获取用户的已关注用户列表,写入SESSION $friends = $c->friends_by_id($_SESSION['sid']); if_weiboapi_fail($friends, __FILE__, __LINE__); foreach ($friends['users'] as $friend) { $followed_id[] = $friend['id']; } $_SESSION['followed_id'] = $followed_id; } else { $msg = '授权失败。请稍候重试' . '<a href="index.php">点此</a>返回主页'; debug($msg, __FILE__, __LINE__); }
$pid_len = strlen($pid); if (10 > $pid_len || 12 < $pid_len) { $msg = 'improper user_id: perhaps you did choose correct person.'; $to_url = $siteRoot . 'create_task.php'; $to_name = '任务创建页面'; delay_jump(3, $msg, $ot_url, $to_name); } $person = $c->show_user_by_id($pid); } if (isset($pserson['error_code']) && '20003' == $person['error_code']) { $msg = '您指定的用户不存在!'; $to_url = $siteRoot . 'task.php'; $to_name = '任务列表'; delay_jump(3, $msg, $to_url, $to_name); } if_weiboapi_fail($person); // 欲关注的用户存在,扣钱先 $sql = "update user set realtime_money = realtime_money - {$db_total_price} where user_id = '{$_SESSION['uid']}' limit 1"; $num = $dbo->exeUpdate($sql); if (1 != $num) { $msg = "扣钱失败,跳转。SQL:" . $sql; debug($msg, __FILE__, __LINE__); } // 扣钱成功,写task表 $sql = "insert into task (owner_id, publisher_id, type, base_price, amount, status, create_at, expire_in) values('{$_SESSION['uid']}', '{$_SESSION['uid']}', 2, '{$db_base_price}', '{$amount}', 1, now(), '{$expire_in_sec}')"; $num = $dbo->exeUpdate($sql); if (1 != $num) { // 写数据表失败,回滚金钱数据 $sql2 = "update user set realtime_money = realtime_money + {$db_total_price} where user_id = '{$_SESSION['uid']}' limit 1"; $num2 = $dbo->exeUpdate($sql2); if (1 != $num2) {
$trim_user = 1; // 0:返回完整user信息,1:user字段仅返回uid $base_app = 0; // 0:无限制,1:仅返回通过本应用发布的微博 $statuses = $c->user_timeline_by_id($uid, $page, $count, $since_id, $max_id, $feature, $trim_user, $base_app); if_weiboapi_fail($statuses); break; case 2: // sina_follow $uid = $_SESSION['sid']; $cursor = 0; $count = 50; $followers = $c->followers_by_id($uid, $cursor, $count); if_weiboapi_fail($followers); $friends = $c->friends_by_id($uid, $cursor, $count); if_weiboapi_fail($friends); break; default: $msg = '暂不支持该类型'; } require_once "uiparts/docheader.php"; ?> <body> <?php include "uiparts/header.php"; ?> <div id="func_column"> <ul > <li><a href="create_task.php?type=sina_follow">创建新浪关注任务</a></li> <li><a href="create_task.php?type=sina_repost">创建新浪转发任务</a></li> <li><a alt="create_task.php?type=sina_review">创建新浪评论任务(暂不可用)</a></li>
<?php session_start(); /* unfollow.php * 测试页面 */ include_once "../config.php"; include_once $webRoot . "lib/saetv2.ex.class.php"; include_once $webRoot . "foundation/debug.php"; $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['stoken']); if (isset($_GET['id'])) { $res = $c->unfollow_by_id($_GET['id']); if_weiboapi_fail($res); header("Location:" . $siteRoot . "task.php"); exit; }
if (0 === count($followers_ids)) { $unpass_tasks = $do_tasks; } else { $unpass_tasks = array_diff($do_tasks, $followers_ids['ids']); } if (0 != count($unpass_tasks)) { foreach ($unpass_tasks as $k => $v) { // $k -- user_id, $v -- uid $sql = "select nick_name from user where user_id = {$k} limit 1"; $sql_res = $dbo->getRow($sql); echo 'user_id:' . $k . '; weibo_mid: ' . $v . '; 用户名:' . $sql_res['nick_name'] . "\n"; } // 对未通过draft1验证的用户进行精确验证 foreach ($unpass_tasks as $k => $v) { $result = $c->is_followed_by_id($v, $task_uid); if_weiboapi_fail($result); if (!$result['target']['following']) { $failed[$k] = $v; } } echo "failed by draft2\n"; echo "----------------------\n"; if (0 === count($failed)) { echo "NONE\n"; } else { foreach ($failed as $k => $v) { // $k -- user_id, $v -- mid $sql = "select nick_name from user where user_id = {$k} limit 1"; $sql_res = $dbo->getRow($sql); echo 'user_id:' . $k . '; sina_uid: ' . $v . '; 用户名:' . $sql_res['nick_name'] . "\n"; }
include_once $webRoot . "foundation/status.php"; if (!is_login()) { header('Location:' . $siteRoot . 'index.php?login_error=not_login'); } require_once $webRoot . 'foundation/debug.php'; require_once $webRoot . 'foundation/page.php'; require_once $webRoot . 'foundation/price.php'; require_once $webRoot . 'lib/saetv2.ex.class.php'; global $sina_weibo_id_length; // init in config.php if (isset($_POST['weibo_id']) && is_numeric($_POST['weibo_id']) && $sina_weibo_id_length === strlen($_POST['weibo_id'])) { $real_wid = $_POST['weibo_id']; $stoken = $_SESSION['stoken']; $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $stoken); $api_res = $c->user_timeline_by_id(NULL, 1, 50, 0, 0, 1, 1, 0); if_weiboapi_fail($api_res); $wids = $api_res['statuses']; $text = ""; // 微博文本 foreach ($wids as $wid) { if ($real_wid == $wid['id']) { $text = $wid['text']; } } if (empty($text)) { // 验证微博id未通过,说明post过来的id无效。 echo '对不起,该条微博不是您的原创微博,请查证,若您。。'; exit; } ?> <form action="<?php
// and time ... // attention $sql_res = $dbo->getRs($sql); if (!$sql_res || 0 === count($sql_res)) { echo '<p>没有人做过此任务。</p>'; } else { foreach ($sql_res as $k) { $do_tasks[$k['user_id']] = $k['sina_uid']; echo "<p>{$k['nick_name']} sina_uid: {$k['sina_uid']}</p>"; } } // 此时 $do_task 中存放的是 user_id 和 sina_uid 的键值对。 // 使用api查询任务微博的转发微博 statuses/repost_timeline/ids $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['stoken']); echo '<h3>该任务用户目前的粉丝的uid:</h3>'; $followers_ids = $c->followers_ids_by_id($task_uid, 0, 5); if_weiboapi_fail($followers_ids, __FILE__, __LINE__); if (!$followers_ids || 0 === count($followers_ids)) { echo '<p>该用户没有粉丝,或者api没有获取到ta的粉丝。</p>'; } else { foreach ($followers_ids['ids'] as $id) { echo $id . '</p>'; } } echo '<hr />'; echo '<h3>未通过draft1检测的用户</h3>'; if (0 === count($do_tasks)) { echo '<p>该任务目前没有人做</p>'; exit; } if (0 === count($followers_ids)) { $unpass_tasks = $do_tasks;
function get_user_friends_by_name($name, $c, $dbo) { echo '<h2>根据sina_uid获取关注列表 uid:' . $sid . '</h2>'; $friends = $c->friends_by_name($name, 0, 200); if_weiboapi_fail($friends, __FILE__, __LINE__); foreach ($friends['users'] as $user) { $idstr = $dbo->real_escape_string($user['idstr']); $screen_name = $dbo->real_escape_string($user['screen_name']); $friends_count = $dbo->real_escape_string($user['friends_count']); $followers_count = $dbo->real_escape_string($user['followers_count']); $weibo_count = $dbo->real_escape_string($user['statuses_count']); $profile_image_url = $dbo->real_escape_string($user['profile_image_url']); $avatar_large_url = $dbo->real_escape_string($user['avatar_large']); $location = $dbo->real_escape_string($user['location']); $description = $dbo->real_escape_string($user['description']); $sql_basic = "INSERT INTO `task` (owner_id, publisher_id, type, base_price, amount, finish_amount, status )VALUES (1, 1, 2, 700, 100, 39, 1);"; $sql_advance = "insert into `task_info_sina_follow` (task_id, sina_uid, screen_name, friends_count, followers_count, weibo_count, profile_image_url, avatar_large_url, location, user_description) values (last_insert_id(), '{$idstr}', '{$screen_name}', '{$friends_count}', '{$followers_count}', '{$weibo_count}', '{$profile_image_url}', '{$avatar_large_url}', '{$location}', '{$description}');"; echo "<p>{$sql_basic}<br />{$sql_advance}</p>"; } }
// and time ... // attention $sql_res = $dbo->getRs($sql); foreach ($sql_res as $k) { $do_tasks[$k['user_id']] = $k['repost_mid']; } // 此时 $do_task 中存放的是 user_id 和 repost_mid 的键值对。 // 使用api查询任务微博的转发微博 statuses/repost_timeline/ids $sql_res = $dbo->getRow('select sina_token from user_info_sina where sina_token is not null and unix_timestamp(now()) < unix_timestamp(token_update_at)+token_expires_in limit 1'); //$c = new SaeTClientV2( WB_AKEY, WB_SKEY, $_SESSION['stoken'] ); $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $sql_res['sina_token']); /* echo "\n该任务微博目前的转发微博的mid:\n"; echo "--------------------------\n"; */ $repost_statuses = $c->repost_timeline($task_wid); if_weiboapi_fail($repost_statuses, __FILE__, __LINE__); foreach ($repost_statuses['reposts'] as $status) { //echo $status['mid'].' -- '.$status['text'].' ('.$status['retweeted_status']['text'].")\n"; $real_status_mids[] = $status['mid']; } echo "\n" . '在该任务上出问题的用户' . "\n"; echo "--------------------------\n"; if (0 === count($do_tasks)) { echo 'no body did the task' . "\n"; exit; } if (0 === count($real_status_mids)) { $fail_tasks = $do_tasks; } else { $fail_tasks = array_diff($do_tasks, $real_status_mids); }
$user['create_at'] = $user_info['created_at']; // 原创微博数,优质粉丝列表,优质粉丝数,有效粉丝数 // 最新微博平均被转发次数/平均被评论次数 和 最高转发次数/评论次数 $weibos = $c->user_timeline_by_id($_SESSION['sid']); if_weiboapi_fail($weibos); $weibo_amount = count($weibos['statuses']); unset($ids); foreach ($weibos['statuses'] as $weibo) { $ids .= $weibo['id'] . ','; // 注意,如果系统字长不够,此处有可能无法处理这个数字型的id echo $weibo['id'] . '<br />'; } $params = array(); $params['ids'] = $ids; $weibo_counts = $c->oauth->get('statuses/count', $params); if_weiboapi_fail($weibo_counts); if (0 != $weibo_amount) { // 发过微博 $max_reposts = 0; $max_comments = 0; foreach ($weibo_counts as $weibo_count) { $max_reposts = $weibo_count['reposts'] > $max_reposts ? $weibo_count['reposts'] : $max_reposts; $max_comments = $weibo_count['comments'] > $max_comments ? $weibo_count['comments'] : $max_comments; $total_reposts += $weibo_count['reposts']; $total_comments += $weibo_count['comments']; } $user['max_reposts'] = $max_reposts; $user['max_comments'] = $max_comments; $user['aver_reposts'] = 0 == $total_reposts ? 0 : $total_reposts / $weibo_amount; $user['aver_comments'] = 0 == $total_comments ? 0 : $total_comments / $weibo_amount; // echo 'total reposts: '.$total_reposts.'</p>';
// if_weiboapi_fail($task_res, __FILE__, __LINE__); // 此处不应使用if_weiboapi_fail(),因为它对调用失败的处理只是简单的输出提示,不满足此处处理的需要。 if (isset($task_res['error_code'])) { // 没做成功,回滚task表中finish_amount数据 $sql = "update task set finish_amount = finish_amount - 1 where task_id = {$task_id} limit 1"; $dbo->exeUpdate($sql); $dbo->close(); switch ($task['error_code']) { case '20506': // 用户之前已经关注过该用户/任务不成功,将该任务用户加入当前用户已关注列表 $msg = "该用户已经在您的关注列表中了,如果他继续出现在您的任务列表中,请手动屏蔽。"; $to_name = '任务列表'; $to_url = $siteRoot . 'task.php'; break; case '21327': if_weiboapi_fail($task_res); // token expired break; default: $msg = 'api调用某处出了错误:' . $task_res['error'] . "。您未能完成任务。"; $to_url = $siteRoot . 'task.php'; $to_name = '任务列表'; } delay_jump(3, $msg, $to_url, $to_name); } // 做成功了,写数据库,写SESSION 对应do_task status 11 正常完成 // 写do_task表 $db_level_money = price_base_to_level($base_price, $_SESSION['slevel']); $sql = "insert do_task (task_id, user_id, status, task_type, owner_name, income, repost_mid, time)values({$task_id}, {$_SESSION['uid']}, 11, 2, '{$task_owner_name}', '{$db_level_money}', NULL, now())"; $sql_num = $dbo->exeUpdate($sql); if (1 != $sql_num) {