我如何从 get_video_info URL 在 PHP Json 中获取 Youtube 视频故事板

How Can i Get Youtube Video Story Board from get_video_info URL in PHP Json

我如何从中获取带有签名的 Youtube 视频情节提要缩略图 URL 在 PHP http://www.youtube.com/get_video_info?video_id=V1NW91yW6MA&asv=3&el=detailpage&hl=en_US

我正在尝试这个代码

$url = 'http://www.youtube.com/get_video_info?video_id=V1NW91yW6MA&asv=3&el=detailpage&hl=en_US';
$data = file_get_contents($url);
parse_str($data);
$array = explode('&', $storyboard_spec);

for ($i=0; $i<count($array); $i++){
    $array[$i] =urldecode($array[$i]);
}
$json = json_encode($array);
header('Content-Type: application/json');

echo $json;

我收到了这个 json 回复

["http:\/\/i.ytimg.com\/sb\/V1NW91yW6MA\/storyboard3_L$L\/$N.jpg|48#27#100#10#10#0#default#u1jFfmylte0U7wBdj-0y3T8QUoE|80#45#62#10#10#2000#M$M#6UGOrQjHjUWAcarfFSbcRU_Gpkk|160#90#62#5#5#2000#M$M#hmCD_gj5t0-le7cm2xCBbkED6UI"]

我想要一个URL这样的

http://i.ytimg.com/sb/V1NW91yW6MA/storyboard3_L1/M0.jpg?sigh=6UGOrQjHjUWAcarfFSbcRU_Gpkk

有点难以理解您真正想要的是什么,所以我会在黑暗中尝试一下。这是您要找的吗?

您的代码存在一些问题,导致您无法获得所需的数据:
- 你不应该单独使用 parse_str,因为这会导致意外的变量覆盖和其他不好的东西。
- parse_str 已经用 explode
做了你想做的事 - storyboard_spec 字段包含需要正确解析的特殊格式的所有可用图片的信息

将您的部分代码更改为类似这样的代码应该可以解决问题:

// Fetch the video meta data
$url = 'http://www.youtube.com/get_video_info?video_id=V1NW91yW6MA&asv=3&el=detailpage&hl=en_US';
$data = file_get_contents($url);

// Extract the meta data to an array
$video_info = array();
parse_str($data, $video_info);

// Decode and split up the storyboard specs
$spec_parts = urldecode($video_info['storyboard_spec']);
$spec_parts = explode('|', $spec_parts);

// Extract and build the base URL
$base_url = explode('$', $spec_parts[0]);
$base_url = $base_url[0] . '2/M';

// Extract the sigh parameter
$sigh = explode('#', $spec_parts[3]);
$sigh = array_pop($sigh);

// Find the number of images
if($video_info['length_seconds'] >= 1200) {
    $count = $video_info['length_seconds'] / 240;
} else {
    $count = 5;
}

// Build the URL list
$urls = array();
for($i = 0; $i < $count; $i += 1){
    $urls[] = $base_url . $i . '.jpg?sigh=' . $sigh;
}

// Output the result as JSON
$json = json_encode($urls);
header('Content-Type: application/json');
echo $json;