I have a save post hook that looks like this, it should download youtube thumbnail to wp-includes/uploads base on video id provided. But it doesnt save anything.
add_filter( 'save_post', function () {
$video_id = 'VSB4wGIdDwo';
$path_to_save_thumbnails = '/wp-includes/upload/';
$ch = curl_init();
$thubnail_types = array('0', '1', '2', '3','default',
'sddefault', 'mqdefault',
'hqdefault', 'maxresdefault');
foreach($thubnail_types as $type)
{
$youtube_thumb_url =
'http://img.youtube.com/vi/'.$video_id.'/'.$type.'.jpg';
echo "Downloading thumbnail [$type]." . PHP_EOL;
curl_setopt($ch, CURLOPT_URL, $youtube_thumb_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$image = curl_exec($ch);
$info = curl_getinfo($ch);
if($info['http_code'] == 200) {
file_put_contents($path_to_save_thumbnails.$type.'.jpg', $image);
}
}
});