gpt4 book ai didi

php - 简单数组重定向

转载 作者:行者123 更新时间:2023-12-03 06:34:05 27 4
gpt4 key购买 nike

我正在尝试编写一个将用户重定向到基于上传者的随机youtube视频的代码。该脚本根本没有加载,我也不知道为什么。这是剧本

<?php
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USER/uploads?max-results=50';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry) {
$watch = (string)$media->group->player->attributes()->url;
$videoid = substr($watch, 31, -29);
$finalid = "\"$videoid\", ";
$urls = array ($finalid);
$url = $urls[array_rand($urls)];
header("Location: http://www.youtube.com/watch?v=$url&list=UL");
}

?>

最佳答案

您的代码应该看起来像这样:

<?php
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USER/uploads?max-results=50';
$sxml = simplexml_load_file($feedURL);

// check the return value
if( $sxml === false ) { die('error retrieving XML data'); }

// declare an empty array
$video_ids = array();

// loop through returned data
foreach ($sxml->entry as $entry) {
$watch = (string)$media->group->player->attributes()->url;
// add each ID to the array
$video_ids[] = substr($watch, 31, -29);
}

// check that there were some actual videos in that data
if( empty($video_ids) ) { die('No videos returned'); }

// pick
$url = $video_ids[array_rand($video_ids)];
//redirect
header("Location: http://www.youtube.com/watch?v=$url&list=UL");

关于php - 简单数组重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16866724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com