抖音api封装类:lib/Douyin.php
<?php
namespace lib;
class Douyin
{
private $clientKey;
private $clientSecret;
private $apiUrl = 'https://open.douyin.com/';
private $panel = '';
private $header = array();
private $body = array();
/**
* 构造
*/
public function __construct($config = array())
{
$this->clientKey = isset($config['client_key']) ? $config['client_key'] : '';
$this->clientSecret = isset($config['client_secret']) ? $config['client_secret'] : '';
}
/*
* 创建视频
*/
public function createVideo($data = array())
{
if (!(isset($data['access_token']) && $data['access_token'])) {
return array(
'errcode' => 3001,
'errmsg' => 'access_token不能为空',
);
}
if (!(isset($data['openid']) && $data['openid'])) {
return array(
'errcode' => 3002,
'errmsg' => 'openid不能为空',
);
}
if (!(isset($data['video_id']) && $data['video_id'])) {
return array(
'errcode' => 3003,
'errmsg' => 'video_id不能为空',
);
}
$access_token = $data['access_token'];
$openid = $data['openid'];
$video_id = $data['video_id'];
$this->panel = 'video/create/';
$this->header = array(
'Content-Type: application/json',
'access-token: ' . $access_token,
);
$this->body = array(
'video_id' => $video_id,
);
if (isset($data['text']) && $data['text']) {
$this->body['text'] = $data['text'];
}
if (isset($data['micro_app_url']) && $data['micro_app_url']) {
$this->body['micro_app_url'] = $data['micro_app_url'];
}
if (isset($data['micro_app_id']) && $data['micro_app_id']) {
$this->body['micro_app_id'] = $data['micro_app_id'];
}
if (isset($data['micro_app_title']) && $data['micro_app_title']) {
$this->body['micro_app_title'] = $data['micro_app_title'];
}
if (isset($data['poi_name']) && $data['poi_name']) {
$this->body['poi_name'] = $data['poi_name'];
}
if (isset($data['custom_cover_image_url']) && $data['custom_cover_image_url']) {
$this->body['custom_cover_image_url'] = $data['custom_cover_image_url'];
}
if (isset($data['poi_id']) && $data['poi_id']) {
$this->body['poi_id'] = $data['poi_id'];
}
if (isset($data['cover_tsp']) && $data['cover_tsp']) {
$this->body['cover_tsp'] = (float) $data['cover_tsp'];
}
$url = $this->apiUrl . $this->panel . '?open_id=' . $openid;
$result = json_decode($this->post($url, json_encode($this->body)), true);
if (!isset($result['data'])) {
return array(
'errcode' => 3004,
'errmsg' => '远程服务器响应异常',
);
}
if ($result['data']['error_code']) {
return array(
'errcode' => 3005,
'errmsg' => $result['data']['description'],
);
}
return array(
'errcode' => 0,
'errmsg' => 'success',
'data' => array(
'item_id' => $result['data']['item_id'],
),
);
}
/*
* 上传视频
*/
public function uploadVideo($data = array())
{
if (!(isset($data['access_token']) && $data['access_token'])) {
return array(
'errcode' => 3001,
'errmsg' => 'access_token不能为空',
);
}
if (!(isset($data['openid']) && $data['openid'])) {
return array(
'errcode' => 3002,
'errmsg' => 'openid不能为空',
);
}
if (!(isset($data['video']) && $data['video'])) {
return array(
'errcode' => 3003,
'errmsg' => '视频文件不能为空',
);
}
$access_token = $data['access_token'];
$openid = $data['openid'];
$video = realpath($data['video']);
$this->panel = 'video/upload/';
$this->body = array(
'video' => new \CURLFile($video),
);
$this->header = array(
'Content-Type: multipart/form-data',
'access-token: ' . $access_token,
);
$url = $this->apiUrl . $this->panel . '?open_id=' . $openid;
$result = json_decode($this->post($url, $this->body), true);
if (!isset($result['data'])) {
return array(
'errcode' => 3004,
'errmsg' => '远程服务器响应异常',
);
}
if ($result['data']['error_code']) {
return array(
'errcode' => 3005,
'errmsg' => $result['data']['description'],
);
}
return array(
'errcode' => 0,
'errmsg' => 'success',
'data' => array(
'video_id' => $result['data']['video']['video_id'],
'width' => $result['data']['video']['width'],
'height' => $result['data']['video']['height'],
),
);
}
/*
* 上传图片
*/
public function uploadImage($data = array())
{
if (!(isset($data['access_token']) && $data['access_token'])) {
return array(
'errcode' => 3001,
'errmsg' => 'access_token不能为空',
);
}
if (!(isset($data['openid']) && $data['openid'])) {
return array(
'errcode' => 3002,
'errmsg' => 'openid不能为空',
);
}
if (!(isset($data['image']) && $data['image'])) {
return array(
'errcode' => 3003,
'errmsg' => '图片文件不能为空',
);
}
$access_token = $data['access_token'];
$openid = $data['openid'];
$image = realpath($data['image']);
$this->panel = 'image/upload/';
$this->body = array(
'image' => new \CURLFile($image),
);
$this->header = array(
'Content-Type: multipart/form-data',
'access-token: ' . $access_token,
);
$url = $this->apiUrl . $this->panel . '?open_id=' . $openid;
$result = json_decode($this->post($url, $this->body), true);
if (!isset($result['data'])) {
return array(
'errcode' => 3004,
'errmsg' => '远程服务器响应异常',
);
}
if ($result['data']['error_code']) {
return array(
'errcode' => 3005,
'errmsg' => $result['data']['description'],
);
}
return array(
'errcode' => 0,
'errmsg' => 'success',
'data' => array(
'image_id' => $result['data']['image']['image_id'],
'width' => $result['data']['image']['width'],
'height' => $result['data']['image']['height'],
),
);
}
/*
* 获取用户信息
*/
public function getUserInfo($data = array())
{
if (!(isset($data['access_token']) && $data['access_token'])) {
return array(
'errcode' => 3001,
'errmsg' => 'access_token不能为空',
);
}
if (!(isset($data['openid']) && $data['openid'])) {
return array(
'errcode' => 3002,
'errmsg' => 'openid不能为空',
);
}
$access_token = $data['access_token'];
$openid = $data['openid'];
$this->panel = 'oauth/userinfo/';
$this->body = array(
'open_id' => $openid,
'access_token' => $access_token,
);
$this->header = array();
$url = $this->apiUrl . $this->panel;
$result = json_decode($this->post($url, $this->body), true);
if (!isset($result['data'])) {
return array(
'errcode' => 3003,
'errmsg' => '远程服务器响应异常',
);
}
if ($result['data']['error_code']) {
return array(
'errcode' => 3004,
'errmsg' => $result['data']['description'],
);
}
return array(
'errcode' => 0,
'errmsg' => 'success',
'data' => array(
'union_id' => $result['data']['union_id'],
'open_id' => $result['data']['open_id'],
'nickname' => $result['data']['nickname'],
'avatar' => $result['data']['avatar'],
'gender' => $result['data']['gender'],
'country' => $result['data']['country'],
'province' => $result['data']['province'],
'city' => $result['data']['city'],
'e_account_role' => $result['data']['e_account_role'],
),
);
}
/*
* 刷新access_token
*/
public function refreshAccessToken($refresh_token)
{
$this->panel = 'oauth/renew_refresh_token/';
$this->body = array(
'client_key' => $this->clientKey,
'refresh_token' => $refresh_token,
);
$this->header = array(
'Content-Type: multipart/form-data',
);
$url = $this->apiUrl . $this->panel;
$result = json_decode($this->post($url, $this->body), true);
if (!isset($result['data'])) {
return array(
'errcode' => 3001,
'errmsg' => '远程服务器响应异常',
);
}
if ($result['data']['error_code']) {
return array(
'errcode' => 3002,
'errmsg' => $result['data']['description'],
);
}
return array(
'errcode' => 0,
'errmsg' => 'success',
'data' => array(
'refresh_token' => $result['data']['refresh_token'],
'expires_in' => (int) $result['data']['expires_in'] - 300,
),
);
}
/*
* 获取access_token
*/
public function getAccessToken($code)
{
$this->panel = 'oauth/access_token/';
$this->body = array(
'client_key' => $this->clientKey,
'client_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'code' => $code,
);
$this->header = array(
'Content-Type: multipart/form-data',
);
$url = $this->apiUrl . $this->panel;
$result = json_decode($this->post($url, $this->body), true);
if (!isset($result['data'])) {
return array(
'errcode' => 3001,
'errmsg' => '远程服务器响应异常',
);
}
if ($result['data']['error_code']) {
return array(
'errcode' => 3002,
'errmsg' => $result['data']['description'],
);
}
return array(
'errcode' => 0,
'errmsg' => 'success',
'data' => array(
'access_token' => $result['data']['access_token'],
'expires_in' => (int) $result['data']['expires_in'] - 300,
'open_id' => $result['data']['open_id'],
'refresh_expires_in' => (int) $result['data']['refresh_expires_in'],
'refresh_token' => $result['data']['refresh_token'],
'scope' => $result['data']['scope'],
),
);
}
/*
* http协议发送get请求
*/
private function get($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 2);
curl_setopt($ch, CURLOPT_HEADER, 0);
if ($this->header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
}
$out = curl_exec($ch);
curl_close($ch);
return $out;
}
/*
* http协议发送post请求
*/
private function post($url, $data = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if ($this->header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
控制器中调用示例:app/demo/controller/Sample.php
<?php
namespace app\demo\controller;
use app\demo\controller\Base;
use lib\Douyin;
use think\App;
class Sample extends Base
{
protected $app;
private $dy;
/**
* 构造方法
*/
public function __construct(App $app)
{
parent::__construct($app);
$this->dy = new Douyin(array(
'client_key' => '****************',
'client_secret' => '****************',
));
}
/**
* 拼装抖音授权链接地址
*/
public function build()
{
$url = 'https://open.douyin.com/platform/oauth/connect/?';
$query = array(
'client_key' => '****************',
'response_type' => 'code',
'scope' => 'trial.whitelist,user_info,video.create,video.data',
'redirect_uri' => 'https://www.xxx.com/demo/sample/authorize',
'state' => '******',
);
$url .= http_build_query($query);
header('Location:' . $url);
}
/**
* 抖音授权验证
*/
public function authorize()
{
$code = input('code', '');
$state = input('state', '');
// 获取用户openid
$result = $this->dy->getAccessToken($code);
$openid = $result['data']['open_id'];
$access_token = $result['data']['access_token'];
// 获取用户信息
$result = $this->dy->getUserInfo(array(
'openid' => $openid,
'access_token' => $access_token,
));
}
/**
* 发布视频
*/
public function create()
{
$openid = '****************';
$access_token = '****************';
// 上传视频
$result = $this->dy->uploadVideo(array(
'access_token' => $access_token,
'openid' => $openid,
'video' => '这里是视频文件本地路径',
));
$video_id = $result['data']['video_id'];
// 提交发布
$result = $this->dy->createVideo(array(
'access_token' => $access_token,
'openid' => $openid,
'video_id' => $video_id,
'text' => '这里是视频标题',
));
}
}