본문 바로가기
기타

틱톡 API로 동영상 업로드하기 5 : 동영상 업로드 (PHP)

by zgabriel 2025. 12. 29.
728x90
반응형

틱톡API를 이용해서 동영상을 업로드하는 함수를 만들어봤다.

 

영상 업로드 방식은 서버에 있는 영상을 업로드하는 방식과

 

URL이 있는 영상을 업로드하는 방식이 있는데 아래 함수는 두번 째 방법인

 

URL을 이용한 방법이다. 

 

결과 값으로 ID 값을 리턴해주는데 이 아이디 값은 임시 값이 서비스 ID 값은

 

처리가 완료되면 이 임시 값을 이용해 조회할 수 있다.

 

또한, privacy_level 은 sand box 환경에서 테스트하기 위해 SELF_ONLY를 사용했다

 

 

function uploadVod($accessToken, $title, $vodURL){
     $url = 'https://open.tiktokapis.com/v2/post/publish/video/init/';

      $data = [
            'post_info' => [
                'title' => $title,
                  'privacy_level' => 'SELF_ONLY', //PUBLIC_TO_EVERYONE
                  'disable_duet' => false,
                'disable_comment' => true,
                  'disable_stitch' => false,
                  'video_cover_timestamp_ms' => 1000
                  ],
            'source_info' => [
                'source' => 'PULL_FROM_URL',
                'video_url' => $vodURL
            ]
      ];

      $jsonData = json_encode($data);

      $headers = [
            'Authorization: Bearer ' . $accessToken,
            'Content-Type: application/json; charset=UTF-8',
            'Content-Length: ' . strlen($jsonData)  
      ];

      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
      curl_setopt($ch, CURLOPT_POST, 1);  
      curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);  
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
      $response = curl_exec($ch);
      curl_close($ch);

      $responseData = json_decode($response, true);

      if (curl_errno($ch) || $responseData['error']['code']!="ok") {
            echo 'cURL Error: ' . curl_error($ch);
            echo "error msg=" . $responseData['error']['message'];
      } else {
            return $responseData['data']['publish_id'];
      }
}

728x90
반응형