快手开放平台对接内容管理demo

这篇具有很好参考价值的文章主要介绍了快手开放平台对接内容管理demo。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

其中包括用户授权,获取accessToken,获取用户信息,自动上传视频,发布视频,视频列表,删除视频等

<?php
namespace app\controller;

use app\BaseController;
use think\Exception;
use think\facade\App;

class KuaiShou extends BaseController
{

    private $accessToken = "";
    private $appId = "";
    private $appSecret = "";

    private $uploadToken; // 上传令牌
    private $endpoint;//上传网关的域名
    
    private $redirectUri = '';


    /**
     * 用户授权
     * */
    public function authorize($name = 'ThinkPHP6')
    {
        // 要请求的 URL
        $url = 'https://open.kuaishou.com/oauth2/authorize?';
        $params = [
            'app_id'                    => $this->appId,  // 应用唯一标识
            'scope'                     => 'user_info,user_base,user_video_publish,user_video_delete,user_video_info', // code
            'response_type'             => 'code',
            'redirect_uri'              => $this->redirectUri,// 应用授权作用域,多个授权作用域以英文逗号(,)分隔
            'state'                     => '123456789', //安全参数,标识和用户或者设备相关的授权请求。建议开发者实现。回调的时候会带回。
            'ua'                        => 'pc', // 运行环境,普通网页传pc, H5网页不传此参数即可
        ];
        //

        // 调用 CURL GET 请求方法
        $result = $this->performCurlGetRequestWithParameters($url,$params);
        exit;

    }


    /**
     * 获取access_token
     * */
    public function getAccessToken()
    {
        $url = "https://open.kuaishou.com/oauth2/access_token?";
        $data = [
            'app_id' => $this->appId,
            'app_secret' => $this->appSecret,
            'code' => 'b0881372a93aa4434335c4d3e9a1773dcc8cd759c65b965a727a6ec18b85699867841685',
            'grant_type' => 'authorization_code',
        ];

        $result = $this->performCurlGetRequestWithParameters($url,$data);
        dd($result);

    }

    /**
     * 刷新token
     * */
    public function refreshAccessToken()
    {
        $url = "https://open.kuaishou.com/oauth2/refresh_token?";
        $data = [
            'app_id' => $this->appId,
            'app_secret' => $this->appSecret,
            "refresh_token" => "ChJvYXV0aC5yZWZyZXNoVG9rZW4SoAEWZ1a48M_hdbBMnUf1R2Tep9mcpFOllaVLX_uHdVL-1D7pLxV1PrdS-cy44h1ecc8LaWeyA6By76joHJXUE8LHnPjWSq1lotOS1a4GczDxoDo9bAzkoln3lGdTxh3OaHkuXUAPku_IW7H7ql81DPORYZmVK6RQHNb3EVkLBuelvp9nxTwThxZxUYe1LZcMsKGAsbFrXtfMaIhrXclDfIVTGhJmpGi_HIZyL3299lSodYIPN3UiIInoA_U1haHPefAICe8rsnU1-tjTZu3NxcCPR3vzLlERKAUwAQ",
            'grant_type' => 'refresh_token',
        ];

        $result = $this->performCurlGetRequestWithParameters($url,$data);
        dd($result);
    }

    /**
     * 获取用户信息
     * */
    public function getUserInfo()
    {
        $url = "https://open.kuaishou.com/openapi/user_info?";
        $data = [
            'app_id' => $this->appId,
            'access_token' => $this->accessToken
        ];

        $result = $this->performCurlGetRequestWithParameters($url,$data);
        dd($result);
    }

    /**
     * 获取用户手机号码
     * */
    public function getUserPhone()
    {
        $url = "https://open.kuaishou.com/openapi/user_phone?";
        $data = [
            'app_id' => $this->appId,
            'access_token' => $this->accessToken
        ];

        $result = $this->performCurlGetRequestWithParameters($url,$data);
        dd($result);
    }

    /**
     * 发起上传
     * */
    public function startUpload()
    {
        $url = "https://open.kuaishou.com/openapi/photo/start_upload?";
        $data = [
            'app_id' => $this->appId,
            'access_token' => $this->accessToken
        ];
        $params = '';
        foreach ($data as $key => $val) {
            $params .= "$key=$val&";
        }
        $url .= rtrim($params, '&');

        $res = $this->sendPostRequest($url);
        $res = json_decode($res, true);
        if ($res['result'] == 1) {
            $this->uploadToken = $res['upload_token'];
            $this->endpoint = $res['endpoint'];
        }
    }

    /**
     * 视频上传 FormData
     * */
    public function uploadForFormData()
    {
        $basePath = App::getBasePath();
        // 构造runtime文件夹的完整路径
        $runtimePath = $basePath . '../runtime/storage/';;

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');

        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile( 'topic', $file);

        $url = "http://{$this->endpoint}/api/upload/multipart?upload_token={$this->uploadToken}";

        $file_path = $runtimePath.$savename;
        $result = $this->sendPostFileRequest($url, $file_path);
        dd($result);
    }

    /**
     * body二进制 视频上传
     * */
    public function uploadForBinaryData()
    {
        $basePath = App::getBasePath();
        // 构造runtime文件夹的完整路径
        $runtimePath = $basePath . '../runtime/storage/';;

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 上传到本地服务器

        $savename = \think\facade\Filesystem::putFile( 'topic', $file);

        $url = "http://{$this->endpoint}/api/upload?upload_token={$this->uploadToken}";

        $file_path = $runtimePath.$savename;

        $result = $this->sendPostBinaryDataRequest($url, $file_path);

        dd($result);
    }

    /**
     * 分片上传
     * */
    public function uploadFragment()
    {

        $basePath = App::getBasePath();
        // 构造runtime文件夹的完整路径
        $runtimePath = $basePath . '../runtime/storage/';;

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile( 'topic', $file);
        $file_path = $runtimePath.$savename;
        
        // 使用示例
        $chunkSize = 1024 * 1024 * 2; // 分片大小,例如:1MB
        $chunks = $this->splitFileIntoChunks($file_path, $chunkSize);

        $is_file = false;
        foreach ($chunks as $k => $chunk) {
            // 对每个分片进行处理,例如上传到服务器或写入到另一个文件
            $url = "http://{$this->endpoint}/api/upload/fragment?fragment_id={$k}&upload_token={$this->uploadToken}";
            $result = $this->sendPostBinaryDataRequest($url, $chunk, $is_file);
        }
        echo $k;
    }

   /**
    * 断点续传
    * */
    public function uploadResume()
    {
        $endpoint = "upload.kuaishouzt.com";
        $url = "http://{$endpoint}/api/upload/resume?upload_token={$this->uploadToken}";

        $this->performCurlGetRequestWithParameters($url);
    }

    /**
     * 完成分片上传
     * */
    public function uploadComplete()
    {
        $fragment_count = 10;
        $url = "http://{$this->endpoint}/api/upload/complete?upload_token={$this->uploadToken}&fragment_count={$fragment_count}";

        $this->sendPostRequest($url);
    }

    function splitFileIntoChunks($filePath, $chunkSize) {
        $fileHandle = fopen($filePath, 'rb'); // 以二进制读取模式打开文件
        if ($fileHandle === false) {
            return false;// 如果文件打开失败,返回false
        }
        $chunks = [];
        while (!feof($fileHandle)) {
            $buffer = fread($fileHandle, $chunkSize);
            // 读取文件的一个片段
            if ($buffer === false) {
                break;// 如果读取失败,跳出循环
            }
            $chunks[] = $buffer; // 将片段添加到数组中
        }
        fclose($fileHandle); // 关闭文件句柄

        return $chunks;// 返回包含所有片段的数组
    }


    /**
     * 发布视频
     * */
    public function publish()
    {
        $basePath = App::getBasePath();
        // 构造runtime文件夹的完整路径
        $runtimePath = $basePath . '../runtime/storage/';;

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');

        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile( 'topic', $file);
        $file_path = $runtimePath.$savename;

        $url = "https://open.kuaishou.com/openapi/photo/publish?";
        $data = [
            'app_id' => $this->appId,
            'access_token' => $this->accessToken,
            'upload_token' => $this->uploadToken,
        ];

        $params = '';
        foreach ($data as $key => $val) {
            $params .= "$key=$val&";
        }
        $url .= rtrim($params, '&');

        $title = "分片";
        $res = $this->sendPostFileRequest1($url,$file_path,$title);
        dd($res);
    }

    /**
     * 查询用户视频列表
     * */
    public function userVideoList()
    {
        $url = "https://open.kuaishou.com/openapi/photo/list?";
        $data = [
            'access_token'	=> $this->accessToken,
            'app_id'	=> $this->appId
        ];

        $this->performCurlGetRequestWithParameters($url, $data);
    }


    /**
     * 删除视频
     * */
    public function deleteVideo()
    {
        $url = "https://open.kuaishou.com/openapi/photo/delete?";

        $data = [
            'access_token'	=> $this->accessToken,
            'app_id'	=> $this->appId,
            'photo_id' => '3xurpbkusbyh6hw'
        ];

        $params = '';
        foreach ($data as $key => $val) {
            //if ($key=='redirect_uri') $val = urlEncode($val);
            $params .= "$key=$val&";
        }
        $url .= rtrim($params, '&');

        $res = $this->sendPostRequest($url);
        dd($res);
    }

    /**
     * params string $url 请求的url
     * $file_path 文件路径
     * */
    public function sendPostFileRequest1($url,$file_path, $title)
    {
        // 初始化cURL会话
        $ch = curl_init();
        // 设置目标URL
        curl_setopt($ch, CURLOPT_URL, $url);

        // 启用POST方法,并设置请求体的类型为multipart/form-data
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'cover' => new \CURLFile($file_path),
            'caption' => $title
        ));

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 为cURL会话设置适当的请求头
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
        // 设置cURL选项,以便将响应结果直接作为字符串返回,而不是输出到浏览器
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // 执行cURL请求并获取结果
        $response = curl_exec($ch);

        // 检查是否有错误发生
        if (curl_errno($ch)) {
            echo 'cURL error: ' . curl_error($ch);
        }

        // 关闭cURL会话
        curl_close($ch);// 输出响应结果echo $response;

        return $response;
    }


    public function sendPostBinaryDataRequest($url, $binaryData, $is_file = true)
    {
        // 初始化cURL会话
        $ch = curl_init();
        // 设置目标URL,替换{endpoint}为实际的服务器地址

        // 设置请求头
        $headers = array('Content-Type: video/mp4');
        if ($is_file) {
            // 准备二进制数据,这里假设$binaryData是文件的二进制内容
            $binaryData = file_get_contents($binaryData);
        }

        // 替换为实际的文件二进制内容
        // 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $binaryData);

        // 设置cURL选项,以便将响应结果直接作为字符串返回,而不是输出到浏览器
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行cURL请求
        $response = curl_exec($ch);
        // 检查是否有错误发生
        if(curl_errno($ch)) {
            echo 'cURL error: ' . curl_error($ch);
        }
        // 关闭cURL会话
        curl_close($ch);

        // 输出响应结果
        return $response;
    }


    public function sendPostFileRequest($url,$file_path )
    {
        // 初始化cURL会话
        $ch = curl_init();
        // 设置目标URL
        curl_setopt($ch, CURLOPT_URL, $url);

        // 启用POST方法,并设置请求体的类型为multipart/form-data
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'file' => new \CURLFile($file_path),
        ));

        // 为cURL会话设置适当的请求头
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
        // 设置cURL选项,以便将响应结果直接作为字符串返回,而不是输出到浏览器
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // 执行cURL请求并获取结果
        $response = curl_exec($ch);

        // 检查是否有错误发生
        if (curl_errno($ch)) {
            echo 'cURL error: ' . curl_error($ch);
        }

        // 关闭cURL会话
        curl_close($ch);// 输出响应结果echo $response;

        return $response;
    }

    public function sendPostRequest($url, $data = array(), $headers = array()) {
        // 创建 curl 实例
        $ch = curl_init();

        // 设置请求 URL
        curl_setopt($ch, CURLOPT_URL, $url);

        // 设置 POST 请求方式
        curl_setopt($ch, CURLOPT_POST, 1);

        // 设置请求头为 application/json
        //$headers[] = "Content-Type: application/json";
        if ($data) {
            // 将数据以 JSON 格式发送
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        }

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        // 忽略 SSL 认证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        // 设置cURL选项,以便将响应结果直接作为字符串返回,而不是输出到浏览器
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // 执行 curl 请求
        $response = curl_exec($ch);

        // 获取响应代码
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        // 关闭 curl 连接
        curl_close($ch);

        return $response;
    }

    function performCurlGetRequestWithParameters($url, $parameters = []) {
        // 构建带有参数的 URL
        $params = '';
        foreach ($parameters as $key => $val) {
            //if ($key=='redirect_uri') $val = urlEncode($val);
            $params .= "$key=$val&";
        }
        $url .= rtrim($params, '&');
        header("Content-Type: application/json");
        header("Location: $url");
        die();
        var_dump($url);exit;
        // 执行 CURL GET 请求
        $this->performCurlGetRequest($url);

    }

    /**
     * 执行 CURL GET 请求的方法
     *
     * @param string $url 要请求的 URL
     * @return string 请求的结果
     */
    function performCurlGetRequest($url,$data = array()) {
        // 初始化 CURL
        $ch = curl_init();

        // 设置请求方式为 GET
        curl_setopt($ch, CURLOPT_HTTPGET, true);

        // 设置要请求的 URL
        curl_setopt($ch, CURLOPT_URL, $url);

        // 禁用 SSL 证书验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // 设置默认的头部信息
        $headers = [
            'Content-Type: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        // 将数据以 JSON 格式发送
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

        // 执行请求
        $result = curl_exec($ch);

        // 检查请求是否成功
        if (curl_errno($ch)) {
            // 处理请求失败的情况
            echo "CURL 请求失败: ". curl_error($ch);
            return;
        }

        // 关闭 CURL 连接
        curl_close($ch);
        // 返回请求结果

        return $result;
    }

}

文章来源地址https://www.toymoban.com/news/detail-845566.html

到了这里,关于快手开放平台对接内容管理demo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 海康 综合安防管理平台 对接

    1.1 官网介绍 1.2 个人理解 综合安防管理平台部署之后,有2个系统,一个是综合安防管理平台:是用户端系统,一个是运营中心:是综合安防平台的后台管理系统,可提供api与业务平台对接,实现实时预览、录播回放、语音对讲、报警订阅等功能。 1.3 综合安防管理平台 1.4 运

    2024年02月06日
    浏览(30)
  • ChatGPT工作提效之使用python开发对接百度地图开放平台API的实战方案(批量路线规划、批量获取POI、突破数量有限制、批量地理编码)

    ChatGPT工作提效之初探路径独孤九剑遇强则强 ChatGPT工作提效之在程序开发中的巧劲和指令(创建MySQL语句、PHP语句、Javascript用法、python的交互) ChatGPT工作提效之生成开发需求和报价单并转为Excel格式 ChatGPT工作提效之小鹅通二次开发批量API对接解决方案(学习记录同步、用户注

    2024年02月06日
    浏览(37)
  • 动手搓一个kubernetes管理平台(3)-后端框架

    后端框架的选择面比较大,由于不涉及复杂的调度/分布式管理等场景,所以后端选用一个标准的web server即可,比如gin, iris, beego等等,因为正好最近在看iris的一些项目,所以就选用了irsi+corba的框架进行后端开发 。 通过cobra进行初始化的操作就不在赘述,这边先show一下相关的

    2024年01月21日
    浏览(34)
  • 基于springboot的农产品销售管理系统/电商项目/水果超市管理系统/微信小程序毕设/农村电商资源对接平台【附源码】

    🥇 个人主页 :@MIKE笔记 🥈 文章专栏 :毕业设计源码合集 精准扶贫视域下农村电商资源对接平台设计-以“果农无忧” 微信小程序商城为例 基于springboot微信小程序农产品商城 摘要 :随着\\\"互联网+\\\"时代的到来,依托电商平台促进农村资源对接成为了电商扶贫的重要助力。本

    2024年02月03日
    浏览(39)
  • 使用微信提供的云开发实现后端 微信小程序云开发的内容管理CMS

    以前开发一款小程序或者应用啥的,首先就是申请域名租服务器,这是必不可少的步骤。 现在小程序云开发出来后,又再出现内容管理的这个功能,对于开发一款简单的小程序来说,真的是太简单的了。 现成的后台直接配置,一个前端全部搞定。 1.有云开发环境的小程序

    2024年02月08日
    浏览(29)
  • 基于微信小程序的停车场管理平台+ssm后端源码和论文

    由于APP软件在开发以及运营上面所需成本较高,而用户手机需要安装各种APP软件,因此占用用户过多的手机存储空间,导致用户手机运行缓慢,体验度比较差,进而导致用户会卸载非必要的APP,倒逼管理者必须改变运营策略。随着微信小程序的出现,解决了用户非独立APP不可

    2024年01月20日
    浏览(36)
  • 10个优质的基于Node.js的CMS 内容管理平台

    冬尽今宵长 ❝ hi, 大家好, 我是徐小夕,之前和大家分享了很多 「低代码可视化」 和 「前端工程化」 相关的话题, 今天继续和大家聊聊 「CMS」 系统. ❞ 内容管理系统 ( 「CMS」 ) 使没有强大技术背景的人也能够轻松发布内容。我们可以使用 「CMS」 来管理我们的内容和交付。市

    2024年02月09日
    浏览(29)
  • 微信native-v3版支付对接流程及demo

    openssl pkcs12 -in apiclient_cert.p12 -out apiclient_cert.pem -nodes 密码是:商户id https://github.com/wechatpay-apiv3/CertificateDownloader 生成证书 https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient

    2024年02月07日
    浏览(36)
  • Spring Security对接OIDC(OAuth2)外部认证

    前后端分离项目对接OIDC(OAuth2)外部认证,认证服务器可以使用Keycloak。 后端已有用户管理和权限管理,需要外部认证服务器的用户名和业务系统的用户名一致才可以登录。 后台基于Spring Boot 2.7 + Spring Security 流程: 前台浏览器跳转到  后台地址 + /login/oauth2/authorization/my-oid

    2024年02月21日
    浏览(33)
  • uniapp前端支付篇(微信、抖音、快手、h5)四个平台支付

    微信、快手、h5支付步骤大致相同,只有抖音是有自己的支付组件 项目同时支持多个(微信、快手、h5)平台支付,后端那边代码可以封装的 点击支付 创建订单 生成密钥和支付所需要的参数 支付成功 查询订单状态 1.支付按钮 2.支付事件 1.支付按钮 2.支付事件 抖音有自己的

    2024年02月02日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包