php - 在 php 中使用 youtube api v3 但出现错误
问题描述
希望你一切顺利。在阅读这个问题时。我在 php 中使用 youtube api v3,但是当我运行代码时,youtube 结构没有缩略图,当我点击它播放视频时,会出现错误:
发生错误。请稍后再试。(播放 ID:JsObjnA1VhNPRivN)了解更多
这是我的html部分代码:
<h2>Search Videos by keyword using YouTube Data API V3</h2>
<div class="search-form-container">
<form id="keywordForm" method="post" action="">
<div class="input-row">
Search Keyword : <input class="input-field" type="search"
id="keyword" name="keyword"
placeholder="Enter Search Keyword">
</div>
<input class="btn-submit" type="submit" name="submit"
value="Search">
</form>
</div>
现在是php部分:
<?php
if (isset($_POST['submit']) )
{
$keyword = $_POST['keyword'];
define('MAX_RESULTS', 1);
if (empty($keyword))
{
$response = array(
"type" => "error",
"message" => "Please enter the keyword."
);
}
}
?>
<?php
if(!empty($response)) {
?>
<div class="response <?php echo $response["type"]; ?>
">
<?php echo $response["message"]; ?>
</div>
<?php
}
?>
<?php
if (isset($_POST['submit']) )
{
if (!empty($keyword))
{
$apikey = 'AIzaSyAjowKxG2mRdr7p1N-3RtBd-BWAgjbf87g';
$googleApiUrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' . $keyword . '&maxResults=' . 'MAX_RESULTS' . '&key=' . $apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $googleApiUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$value = json_decode(json_encode($data), true);
?>
<div class="result-heading">About <?php echo MAX_RESULTS; ?> Results</div>
<div class="videos-data-container" id="SearchResultsDiv">
<?php
for ($i = 0; $i < MAX_RESULTS; $i++) {
$videoId = $value['items'][$i]['id']['videoId'];
$title = $value['items'][$i]['snippet']['title'];
$description = $value['items'][$i]['snippet']['description'];
?>
<div class="video-tile">
<div class="videoDiv">
<iframe id="iframe" style="width:50%;height:50%" src="//www.youtube.com/embed/<?php echo $videoId; ?>"
data-autoplay-src="//www.youtube.com/embed/<?php echo $videoId; ?>?autoplay=1"></iframe>
</div>
<div class="videoInfo">
<div class="videoTitle"><b><?php echo $title; ?></b></div>
<div class="videoDesc"><?php echo $description; ?></div>
</div>
</div>
<?php
}
}
}
?>
解决方案
推荐阅读
- multithreading - 在后台队列上创建 NSAttributedString:“[NSCell init] 必须仅在主线程中使用”
- javascript - 为什么我调用 this.form.submit() 后我的选择没有改变?
- r - R在检查字母顺序的列中交换值
- node.js - Heroku 部署后运行 URL 显示应用程序错误
- java - 如何跟踪 Firestore 读/写操作?
- git - VisualStudio 在 Rebase 时会创建非常不必要的合并冲突?
- c# - Azure Blob 存储的文件大小限制?
- kubernetes - 我可以在没有 virtualBox 的情况下在 ubuntu 上安装 minikube 吗?
- url - HAProxy http-request 将特定路径重定向到另一个路径
- android - Retrofit2:保护baseUrl