显示YouTube时间自动从关键点开始

时间:2013-02-27 作者:Batman

为什么我在制作预告片,我想从Youtube视频的。。。

自定义字段

$key=“拖车”表示拖车Ex的钥匙(http://www.youtube.com/watch?v=QiNIfGiNiOk WARE key=QiNIfGiNiOk)$key=“img\\U拖车”是youtube 0的img。jpg是youtube视频的时间,这是我手动输入的问题“我可以让youtube时间自动检索吗?如何让代码使用更少的查询?”

这是我的代码:

<?php
$recentPosts = new WP_Query();
$recentPosts-> query(array(\'showposts\' => 4,\'orderby\' => \'rand\', \'post_type\' =>array(\'trailers\')));
?>
<div class="art-Post" >
    <div class="art-Post-tl"></div>
    <div class="art-Post-tr"></div>
    <div class="art-Post-bl"></div>
    <div class="art-Post-br"></div>
    <div class="art-Post-tc"></div>
    <div class="art-Post-bc"></div>
    <div class="art-Post-cl"></div>
    <div class="art-Post-cr"></div>
    <div class="art-Post-cc"></div>
    <div class="art-Post-body">

<h2 id="titlu-descriere" class="titlu-d">Trailere care s-ar putea sa-ti placa</h2>
<div class="linie-total"></div>   
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div class="box-trailere">
<div class="trailer-img">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__(\'Trailer film %s\', \'kubrick\'), the_title_attribute(\'echo=0\')); ?>">
<img src="http://img.youtube.com/vi/<?php $key="trailer"; echo get_post_meta($post->ID, $key, true); ?>/<?php $key="img_trailer"; echo get_post_meta($post->ID, $key, true); ?>">
<span class="duration"><?php $key="time"; echo get_post_meta($post->ID, $key, true); ?></span>
</a>
</div>
<span class="trailer-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></span>
</div>   
<?php endwhile; ?>

<div class="cleared"></div>
    </div>   
</div>
-------------------------------更新-------------------

密码告诉我这个

stdClass Object
(
    [apiVersion] => 2.1
    [data] => stdClass Object
        (
            [updated] => 2013-02-27T18:12:03.984Z
            [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
                                    [embed] => allowed
                                    [list] => allowed
                                    [autoPlay] => allowed
                                    [syndicate] => allowed
                                )

                        )

                )

        )

)

1 个回复
SO网友:Tom J Nowell

这两个函数将从YouTube API获取有关视频的数据:

    // 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 wp_remote_retrieve_body( $data );
    }
您必须使用这些来获取时间数据

因此,对于您的示例,下面是如何获取数据:

$youtubecode = \'QiNIfGiNiOk\'; // replace with your youtube code or variable etc etc..
$video_data = get_youtube_json_video_data($youtubecode);
$video_data = json_decode( $video_data );
最后,要查看您得到的数据,让我们传递video_data 进入print_r 要在屏幕上显示一些调试输出,请执行以下操作:

// lets print out the data so we can see what we got:
printf( \'<pre>%s</pre>\', var_export( $video_data, true ) );
你应该看到pre 包含PHP数据结构的标记,其中包含您可能想知道的关于YouTube视频的所有信息(假设视频存在)。

就这样,$video_data 应包含时间、描述、上载者、所有缩略图、评级等

然后,您将使用这些新发现的知识创建一个函数,将该函数挂接到\'save_post\'\'publish_post\' 操作,它基于上述代码更新post meta/custom字段

如何使用过滤器和操作,或者如何从自定义字段中获取或保存到自定义字段,超出了您的问题范围。

结束

相关推荐

我的存档-posttype.php模板未加载

我正在尝试创建一个存档照片。我的照片自定义帖子类型的php模板。我的问题是,当我查看页面时,它是归档文件。使用的是php模板,而不是存档照片。php。我只是想要存档照片。查看特定自定义帖子类型的归档页面时要使用的php。我错过了什么?请注意,在最后,我已将照片自定义帖子类型设置为主循环。非常感谢您的时间和帮助。<?php //custom post type add_action( \'init\', \'create_post_type\' ); function crea