Jump to content
php.lv forumi

youtube embed


yeahz

Recommended Posts

šis ir tas ko youtube piedāvā:

<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/sFZwcgoBxv8?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/sFZwcgoBxv8?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>

 

gribējās uztaisīt tādu skriptiņu, lai lietotājam būtu jāpievieno tikai youtube links, piemēram -

http://www.youtube.com/watch?v=sFZwcgoBxv8&feature=rec-LGOUT-farside_rn-4r-13-HM

taču, ja pievieno šādu linku, tad video grib strādāt.

 

papētīju, ka ja nomaina to daļu no linka (sFZwcgoBxv8) pret cita video linka daļu, tad strādā, taču nespēju izdomāt, kā lai to daļu izvelk, varētu nogriezt sākumu

http://www.youtube.com/watch?v=

, un izdzēst beigas, taču beigas var būt visvisādākās, kaut vai šādas &feature=rec-LGOUT-farside_rn-4r-13-HM , vai šādas &feature=player_embedded kā arī var vispār nebūt.

 

Kādam ir kādi risinājumi?

 

Drošvien šis ir no sērijas "divritenis jau ir izgudrots" :)

Edited by yeahz
Link to comment
Share on other sites

uz ātro, bet vajadzētu derēt

$youtube = 'http://www.youtube.com/watch?v=sFZwcgoBxv8&feature=rec-LGOUT-farside_rn-4r-13-HM';
$newyoutubelink = false;
if (strpos($youtube,'?')!==false) {
 $parts = array();
 parse_str(array_pop(explode('?',$youtube,2)), $parts);
 $newyoutubelink = 'http://www.youtube.com/watch?v='.$parts['v'];
}

Link to comment
Share on other sites

Izvilkums no mana koda, strādā viss pareizi un nočeko vai gadījumā embedošana nav diseiblota.

	if(!($url = parse_url($video_link))) return add_video(notice_success('Ievadi derīgu video saiti.'));
	if($url['host'] == 'www.youtube.com'){
		if(empty($url['path']) || $url['path'] != '/watch' || empty($url['query'])) return add_video(notice_success('Ievadi derīgu video saiti.'));
		$vars = array();
		parse_str($url['query'], $vars);
		if(empty($vars['v'])) return add_video(notice_success('Ievadi derīgu video saiti.'));
		if(!($data = file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.urlencode($vars['v']).'?v=2&alt=json'))) return add_video(notice_success('Ievadi derīgu video saiti.'));
		$data = json_decode($data, true);
		foreach($data['entry']['yt$accessControl'] as $item){
			if($item['action'] == 'embed'){
				if($item['permission'] != 'allowed') return add_video(notice_success('Šo video nav atļauts ievietot citās vietnēs.'));
				break;
			}
		}
		$video_data['vtype'] = self::TYPE_YOUTUBE;
		$video_data['file_name'] = $vars['v'];
		$image = 'http://i.ytimg.com/vi/'.urlencode($vars['v']).'/0.jpg';
	}elseif($url['host'] == 'vimeo.com'){
		if(empty($url['path'])) return add_video(notice_success('Ievadi derīgu video saiti.'));
		$vars = explode('/', $url['path']);
		if(empty($vars[1]) || !is_numeric($vars[1])) return add_video(notice_success('Ievadi derīgu video saiti.'));
		$data = json_decode(file_get_contents('http://vimeo.com/api/v2/video/'.urlencode($vars[1]).'.json'));
		if(count($data) != 1) return add_video(notice_success('Ievadi derīgu video saiti.'));
		$data = $data[0];
		$video_data['vtype'] = self::TYPE_VIMEO;
		$video_data['file_name'] = $vars[1];
		$image = $data->thumbnail_medium;
	}else{
		return add_video(notice_success('Ievadi derīgu video saiti.'));	
	}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...