二、新浪微博站内应用demo添加数据到数据库【新浪微博应用开发】

这里的例子已在SAE上为基础进行讲解

 

1.在demo分析中已经进行应用授权进行了分析,这里对于应用授权之后插入自定义数据到数据库

 

2.在SAE创建应用

 

3.开启应用的mysql即可

 

4.插入数据到数据库,我这里的例子就是在用户授权之后获取用户的相关数据插入到书库内

 

5.实力代码:在index.php进行添加

 

<?php
session_start();//开启动session

include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );

//从POST过来的signed_request中提取oauth2信息
if(!empty($_REQUEST["signed_request"])){
    $o = new SaeTOAuthV2( WB_AKEY , WB_SKEY  );
    $data=$o->parseSignedRequest($_REQUEST["signed_request"]);
    if($data=='-2'){
         die('签名错误!');
    }else{
        $_SESSION['oauth2']=$data;
    }
}
//判断用户是否授权
if (empty($_SESSION['oauth2']["user_id"])) {
        include "auth.php";
        exit;
} else {
    //初始化API接口类
        $c = new SaeTClientV2( WB_AKEY , WB_SKEY ,$_SESSION['oauth2']['oauth_token'] ,'' );
    //初始化Mysql
        $mysql = new SaeMysql();
    
    //获取用户uid
        $uids = $c->get_uid();
        $uid = $uids['uid'];
    
    //调用apishow_user_by_id($uid)获取用户详细信息
        $uinfo  = $c->show_user_by_id($uid);
        
    //查询用是否存在调用getLine()查询一条数据
        $sql    = "SELECT uid FROM `fanspower_user` WHERE uid=$uid";
        $is_uid = $mysql->getLine($sql);
        
    //判断用户是否存在    
        
    if(empty($is_uid)){//不存在 插入一条数据
        
            $id           = $uinfo['id'];//uid
            $nickname     = $uinfo['screen_name'];//昵称
            $address      = $uinfo['location'];//地址
            $imgurl_small = $uinfo['profile_image_url'];//小图
            $imgurl_big   = $uinfo['avatar_large'];//大图
            $followers    = $uinfo['followers_count'];//粉丝数
            $created      = $uinfo['created_at'];  //微博创建时间
            $gender       = $uinfo['gender'];//性别
            $issued_atime = time();//授权时间
            
            $in_sql = "INSERT INTO `fanspower_user` (id, uid, nickname, imgurl_big, address, followers, imgurl_small, created, gender, issued_atime) VALUES (null, $id, '$nickname', '$imgurl_big', '$address', $followers, '$imgurl_small', '$created', '$gender', '$issued_atime');";
        //运行sql
            $mysql->runSql($in_sql);
        //获取插入数据的id
            $lastid =$mysql->lastId ();
        //判断
            if($lastid ==0){
                echo "参数错误,请反馈信息";
            }
    }else{//用户存在
            echo "已是授权用户";
    }        


} 

?>

 

6.对于调用的方法不知道是什么用途的请在 

api接口定义:http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI

api定义函数文件:目录文件中的saetv2.ex.class.php即可查阅

mysql函数地址:http://apidoc.sinaapp.com/sae/SaeMysql.html

 

 

 

posted @ 2014-01-14 11:22  Confi-Jin  阅读(275)  评论(0编辑  收藏  举报