美文网首页
php 抖音openApi

php 抖音openApi

作者: define光年 | 来源:发表于2019-12-11 11:38 被阅读0次
image.png

首先要获取权限 这里用的tp5

   $ClientKey=config('site.ClientKey');
    $douyinClientSecret=config('site.douyinClientSecret');
    $url="https://open.douyin.com/platform/oauth/connect/?client_key=".$ClientKey."&response_type=code&scope=user_info,fans.data&redirect_uri=http://screen.imfic.com.cn/admin.php/douyin/notify";
 echo '<a href="'.$url.'" title="点击登录" target="_blank">登录</a><br/>';

点击登录即会跳转到抖音授权登录界面
使用抖音登录会跳转到回调地址

 public function notify(){
         $ClientKey=config('site.ClientKey');
         $douyinClientSecret=config('site.douyinClientSecret');
        $code=input("get.code");
       $url="https://open.douyin.com/oauth/access_token/?client_key=".$ClientKey."&client_secret=".$douyinClientSecret."&code=".$code."&grant_type=authorization_code";
           file_put_contents('1.txt', $url);
      $info=$this->getJson($url);
   if($info['message']=="success"){
     $data['access_token']=$info['data']['access_token'];
     $data['open_id']=$info['data']['open_id'];
     $data['expires_in']=$info['data']['expires_in'];
     $data['refresh_token']=$info['data']['refresh_token'];
     $data['addtime']=time();
     $flag=Db::table('daping_douyintoken')->insert($data);
     if($flag){
        echo "<script  type=\"text/javascript\" charset=\"utf-8\" async defer>alert('登录成功');window.opener=null;window.open('','_self');window.close();</script>";
     } 
   }

下面是一个获取粉丝情况的接口的调用

  public function  getData(){
          header('Access-Control-Allow-Origin:*');
          $timestamp = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
          $isExistToday=Db::table('daping_douyin')->where('addtime>'.$timestamp)->find();
           if ($isExistToday) {
             return json_encode($isExistToday);
           }else{
             $info=Db::table('daping_douyintoken')->order('addtime desc')->find();
        $ClientKey=config('site.ClientKey');
        $douyinClientSecret=config('site.douyinClientSecret');
        $url="https://open.douyin.com/fans/data/?access_token=".$info['access_token']."&open_id=".$info['open_id'];
        $info=$this->getJson($url);
        $data=$info['data'];
        if($data['error_code']==0){
          $fans_data=$data['fans_data'];
          $gender_distributions_data=$fans_data['gender_distributions'];//男女
          $age_distributions_data=$fans_data['age_distributions'];//年龄
          $active_days_distributions=$fans_data['active_days_distributions'];//粉丝活跃天数
      
          $insertData['all_fans_num']=$fans_data['all_fans_num'];

          $insertData['man']=$gender_distributions_data[0]['value'];
          $insertData['woman']=$gender_distributions_data[1]['value'];

          $insertData['age_less23']=$age_distributions_data[0]['value'];
          $insertData['age_24_29']=$age_distributions_data[1]['value'];
          $insertData['age_30_35']=$age_distributions_data[2]['value'];
          $insertData['age_36_41']=$age_distributions_data[3]['value'];
          $insertData['age_greater_42']=$age_distributions_data[4]['value'];

          $insertData['day0_4']=$active_days_distributions[0]['value'];
          $insertData['day5_12']=$active_days_distributions[1]['value'];
          $insertData['day13_20']=$active_days_distributions[2]['value'];
          $insertData['day_21_31']=$active_days_distributions[3]['value'];
         
           $insertData['addtime']=time();
          

           $flag=Db::table('daping_douyin')->insert($insertData);
           if ($flag) {
           $data= Db::table('daping_douyin')->order('addtime desc')->find();
             return  json_encode($data);
           }else{
            return json_encode(array('code'=>400,'msg'=>'插入数据失败'));
           }

        }
           }
       
        
    }

其他的没有写。。。暂时写到这

相关文章

网友评论

      本文标题:php 抖音openApi

      本文链接:https://www.haomeiwen.com/subject/nhelgctx.html