サポート #2617
未完了TOPのYoutube表示の件
50%
説明
指示内容¶
またTOPのyoutubeでエラー表示が出ています。
確認と対応可能かみてください。
ーーーーーーーーーー
Warning: simplexml_load_file(https://www.youtube.com/feeds/videos.xml?channel_id=UCILd6pe5dcIHzcdmr8FvphA): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /home/xb921021/clean-kaihatsu.co.jp/public_html/wp-content/themes/clean/functions.php on line 62
Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://www.youtube.com/feeds/videos.xml?channel_id=UCILd6pe5dcIHzcdmr8FvphA" in /home/xb921021/clean-kaihatsu.co.jp/public_html/wp-content/themes/clean/functions.php on line 62
Warning: Invalid argument supplied for foreach() in /home/xb921021/clean-kaihatsu.co.jp/public_html/wp-content/themes/clean/functions.php on line 64
ーーーーーーーーーー
ファイル
アデ アルドリノ デフリン さんが23日前に更新
- ステータス を 新規 から 進行中 に変更
- 進捗率 を 0 から 30 に変更
アデ アルドリノ デフリン さんが23日前に更新
- ステータス を 進行中 から フィードバック に変更
- 担当者 を アルドリノ デフリン から 廣瀬 僚一 に変更
- 進捗率 を 30 から 50 に変更
原因¶
サイトが短時間に何度も YouTube の RSS フィードへアクセスしたため、一時的に YouTube 側でアクセスが制限され、404 / 403 エラーが返されました。
その結果、フィードの読み込みに失敗し、取得できなかったデータを foreach() で処理しようとして追加の PHP エラーが発生しています。
対処¶
- YouTube の RSS フィード取得を廃止し、Google YouTube API を使用するよう変更
-
simplexml_load_file()をwp_remote_get()に置き換え - API エラー時のバリデーションを追加
- レート制限対策として取得結果をキャッシュ化
function get_recent_youtube()
{
$rtn = array(
"id" => 'CuuFGMA-N-U',
"title" => '【挑戦と変革】 ユニフォームで差をつけろ!'
);
$cache_key = 'clean_youtube_latest_' . md5(YOUTUBE_PLAYLIST_ID);
$cached = get_transient($cache_key);
if (is_array($cached) && isset($cached['id'], $cached['title'])) {
return $cached;
}
$query = http_build_query(
array(
'part' => 'snippet',
'playlistId' => YOUTUBE_PLAYLIST_ID,
'maxResults' => 1,
'key' => GOOGLE_API_KEY,
),
'',
'&',
PHP_QUERY_RFC3986
);
$url = 'https://www.googleapis.com/youtube/v3/playlistItems?' . $query;
$response = wp_remote_get(
$url,
array(
'timeout' => 10,
'redirection' => 3,
)
);
if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
return $rtn;
}
$contents = wp_remote_retrieve_body($response);
$yt = json_decode($contents);
if (!is_object($yt) || isset($yt->error)) {
return $rtn;
}
if (empty($yt->items[0])) {
return $rtn;
}
$snippet = $yt->items[0]->snippet ?? null;
if (!$snippet || empty($snippet->resourceId->videoId)) {
return $rtn;
}
$rtn = array(
"id" => $snippet->resourceId->videoId,
"title" => $snippet->title,
);
set_transient($cache_key, $rtn, HOUR_IN_SECONDS);
return $rtn;
}
YOUTUBE_PLAYLIST_IDとGOOGLE_API_KEYはwp-config.phpに定義して管理