抖音用户扫码一键转发视频至抖音php

需求:用户扫码授权后自动转发视频到抖音,转发完成后获得一张优惠券,视频要随机生成(用阿里云视频生产接口提前后台生成好),因为同一个视频会被抖音降权

我用的yii2框架,具体写法需要自己改一改

1:接入抖音jssdk,实现抖音端扫码授权,具体接入步骤参考抖音给的文档

根据抖音给的文档接入,比较值得注意的是签名验证,下边附上签名验证的php代码

后台生成签名

    //验证签名
    public function actionGetSignature(){
        \Yii::$app->response->format = Response::FORMAT_JSON;
        $url = 'https://open.douyin.com/oauth/client_token/';
        $params = [
            'client_key' => 网站应用的clientkey,
            'client_secret' => 网站应用的secret,
            'grant_type' => 'client_credential'
        ];
        $client_data = \Yii::$app->HttpHelper->curlPost($url,$params);//封装好的curl方法 post上传
        $client_data = json_decode($client_data,true);
        $client_acess_token = $client_data['data']['access_token'];
        $jsapi_ticket_url = "https://open.douyin.com/js/getticket?access_token=".$client_acess_token;
        $ticket_info = \Yii::$app->HttpHelper->curlGet($jsapi_ticket_url);//curl get方式上传
        $ticket_info = json_decode($ticket_info,true);
        $ticket = $ticket_info['data']['ticket'];
        $timestamp = "1597213176393";//时间串必须为string类型 否则签名不通过
        $nonce_str = 'ABCD561';//随机一个字符串
        $string = "jsapi_ticket=$ticket&nonce_str=$nonce_str×tamp=$timestamp&url=要跳转的授权页面";
        $signature = md5($string);//签名MD5加密
        $resData['signature'] = $signature;
        $resData['timestamp'] = (string)$timestamp;
        $resData['nonce_str'] = $nonce_str;
        return $resData; 
        
    }

页面验证签名




    
    授权






2:授权成功后,调用openapi的视频/video/upload/接口获取video_id

    //上传视频 获取videoid
    public function getVideoId($open_id,$access_token,$url){       
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://open.douyin.com/video/upload?open_id=' . $open_id. '&access_token=' . $access_token,
            CURLOPT_RETURNTRANSFER => true,
            //CURLOPT_ENCODING => '',
            //CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            //CURLOPT_FOLLOWLOCATION => true,
            //CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            //CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => array('video' => new \CURLFILE($url)),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $response = json_decode($response, true);
        if($response['data']['error_code'] == 0){
            return $response['data']['video']['video_id'];
        }
        return $null;
    }

3:调用/video/create/接口,拿着video_id上传视频 具体的优惠券领取代码比较简单,我这边搭demo的时候略过了

    
    //上传video
    public function actionCreateVideo(){
        \Yii::$app->response->format = Response::FORMAT_JSON;
        $open_id = $_POST['openid'];
        $access_token = 授权时存起来的accesstoken;
        $url = $_POST['url'];//这个url为要转发视频的url
        $video_id = $this->getVideoId($open_id, $access_token, $url);
        if(!$video_id){
            return ['code' => 1,'msg' => '转发失败'];
        }
        $url = "https://open.douyin.com/video/create?open_id=$open_id&access_token=$access_token";
        $params = [
            'video_id' => $video_id,
            'text' => '好好吃饭鸭'
        ];
        $video_info = \Yii::$app->HttpHelper->curlPost($url,$params,true);
        $video_info = json_decode($video_info,true);
        if(isset($video_info['data']['item_id'])){
            return ['code' => 1,'msg' => '卡券领取成功'];
        }
        return ['code' => 0,'msg' => '卡券领取失败'];
    }

你可能感兴趣的:(javascript,ajax,php)