У меня есть функция php, которая загружает эскизы YouTube на сервер, и она отлично работает в моей локальной версии, однако, как только она загружается на сервер, все файлы .jpg, которые она загружает, пусты.
function download_thumbnail() {
preg_match('/src = "(.+?)"/', get_field('youtube_link'), $matches); //Pull the URL of the video out of the iframe generated by ACF
preg_match("/embed/(.+)\?/", $matches[1], $vid_id); //Pull the ID of the video from the URL
$download_url = "https://img.youtube.com/vi/".$vid_id[1]."/maxresdefault.jpg"; //Use the standard youtube link to get the max res thumbnail
$uploads = wp_upload_dir();
$path = $uploads['path']."/"; //Get the upload path
$filename = $vid_id[1].'.jpg'; //Set the filename as the ID of the video
$save_as = $path.$filename; //Full path of the created file
if (!file_exists($save_as)) { //Check if file exists
$ch = curl_init($download_url); //Initialize the PHP cURL to the youtube page
$fp = fopen($save_as, 'wb'); //Start the path link
curl_setopt($ch, CURLOPT_FILE, $fp); //Download the image to the path link
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$filetype = wp_check_filetype( basename( $save_as ), null ); //Check the file type
$attachment = array( //Create neccessary wordpress data to the image
'guid' => $uploads['url'] . '/' . basename( $save_as ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $save_as ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $save_as, get_the_ID() ); //Add neccessary wordpress data to the image
require_once( ABSPATH . 'wp-admin/includes/image.php' ); //Make sure that the image gets registered as an image
$attach_data = wp_generate_attachment_metadata( $attach_id, $save_as ); //Generate more meta data
wp_update_attachment_metadata( $attach_id, $attach_data ); //Attatch the meta data
set_post_thumbnail( get_the_ID(), $attach_id ); //Set image as the featured image
}
}
Любая помощь / понимание приветствуются.
Спасибо
Да, функция принимает iframe, использует регулярное выражение для получения идентификатора видео, а затем загружает изображение из img.youtube.com/vi/<insertvidIDhere>/maxresdefault.jpg
curl_exec, вероятно, возвращает false. Вы не проверяете возвращаемое значение и не вызываете curl_errno, поэтому вы не знаете, в чем ошибка. Лучше начать с этого. Возможно вам понадобится curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);Спасибо, Крис, это сработало отлично






вы пытались получить доступ к YouTube через сервер?