In the era of booming social media, short videos have become an important medium to express creativity and opinions. Kuaishou, as one of the leading short video platforms in China, offers an open API that allows developers to upload, share, and promote videos using PHP.
This article explains step-by-step how to use the PHP Kuaishou API to complete user authorization, video uploading, information retrieval, video sharing, and promotion.
Before calling Kuaishou's API, user authorization is required. Kuaishou uses OAuth 2.0 authorization, and PHP's cURL library can interact with the authorization interface to obtain an access token. The sample code is as follows:
<?php // User Authorization $client_id = 'your_client_id'; // Replace with your client_id $redirect_uri = 'your_redirect_uri'; // Replace with your redirect_uri $scope = 'operate_publish'; // Authorization scope for publishing $state = 'random_state'; // Random string to prevent CSRF $authorize_url = 'https://www.kuaishou.com/oauth2/authorize?client_id=' . $client_id . '&redirect_uri=' . urlencode($redirect_uri) . '&response_type=code&scope=' . $scope . '&state=' . $state; // Redirect to Kuaishou authorization page for user login and approval header('Location: ' . $authorize_url); ?>
After user authorization and obtaining an access token, you can call the Kuaishou video upload API to upload a local video file and receive a video ID. Sample code:
<?php // Upload Video $upload_url = 'https://api.kuaishou.com/rest/2.0/media/upload'; $access_token = 'your_access_token'; // Replace with your access token $video_file = 'path/to/video.mp4'; // Replace with the local video file path $ch = curl_init(); $cfile = curl_file_create($video_file); $data = array('video' => $cfile); curl_setopt($ch, CURLOPT_URL, $upload_url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:' . $access_token)); $response = curl_exec($ch); curl_close($ch); $response_data = json_decode($response, true); $video_id = $response_data['video_id']; ?>
After successfully uploading the video, you can retrieve detailed video information such as title and cover image by video ID. Example code:
<?php // Get Video Information $video_info_url = 'https://api.kuaishou.com/rest/2.0/media/' . $video_id; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $video_info_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:' . $access_token)); $response = curl_exec($ch); curl_close($ch); $video_info = json_decode($response, true); $video_title = $video_info['caption']; $cover_image = $video_info['cover_url']; ?>
Once video information is obtained, you can share the video link on Kuaishou to reach more users. Sample code:
<?php // Share Video $share_url = 'https://live.kuaishou.com/video/' . $video_id; echo 'Click the link below to share the video:<br>'; echo '<a href="' . $share_url . '">' . $share_url . '</a>'; ?>
If you want to further increase video exposure, Kuaishou’s advertising API allows you to create ad campaigns and set creative ads to promote videos precisely. Due to the diversity of requirements, detailed code is not included here.
Following the steps above, developers can leverage PHP and Kuaishou API to complete the full process from uploading to sharing and promoting videos, helping short video content achieve effective distribution.
We hope this guide assists you in mastering Kuaishou API usage and boosts your short video projects to greater success.
Related Tags:
API