Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
video(kuaishou/haokan) (#14)
Browse files Browse the repository at this point in the history
 快手、好看视频相关接口
  • Loading branch information
waset authored Apr 30, 2022
1 parent 930f5a3 commit 6a10896
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"conventionalCommits.scopes": ["kernel", "haokan", "kuaishou", "vscode"]
}
52 changes: 52 additions & 0 deletions src/Haokan/Video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Waset\Haokan;

class Video extends Application
{
/**
* 发布视频
*
* @param string $title
* @param string $video_url
* @param string $cover_images
* @return Video
*/
public function push(string $title, string $video_url, string $cover_images)
{
$api_url = self::BaseUrl . '/video/publish';

$params = [
'app_id' => $this->app_id,
'app_token' => $this->app_token,
"title" => $title,
"video_url" => $video_url,
"cover_images" => $cover_images,
];

$res = $this->https_post($api_url, $params)->toArray();

return $res;
}

/**
* 获取视频状态
*
* @param array $article_id
* @return Video
*/
public function status(array $article_id)
{
$api_url = self::BaseUrl . '/query/status';

$params = [
'app_id' => $this->app_id,
'app_token' => $this->app_token,
"article_id" => implode(',', $article_id),
];

$res = $this->https_post($api_url, $params)->toArray();

return $res;
}
}
43 changes: 43 additions & 0 deletions src/Kernel/BaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,47 @@ public function https_request(string $url, mixed $data = null, mixed $headers =
curl_close($curl);
return ($output);
}

/**
* 上传二进制文件
*
* @param string $url
* @param string $file
* @return array
*/
public function https_byte(string $url, string $file)
{
$payload = '';
$params = "--__X_PAW_BOUNDARY__\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. "\r\n"
. $payload . "\r\n"
. "--__X_PAW_BOUNDARY__\r\n"
. "Content-Type: video/mp4\r\n"
. "Content-Disposition: form-data; name=\"file\"; filename=\"test.mp4\"\r\n"
. "\r\n"
. file_get_contents($file) . "\r\n"
. "--__X_PAW_BOUNDARY__--";

$first_newline = strpos($params, "\r\n");
$multipart_boundary = substr($params, 2, $first_newline - 2);
$request_headers = array();
$request_headers[] = 'Content-Length: ' . strlen($params);
$request_headers[] = 'Content-Type: multipart/form-data; boundary=' . $multipart_boundary;

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output, true);
}
}
153 changes: 153 additions & 0 deletions src/Kuaishou/Video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Waset\Kuaishou;

class Video extends Application
{
/**
* 开始上传视频
*
* @param string $access_token
* @return Video
*/
public function start_upload(string $access_token)
{
$api_url = self::BaseUrl . '/openapi/photo/start_upload';

$body = [
'access_token' => $access_token,
'app_id' => $this->app_id,
];
$res = $this->https_post($api_url, $body)->toArray();

return $res;
}

/**
* 上传视频
*
* @param string $endpoint
* @param string $upload_token
* @param string $file
* @return Video
*/
public function upload(string $endpoint, string $upload_token, string $file)
{
$api_url = "http://${endpoint}/api/upload?upload_token=${upload_token}";


return $this->https_byte($api_url, $file);
}

/**
* 上传视频 - Multipart Form Data上传
*
* @param string $endpoint 上传网关的域名
* @param string $upload_token 上传令牌
* @param string $file 上传文件
*/
public function upload_multipart(string $endpoint, string $upload_token, string $file)
{
$api_url = "http://${endpoint}/api/upload/multipart?upload_token=${upload_token}";

return $this->https_byte($api_url, $file);
}

/**
* 分片上传 - 上传分片
*
* @param string $endpoint 上传网关的域名
* @param string $upload_token 上传令牌
* @param string $fragment_id 分片id 从0开始
*/
public function video_upload_fragment(string $endpoint, string $upload_token, string $fragment_id, mixed $file)
{
$api_url = "http://${endpoint}/api/upload/fragment";
$api_url = $this->https_url($api_url, [
'fragment_id' => $fragment_id,
'upload_token' => $upload_token
]);

return $this->https_byte($api_url, $file);
}

/**
* 分片上传 - 断点续传
*
* @param string $endpoint 上传网关的域名
* @param string $upload_token 上传令牌
* @param string $fragment_id 分片id 从0开始
*/
public function video_upload_resume(string $endpoint, string $upload_token)
{
$api_url = "http://${endpoint}/api/upload/resume";
$api_url = $this->https_url($api_url, [
'upload_token' => $upload_token
]);

return $this->https_get($api_url);
}

/**
* 分片上传 - 完成分片上传
*
* @param string $endpoint 上传网关的域名
* @param string $upload_token 上传令牌
* @param string $fragment_id 分片id 从0开始
*/
public function video_upload_complete(string $endpoint, string $upload_token, string $fragment_count)
{
$api_url = "http://${endpoint}/api/upload/complete";
$api_url = $this->https_url($api_url, [
'upload_token' => $upload_token,
'fragment_count' => $fragment_count,
]);

return $this->https_post($api_url);
}


/**
* 发布视频
*
* @param string $access_token
* @param string $upload_token
* @param string $title
* @param string $cover_images
* @return Video
*/
public function publish(string $access_token, string $upload_token, string $title, string $cover_images)
{
$api_url = self::BaseUrl . '/openapi/photo/publish';
$params = [
'access_token' => $access_token,
'app_id' => $this->app_id,
'upload_token' => $upload_token
];
$url = $this->https_url($api_url, $params);
$body = [
'caption' => $title,
'cover' => file_get_contents($cover_images),
];
$res = $this->https_post($url, $body)->toArray();

return $res;
}

/**
* 删除视频
*
* @param string $access_token
* @param string $photo_id
*/
public function video_delete($access_token, $photo_id)
{
$api_url = self::BaseUrl . '/openapi/photo/delete';

$params = [
'access_token' => $access_token,
'photo_id' => $photo_id
];
return $this->https_post($api_url, $params);
}
}

0 comments on commit 6a10896

Please sign in to comment.