获取reddit用户对relevent subreddit的评论和评论

时间:2018-11-06 19:23:50

标签: oauth-2.0 reddit

我使用Reddit登录并获得了用户名,访问令牌和刷新令牌。我想对我站点中用户的子意见获得赞誉和评论。我使用下面的代码。但这不起作用。当我输入===>

$user_info_url = "https://oauth.reddit.com/user/krishb44/upvoted";

            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$user_info_url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: bearer ".$access_token, "User-Agent: BountyMiner/1.0 by krishb44"));
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);


            $result = curl_exec($ch);
            curl_close($ch);

            //$data = file_get_contents($json);
            $char = json_decode($result);
           print_r ( $char->{'data'}->{'dist'});

====>此代码使用reddit页面登录,并提供输出。

 $client_id = '';
    $client_secret = '';
    $fields = array("grant_type" => "refresh_token", "refresh_token" => $refresh_token);



foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
        rtrim($fields_string, '&');

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, "https://ssl.reddit.com/api/v1/access_token");
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$client_secret);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

    $result = curl_exec($ch);
    $result = json_decode($result);

    //new access token
    $access_token = $result->access_token;

  $user_info_url = "https://oauth.reddit.com/user/krishb44/upvoted";

            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$user_info_url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: bearer ".$access_token, "User-Agent: BountyMiner/1.0 by krishb44"));
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);


            $result = curl_exec($ch);
            curl_close($ch);

            //$data = file_get_contents($json);
            $char = json_decode($result);
           print_r ( $char->{'data'}->{'dist'});

0 个答案:

没有答案