如何在 PHP 中使用 JavaScript 变量?
我现在拥有的是一个简单的 Facebook 应用程序,它从 YouTube 频道中提取了视频信息。
刚才,我将 YouTube 响应的第一个视频以及标题、视图和说明嵌入到占位符中,所有视频(包括第一个视频)都显示在包含嵌入视频的 div 下方。
然后,当用户单击任何视频缩略图时,该视频就会嵌入占位符 div 中。
我可以通过在 YouTube 关联数组中硬编码视频的 id 来实现此功能,但我不知道如何在 jQuery 代码的最后一行中使用 PHP 的 id
变量。
这是我当前的代码:
$url = 'http://gdata.youtube.com/feeds/api/videos?max-results=20&alt=jsonc&orderby=published&format=5&safeSearch=none&author=CHANNELID&v=2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body = curl_exec($ch);
curl_close($ch);
$data = json_decode($body, true);
?>
<div id="placeholder" style="font-family:Helvetica; max-width:500px; margin:0 0 20px 0;">
<div id="player"><iframe width="480" height="274" src="https://www.youtube.com/embed/<?php echo $data['data']['items'][0]['id']; ?>" frameborder="0" allowfullscreen></iframe></div>
<div class="title" style=" font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;"><?php echo $data['data']['items'][0]['title']; ?> </div>
<div class="views" style="color:#BBB; font-size:12px;"><?php echo $data['data']['items'][0]['viewCount'];?> views</div>
<div class="description" style="font-size:14px;"><?php echo $data['data']['items'][0]['description'];?></div>
<div class="hr" style="padding:5px 0 0 0; float:left; border-bottom: 1px solid #96CC90; width:480px;"></div>
</div>
<?php
foreach($data['data']['items'] as $item) {
$id = $item['id'];
$description = $item['description'];
$description = str_replace('uk.opticalexpress.com', '', $description);
$description = str_replace('-', '', $description);
$description = trim($description);
$thumb = $item['thumbnail']['sqDefault'];
$i = 0;
?>
<div class="video" id="<?php echo $i; ?>" style="font-family:Helvetica; float:left; max-width:150px; min-height:130px; margin:0 0px 20px 10px;">
<div class="thumb" style="position:relative; left:0; top:0;">
<img src="<?php echo $item['thumbnail']['sqDefault'];?>" title="<?php echo $description;?>" style="position:relative; top:0; left:0px;"/>
<img src="../images/play-thumb.png" style="position:absolute; top:30px; left:45px;"/>
</div>
<div class="title" style="font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;"><?php echo $item['title']; ?> </div>
<div class="views" style="font-size:12px;"><?php echo $item['viewCount'];?> views</div>
</div>
<?php
$i++;
}
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.video').click(function() {
var id = $(this).attr('id');
$('#placeholder').fadeOut('slow');
$('#placeholder').html('<div id="player"><iframe width="480" height="274" src="https://www.youtube.com/embed/<?php echo $data['data']['items'][1]['id']; ?>" frameborder="0" allowfullscreen></iframe></div>');
$('#placeholder').fadeIn('slow');
});
});
</script>
抱歉,文字墙(和内联CSS)!
What I have at the moment is a simple Facebook app that has pulled the video information from a YouTube channel.
Just now, I embed the first video of the response from YouTube into the placeholder along with the title, views and description and all videos (including the first) are displayed below the div that holds the embedded video.
Then, when the user clicks any of the video thumbnails, that video is then embedded in the placeholder div.
I can get this to work by hard-coding the id of the video inside the YouTube associative array but I can't figure out how to use the id
variable with PHP in the last line of my jQuery code.
Here is my current code:
$url = 'http://gdata.youtube.com/feeds/api/videos?max-results=20&alt=jsonc&orderby=published&format=5&safeSearch=none&author=CHANNELID&v=2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body = curl_exec($ch);
curl_close($ch);
$data = json_decode($body, true);
?>
<div id="placeholder" style="font-family:Helvetica; max-width:500px; margin:0 0 20px 0;">
<div id="player"><iframe width="480" height="274" src="https://www.youtube.com/embed/<?php echo $data['data']['items'][0]['id']; ?>" frameborder="0" allowfullscreen></iframe></div>
<div class="title" style=" font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;"><?php echo $data['data']['items'][0]['title']; ?> </div>
<div class="views" style="color:#BBB; font-size:12px;"><?php echo $data['data']['items'][0]['viewCount'];?> views</div>
<div class="description" style="font-size:14px;"><?php echo $data['data']['items'][0]['description'];?></div>
<div class="hr" style="padding:5px 0 0 0; float:left; border-bottom: 1px solid #96CC90; width:480px;"></div>
</div>
<?php
foreach($data['data']['items'] as $item) {
$id = $item['id'];
$description = $item['description'];
$description = str_replace('uk.opticalexpress.com', '', $description);
$description = str_replace('-', '', $description);
$description = trim($description);
$thumb = $item['thumbnail']['sqDefault'];
$i = 0;
?>
<div class="video" id="<?php echo $i; ?>" style="font-family:Helvetica; float:left; max-width:150px; min-height:130px; margin:0 0px 20px 10px;">
<div class="thumb" style="position:relative; left:0; top:0;">
<img src="<?php echo $item['thumbnail']['sqDefault'];?>" title="<?php echo $description;?>" style="position:relative; top:0; left:0px;"/>
<img src="../images/play-thumb.png" style="position:absolute; top:30px; left:45px;"/>
</div>
<div class="title" style="font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;"><?php echo $item['title']; ?> </div>
<div class="views" style="font-size:12px;"><?php echo $item['viewCount'];?> views</div>
</div>
<?php
$i++;
}
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.video').click(function() {
var id = $(this).attr('id');
$('#placeholder').fadeOut('slow');
$('#placeholder').html('<div id="player"><iframe width="480" height="274" src="https://www.youtube.com/embed/<?php echo $data['data']['items'][1]['id']; ?>" frameborder="0" allowfullscreen></iframe></div>');
$('#placeholder').fadeIn('slow');
});
});
</script>
Sorry for the wall of text (and inline css)!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
困难...我会用 jQuery 观看 .video 上的点击事件,然后对服务器上的 PHP 文件(RESTish)执行 GET 请求,这将生成 iframe 所需的 HTML,然后将 #video html 设置为您刚刚收到的 html。可能有用...
另一种可能性是加载所有 iframe,然后在 .video 上单击仅显示匹配相同唯一 id 或其他内容的 iframe。不过,这意味着用户需要更多的加载时间,最好采用 AJAX 类型方法。
Difficult... I would watch the click event on .video with jQuery, then carry out a GET request to a PHP file on the server (RESTish) which would generate the HTML needed for the iframe, then set the #video html to the html you just received. Might work...
Another possibility, is to load all the iframes, then on .video click just show the one matching the same unique id or something. This would mean more load time for your users though, best to go with the AJAX type approach.
你可以通过这样做来使用ajax,我已经这样做了,希望这也适合你。
you can use the ajax by doing this, i have done it like this way, hope this will also work for you.