发布于 2019-04-27  258 次阅读


微博登陆

[TOC]


1.第一次请求:首先访问使用第一个授权API

在idnex/Api/index的视图里面用button的点击事件跳转到:

https://api.weibo.com/oauth2/authorize?client_id=154815301&response_type=code&redirect_uri=http://www.tp5.com/tp5_03/public/index/api/weibo

参数client_id redirect_uri

返回给你设定的授权回调页一个CODE 用于表示授权成功的码(动态生成)

之后会跳转你自己设定回调url .

1
“?code=192ccba787e51a72e9b90c50805f6a2e”

2.自己的回调url传参

​ 传入code

3.第二次请求(使用POST):

https://api.weibo.com/oauth2/access_token?client_id=154815301&client_secret=92c786f162cd3935ce7d64bbaaf2c8f4&grant_type=authorization_code&redirect_uri=http://www.tp5.com/tp5_03/public/index/api/weibo&code=7daeeea6fd7481c6aad8431710fccaae

参数:client_id client_secret redirect_uri 以及上一个API返回的CODE

返回:


1
2
3
4
5
6
7
8
Array
(
    [access_token] => 2.00rF8DvG0zWaTK71839f71c7AJWKYC
    [remind_in] => 157679999
    [expires_in] => 157679999
    [uid] => 6339867279
    [isRealName] => true
)

access_token 对应的是授权的用户

index/Api/weibo


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
public function weibo(){
    //获取code
    $code = input('code');
    echo $code;
    //        exit();
    //POST获取开始
    $tom = "https://api.weibo.com/oauth2/access_token?client_id=154815301&client_secret=92c786f162cd3935ce7d64bbaaf2c8f4&grant_type=authorization_code&redirect_uri=http://www.tp5.com/tp5_03/public/index/api/weibo&code=".$code;
    $url = $tom;
    header("Content-type:text/html;charset=utf-8");
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($curl,CURLOPT_POST,TRUE);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl,CURLOPT_USERPWD,"username:passward");
    $data = curl_exec($curl);
    curl_close($curl);
    $result = json_decode($data,true);


    echo "<pre>";print_r($result);echo "</pre>";

    $access_token =  $result['access_token'];
    $uid =  $result['uid'];

    echo $access_token;
    echo "登陆成功";
}

上面我们就能获取换取Access Token

4.第三次请求用户数据

https://api.weibo.com/2/statuses/public_timeline.json?access_token=abcd


1
2
3
4
5
6
7
8
9
10
11
12
//获取授权用户数据
$access_token =  $result['access_token'];
$uid =  $result['uid'];

//可以get
$tom = "https://api.weibo.com/2/users/show.json?access_token=" . $access_token . "&uid=" . $uid;
$data = file_get_contents($tom,'rb');

$data = json_decode($data,true);

echo "/********************************/";
echo "<pre>";print_r($data);echo "</pre>";

获取的$data用户数据我放到最后

End.


ps:

我是用的是没通过审核的应用

注意调试时不要在第二个请求直接刷新,会得到类似的返回值:

1
2
3
4
5
6
7
8
9
php
   Array
   (
    [error] => invalid_grant
    [error_code] => 21325
    [request] => /oauth2/access_token
    [error_uri] => /oauth2/access_token
    [error_description] => invalid authorization code:3ed09c0168eebf93713d2511b4119c1d
   )

官方文档:https://open.weibo.com/wiki/Connect/login?tdsourcetag=s_pctim_aiomsg

授权的用户数据


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
Array
    (
    [id] => 6339867279
    [idstr] => 6339867279
    [class] => 1
    [screen_name] => Maiiiiiid
    [name] => Maiiiiiid
    [province] => 100
    [city] => 1000
    [location] => 其他
    [description] =>
    [url] =>
    [profile_image_url] => http://tvax2.sinaimg.cn/crop.0.0.996.996.50/006V3r5Rly8fshts00jsyj30ro0rotal.jpg
    [cover_image_phone] => http://ww1.sinaimg.cn/crop.0.0.640.640.640/549d0121tw1egm1kjly3jj20hs0hsq4f.jpg
    [profile_url] => u/6339867279
    [domain] =>
    [weihao] =>
    [gender] => m
    [followers_count] => 20
    [friends_count] => 153
    [pagefriends_count] => 0
    [statuses_count] => 2
    [video_status_count] => 0
    [favourites_count] => 62
    [created_at] => Mon Aug 07 12:18:15 +0800 2017
    [following] =>
    [allow_all_act_msg] =>
    [geo_enabled] => 1
    [verified] =>
    [verified_type] => -1
    [remark] =>
    [insecurity] => Array
    (
        [sexual_content] =>
    )

    [status] => Array
    (
        [created_at] => Sat Apr 27 09:24:35 +0800 2019
        [id] => 4.3656195641262E+15
        [idstr] => 4365619564126181
        [mid] => 4365619564126181
        [can_edit] =>
        [show_additional_indication] => 0
        [text] => 转发微博
        [source_allowclick] => 0
        [source_type] => 1
        [source] => 微博 weibo.com
        [favorited] =>
        [truncated] =>
        [in_reply_to_status_id] =>
        [in_reply_to_user_id] =>
        [in_reply_to_screen_name] =>
        [pic_urls] => Array
        (
        )

        [geo] =>
        [is_paid] =>
        [mblog_vip_type] => 0
        [reposts_count] => 0
        [comments_count] => 0
        [attitudes_count] => 0
        [pending_approval_count] => 0
        [isLongText] =>
        [reward_exhibition_type] => 0
        [hide_flag] => 0
        [mlevel] => 0
        [visible] => Array
        (
            [type] => 0
            [list_id] => 0
        )

        [biz_feature] => 0
        [hasActionTypeCard] => 0
        [darwin_tags] => Array
        (
        )

        [hot_weibo_tags] => Array
        (
        )

        [text_tag_tips] => Array
        (
        )

        [mblogtype] => 0
        [rid] => 0
        [userType] => 0
        [more_info_type] => 0
        [positive_recom_flag] => 0
        [content_auth] => 0
        [gif_ids] =>
        [is_show_bulletin] => 2
        [comment_manage_info] => Array
        (
            [comment_permission_type] => -1
            [approval_comment_type] => 0
        )

    )

    [ptype] => 0
    [allow_all_comment] => 1
    [avatar_large] => http://tvax2.sinaimg.cn/crop.0.0.996.996.180/006V3r5Rly8fshts00jsyj30ro0rotal.jpg
    [avatar_hd] => http://tvax2.sinaimg.cn/crop.0.0.996.996.1024/006V3r5Rly8fshts00jsyj30ro0rotal.jpg
    [verified_reason] =>
    [verified_trade] =>
    [verified_reason_url] =>
    [verified_source] =>
    [verified_source_url] =>
    [follow_me] =>
    [like] =>
    [like_me] =>
    [online_status] => 0
    [bi_followers_count] => 13
    [lang] => zh-cn
    [star] => 0
    [mbtype] => 0
    [mbrank] => 0
    [block_word] => 0
    [block_app] => 0
    [credit_score] => 80
    [user_ability] => 0
    [urank] => 9
    [story_read_state] => -1
    [vclub_member] => 0
    [is_teenager] => 0
    [is_guardian] => 0
    [is_teenager_list] => 0
)

先这样,以后再改吧