Sul mio sito, nella sezione video, ho inserito tutti i video presenti sul mio canale, lo script non legge direttamente dalla api di google, per evitare che mi scadono le query al secondo, ma da due tabelle popolate, con i miei video di youtube, tramite questo script:
Codice PHP:
<?php
if (isset($_GET["synch"])) {
$connection->query("DELETE FROM youtube_playlists");
$connection->query("DELETE FROM youtube_videos");
// enumerate playlist on youtube channel
$playlists = file_get_contents("https://www.googleapis.com/youtube/v3/playlists?part=snippet&maxResults=10&channelId=" . YT_CHANNEL . "&key=" . YT_KEY);
$playlists = json_decode($playlists);
$notView = array("Tutte le mie App");
foreach($playlists->items as $items) {
if (in_array($items->snippet->title, $notView)) break;
$it_title = $connection->real_escape_string($items->snippet->title);
$en_title = $connection->real_escape_string($items->snippet->localized->title);
$thumbnail = $connection->real_escape_string($items->snippet->thumbnails->medium->url);
$it_description = $connection->real_escape_string($items->snippet->description);
$en_description = $connection->real_escape_string($items->snippet->localized->description);
$query = "INSERT INTO youtube_playlists VALUES ('$it_title', '$en_title', '$thumbnail', '$it_description', '$en_description')";
$connection->query($query);
$videos_playlist = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId=" . $items->id .
"&key=" . YT_KEY;
$videos_playlist = json_decode(file_get_contents($videos_playlist));
foreach ($videos_playlist->items as $vitems) {
$v_id = $connection->real_escape_string($vitems->snippet->resourceId->videoId);
$v_it_title = $connection->real_escape_string($vitems->snippet->title);
$v_thumbnail = $connection->real_escape_string($vitems->snippet->thumbnails->medium->url);
$v_it_description = $connection->real_escape_string($vitems->snippet->description);
$v_query = "INSERT INTO youtube_videos VALUES ('$en_title', '$v_id', '$v_it_title', '', '$v_thumbnail',
'$v_it_description', '')";
if (!$connection->query($v_query)) echo $connection->error;
}
}
}
?>
<a href="index.php?page=videos&synch">Synchronize playlists / videos with youtube channel</a>
<p />
<?php
$pl_select = $connection->query("SELECT en_title FROM youtube_playlists");
while ($pl_row = $pl_select->fetch_array(MYSQLI_ASSOC)) {
?>
<div style="margin:10px">
<font onclick="" style="cursor:pointer"><span>+</span> <?=$pl_row["en_title"] ?></font>
<?php
$v_select = $connection->query("SELECT playlist, it_title FROM youtube_videos WHERE playlist = '" . $pl_row["en_title"] . "'");
while ($v_row = $v_select->fetch_array(MYSQLI_ASSOC)) {
?>
<div style="margin:10px; margin-left:30px"><?=$v_row["it_title"] ?></div>
<?php
}
?>
</div>
<?
}
?>
Questo script, mi memorizza nel database tutte le playlist e video, per le playlist me le memorizza correttamente, per i video invece no, la query eseguo la query in $v_query mi da il seguente errore:
Codice:
Incorrect string value: '\xE2\x96\xBA So...' for column 'it_description' at row 1
Come mai? Premetto che lo script qualche giorno fa mi andava senza restituirmi nessun problema.