Я работаю в отделе по работе с клиентами и создаю приложение, которое отвечает на каждый комментарий YouTube.
Итак, то, что я сейчас делаю, это в основном скрипт, который извлекает данные комментариев из YouTube Data API v3. Этот сценарий представляет собой циклический сценарий, который вызывается каждые 30 секунд, однако вы можете знать, что API YouTube имеет лимит квоты, и я продолжаю его нажимать.
Я открыт для любого решения:
- Должен ли я подавать заявку на дополнительную квоту на YouTube
- Какую квоту я должен применить (в основном я просто вытаскиваю комментарий data, from who, id и timestamp) или sis любым другим способом.
мой код
<?php
if (!file_exists(__DIR__ . '/youtube_vendor/autoload.php')) {
throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/youtube_vendor/autoload.php';
include "mysql.php";
$db = new db();
$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope('https://www.googleapis.com/auth/youtube.readonly');
$client->addScope('https://www.googleapis.com/auth/youtube.force-ssl');
$client->setRedirectUri('your_url');
// offline access will give you both an access and refresh token so that
// your app can refresh the access token without user interaction.
$client->setAccessType('offline');
// Using "consent" ensures that your application always receives a refresh token.
// If you are not using offline access, you can omit this.
$client->setPrompt("consent");
$client->setIncludeGrantedScopes(true); // incremental auth
$auth_url = $client->createAuthUrl();
if(isset($_GET['code'])) {
// id index exists
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
// var_dump($access_token);
// echo "<br><br>";
// file_put_contents("received.txt",var_dump($access_token));
// $access_token = file_get_contents("received.txt");
// // $file = json_decode($fb);
// var_dump($access_token);
// serialize your input array (say $array)
$serializedData = serialize($access_token);
// save serialized data in a text file
file_put_contents('youtube_access_token.txt', $serializedData);
// at a later point, you can convert it back to array like:
$recoveredData = file_get_contents('youtube_access_token.txt');
// unserializing to get actual array
$access_token = unserialize($recoveredData);
// you can print your array like
print_r($access_token);
echo "<br>";
$client->setAccessToken($access_token);
$service = new Google_Service_YouTube($client);
// $channel = $youtube->channels->listChannels('snippet', array('mine' => $mine));
// var_dump($channel);
$queryParams = [
'maxResults' => 25,
'mine' => true
];
$arrayComment = array();
$arrayReplies = array();
$responseVideo = $service->activities->listActivities('snippet,contentDetails', $queryParams);
foreach($responseVideo['items'] as $video)
{
$db->insert_youtube_video($video['snippet']['channelId'],$video['snippet']['channelTitle'],$video['snippet']['publishedAt'],$video['snippet']['title'],$video['snippet']['description'],$video['snippet']['thumbnails']['standard']['url'],$video['contentDetails']['upload']['videoId']);
$queryParams = [
'videoId' => $video['contentDetails']['upload']['videoId']
];
$responseComment = $service->commentThreads->listCommentThreads('snippet,replies', $queryParams);
foreach($responseComment['items'] as $comment)
{
$db->insert_youtube_comment($comment['snippet']['topLevelComment']['snippet']['authorChannelUrl'],$comment['snippet']['topLevelComment']['snippet']['authorDisplayName'],$comment['snippet']['topLevelComment']['snippet']['authorProfileImageUrl'],$comment['snippet']['topLevelComment']['snippet']['publishedAt'],$comment['snippet']['topLevelComment']['snippet']['updatedAt'],$comment['snippet']['topLevelComment']['snippet']['textDisplay'],$comment['snippet']['topLevelComment']['snippet']['videoId'],$comment['snippet']['topLevelComment']['id']);
$queryParams = [
'parentId' => $comment['snippet']['topLevelComment']['id']
];
$responseReplies = $service->comments->listComments('snippet', $queryParams);
foreach ($responseReplies['items'] as $replies)
{
$db->insert_youtube_replies($replies['snippet']['authorChannelUrl'],$replies['snippet']['authorDisplayName'],$replies['snippet']['authorProfileImageUrl'],$replies['snippet']['publishedAt'],$replies['snippet']['updatedAt'],$replies['snippet']['textDisplay'],$replies['snippet']['videoId'],$comment['snippet']['topLevelComment']['id'],$replies['id']);
}
$arrayReplies[] = $responseReplies;
}
$arrayComment[] = $responseComment;
}
}
else
{
echo $auth_url;
}
?>
<textarea style="width:100%;height:300px"><?php print_r($responseVideo['items']); ?><?php print_r($arrayComment); ?><?php print_r($arrayReplies); ?></textarea>
<script>
setTimeout(function () { window.location.reload(); }, 15*1000);
document.write(new Date());
</script>