Good day everyone
I was trying to integrate YouTube API Data V3 In PHP and MySQL when i encountered the
following error below.
Fatal error: Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1292
Incorrect datetime value: ‘2022-05-25T15:39:37Z’ for column ‘published_at’ at row 1 in
C:\laragon\www\phpyoutube\index.php:50 Stack trace: #0 C:\laragon\www\phpyoutube\index.php
(50): PDOStatement->execute() #1 {main} thrown in C:\laragon\www\phpyoutube\index.php on
line 50
See my code below
<?php
$key = "**REDACTED**";
$base_url = "https://www.googleapis.com/youtube/v3/";
$channelid = "UCBVDN0GxaRsmtXbsn11kmeQ";
$maxResult = 10;
$API_URL = $base_url . "search?order=date&part=snippet&channelid=" . $channelid .
"&maxResults=" . $maxResult . "&key=" . $key;
/*You can make it according to date or without date as below
$API_URL = $base_url . "search?order=date&part=snippet&channelid=" . $channelid .
"&maxResult=" . $maxResult . "&key=" . $key;
Without date
$API_URL = $base_url . "search?part=snippet&channelid=" . $channelid .
"&maxResult=" . $maxResult . "&key=" . $key;
*/
$videos = json_decode(file_get_contents($API_URL));
include "Dbconnect.php";
$db = new Dbconnect();
$conn = $db->connect();
//echo "<pre>";
//print_r($videos);
foreach ($videos->items as $video) {
$sql = "INSERT INTO videos (id, video_type, video_id, title, thumbnail_url, published_at)
VALUES (NULL, 1, :vid, :title, :turl, :pdate)";
$stmt = $conn->prepare($sql);
//$stmt->bindParam(":vtype", 1);
$stmt->bindParam(":vid", $video->id->videoId);
$stmt->bindParam(":title", $video->snippet->title);
$stmt->bindParam(":turl", $video->snippet->thumbnails->high->url);
$stmt->bindParam(":pdate", $video->snippet->publishedAt);
$stmt->execute();
}
?>
Kindly help