Client.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * This file is part of the apiadmin/tiktok.
  4. */
  5. namespace App\Libs\TikTok\OpenPlatform\Video;
  6. use App\Libs\TikTok\Kernel\BaseClient;
  7. use App\Libs\TikTok\Kernel\Exceptions\BadRequestException;
  8. use App\Libs\TikTok\Kernel\Exceptions\HttpException;
  9. use App\Libs\TikTok\Kernel\Exceptions\InvalidArgumentException;
  10. use App\Libs\TikTok\Kernel\Exceptions\InvalidConfigException;
  11. use App\Libs\TikTok\Kernel\Support\Collection;
  12. use GuzzleHttp\Exception\GuzzleException;
  13. use Psr\Http\Message\ResponseInterface;
  14. /**
  15. * Class Client.
  16. *
  17. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  18. */
  19. class Client extends BaseClient {
  20. protected $postAccessToken = false;
  21. /**
  22. * 视频上传
  23. * @param string $filePath
  24. * @return array|Collection|object|ResponseInterface|string
  25. * @throws HttpException
  26. * @throws InvalidConfigException
  27. * @throws GuzzleException
  28. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  29. */
  30. public function uploadVideo(string $filePath) {
  31. return $this->httpUpload('video/upload/', ['video' => $filePath]);
  32. }
  33. /**
  34. * 分片上传视频
  35. * @param string $filePath
  36. * @return array
  37. * @throws BadRequestException
  38. * @throws GuzzleException
  39. * @throws HttpException
  40. * @throws InvalidConfigException|InvalidArgumentException
  41. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  42. */
  43. public function partUpload(string $filePath): array {
  44. $initInfo = $this->httpPostJson('video/part/init/');
  45. if ($initInfo['data']['error_code'] !== 0) {
  46. throw new BadRequestException($initInfo['data']['description']);
  47. }
  48. $this->httpChunkUpload('video/part/upload/', $filePath, [
  49. 'upload_id' => $initInfo['data']['upload_id']
  50. ]);
  51. return $this->httpPostJson('video/part/complete/', [], [
  52. 'upload_id' => $initInfo['data']['upload_id']
  53. ]);
  54. }
  55. /**
  56. * 创建视频
  57. * @param string $videoId
  58. * @param array $body
  59. * @return array
  60. * @throws GuzzleException
  61. * @throws HttpException
  62. * @throws InvalidConfigException
  63. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  64. */
  65. public function createVideo(string $videoId, array $body = []): array {
  66. $body['video_id'] = $videoId;
  67. return $this->httpPostJson('video/create/', $body);
  68. }
  69. /**
  70. * 获取用户视频列表
  71. * @param int $cursor
  72. * @param int $count
  73. * @return array|Collection|object|ResponseInterface|string
  74. * @throws GuzzleException
  75. * @throws HttpException
  76. * @throws InvalidConfigException
  77. */
  78. public function getList(int $cursor, int $count = 10) {
  79. return $this->httpGet('video/list/', [
  80. 'cursor' => $cursor,
  81. 'count' => $count
  82. ]);
  83. }
  84. /**
  85. * 查询特定视频数据
  86. * @param array $item_ids
  87. * @return array|Collection|object|ResponseInterface|string
  88. * @throws GuzzleException
  89. * @throws HttpException
  90. * @throws InvalidConfigException
  91. */
  92. public function getData(array $item_ids) {
  93. return $this->httpPost('video/data/', $item_ids);
  94. }
  95. }