您的代码存在一些问题,导致您无法获取您想要的数据:
- 您不应单独使用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;