Tom J Nowell posted this code to help me retrieve the YouTube video time.
Functions
// function to parse the code from the URL
function parse_youtube_video_id_from_url( $video_url ) {
$splited_data = explode( '=', $video_url );
$video_unique_code = substr( $splited_data[1], 0, -strlen( strrchr( $splited_data[1], '&' ) ) );
return $video_unique_code;
}
// use the youtube APIs to get a json string containing data about the video
function get_youtube_json_video_data( $video_code ) {
$api_url = 'https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q='.$video_code;
$data = wp_remote_get( $api_url );
return $data['body'];
}
Getting the data:
<?php $youtubecode = 'QiNIfGiNiOk';
$video_data = get_youtube_json_video_data($youtubecode);
$video_data = json_decode( $video_data );
echo '<pre>'.print_r($video_data,true).'</pre>';
?>
The data that the above code retrieves (e.g.):
stdClass Object
(
[apiVersion] => 2.1
[data] => stdClass Object
(
[updated] => 2013-02-27T18:29:22.395Z
[totalItems] => 1
[startIndex] => 1
[itemsPerPage] => 25
[items] => Array
(
[0] => stdClass Object
(
[id] => QiNIfGiNiOk
[uploaded] => 2012-11-30T18:20:08.000Z
[updated] => 2013-02-26T22:33:56.000Z
[uploader] => ramonsteck
[category] => Entertainment
[title] => After Earth - Trailer 2013
[description] => Unete www.facebook.com/Cinemovs Historias Paranormales Reales, PelÃculas de Terror Online y proximos estrenos de cine.
[thumbnail] => stdClass Object
(
[sqDefault] => http://i.ytimg.com/vi/QiNIfGiNiOk/default.jpg
[hqDefault] => http://i.ytimg.com/vi/QiNIfGiNiOk/hqdefault.jpg
)
[player] => stdClass Object
(
[default] => https://www.youtube.com/watch?v=QiNIfGiNiOk&feature=youtube_gdata_player
[mobile] => https://m.youtube.com/details?v=QiNIfGiNiOk
)
[content] => stdClass Object
(
[5] => https://www.youtube.com/v/QiNIfGiNiOk?version=3&f=videos&app=youtube_gdata
[1] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnpiI1ofEgjQhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
[6] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnpiI1ofEgjQhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
)
[duration] => 123
[aspectRatio] => widescreen
[rating] => 2.6722891
[likeCount] => 174
[ratingCount] => 415
[viewCount] => 247218
[favoriteCount] => 0
[commentCount] => 63
[accessControl] => stdClass Object
(
[comment] => allowed
[commentVote] => allowed
[videoRespond] => moderated
[rate] => allowed
=> allowed
[list] => allowed
[autoPlay] => allowed
[syndicate] => allowed
)
)
)
)
)
How can I get the duration of the video (e.g. ‘1:23’).
He told me to:
create a function, hook that function into the ‘save_post’ and ‘publish_post’ actions, that updates the post meta/custom field based
on the above code
How can I create that function hook? And how can i put together:
Custom Field were I put the key:
<?php $key="trailer"; echo get_post_meta($post->ID, $key, true); ?>
To retrieve the video data, specify the YouTube URL code such as:
$youtubecode = 'QiNIfGiNiOk';
————————– UPDATE – The code that Works ————————-
<?php
$key="trailer";
$youtubecode = get_post_meta($post->ID, $key, true);
$video_data = get_youtube_json_video_data($youtubecode);
$video_data = json_decode( $video_data );
foreach ( $video_data->data->items as $data ){
echo brsfl_secondsToTime($data->duration);
}
?>
——————————— Thanks to Brasofilo —————————-
The following are the functions that I use to extract data from a YouTube response to
wp_remote_get
.It’s just the basic stuff, you’ll have to adjust, complement and integrate into your code.