我有一个页面,它接受用户输入的字符串并将其用作许多搜索 API 的查询字符串。我的问题是我自己的 Google+ API 可以正常工作。我还可以让 YouTube 数据 API 自行运行。但是,当我将代码放在同一页面上时,两者都会中断。
我已将问题缩小到如果我 include() 或 require() google-api-php-client/src/contrib/ 目录中的任何其他 .php 文件,Google_PlusService.php 无法处理它。
如果我 include() 或 require() 任何其他 .php 文件,Google_YouTubeService.php 可以很好地应对。Google_PlusService.php 不能。
即使我不使用这些其他 API,只使用 include() 或 require(),它仍然会中断。
我正处于精神错乱的边缘,因此非常欢迎任何帮助。
<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* For example, it puts together the home page when no home.php file exists.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/
get_header();
?>
<script type='text/javascript'>
function validateSearchTerm() {
var name = $('input[name=brand_name]').val();
var error = 0;
if(name.length == 0)
{
document.getElementById('brand-name-empty').style.display = "block";
error = 1;
}
else {
document.getElementById('brand-name-empty').style.display = "none";
}
return error;
}
</script>
<div id="primary" class="site-content">
<div id="content" role="main">
<div class="-input-block">
<h2>BRAND</h2>
<form id="one-brand" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validateSearchTerm();" method="post">
<label for="brand_name">SEARCH FOR:</label>
<input class="brand_name" type="text" name="brand_name" value=""/>
<span id="brand-name-empty" style="display:none;">Please enter a search term.</span><br/>
<input class="brand_submit" type="submit" name="brand_submit" value="SEARCH"/>
</form>
</div>
<?php
function youtubeandgoogleplusSearch($searchstring) {
require_once("google-api-php-client/src/Google_Client.php");
require_once("google-api-php-client/src/contrib/Google_YouTubeService.php");
require_once("google-api-php-client/src/contrib/Google_PlusService.php");
//IF I UNCOMMENT THE CALENDARSERVICE LINE AND COMMENT OUT THE PLUSSERVICE LINE THEN YOUTUBE WILL WORK OK.
//IF I UNCOMMENT THE CALENDARSERVICE LINE AND COMMENT OUT THE YOUTUBESERVICE LINE THEN PLUSSERVICE WON'T WORK.
//require_once("google-api-php-client/src/contrib/Google_CalendarService.php");
$DEVELOPER_KEY = '{MY API KEY}';
$client = new Google_Client();
$client->setApplicationName('{MY APPLICATION NAME}');
$client->setClientId('{MY CLIENT ID FOR A PROJECT WITH GOOGLE + AND YOUTUBE ENABLED}');
$client->setClientSecret('{MY CLIENT SECRET}');
$client->setRedirectUri('{MY REDIRECT URI}');
$client->setDeveloperKey($DEVELOPER_KEY);
$youtube = new Google_YoutubeService($client);
echo '<h2>YOUTUBE</h2>';
try {
$youtubeSearchResponse = $youtube->search->listSearch('id,snippet', array('q' => $searchstring, 'type' => 'channel', 'maxResults' => '10'));
$channels = '';
$count = 0;
foreach ($youtubeSearchResponse['items'] as $searchResult) {
$snippet = $searchResult['snippet'];
$valuename = $snippet['title'];
$valueid = $snippet['channelId'];
$valuelink = 'http://www.youtube.com/user/'.$valuename;
echo '<input class="youtube_account" type="radio" name="youtube_account" value="'.$valuelink.'"/>
<span><a href="'.$valuelink.'" target="blank">http://www.youtube.com/user/'.$valuename.'</a></span><br/>';
}
}
catch (Google_ServiceException $e) {
echo sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
}
catch (Google_Exception $e) {
echo sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
}
$googleplus = new Google_PlusService($client);
echo '<h2>GOOGLE +</h2>';
try {
$googleplusSearchResponse = $googleplus->people->search($searchstring,array('maxResults' => 10));
foreach ($googleplusSearchResponse['items'] as $googleplusSearchResult) {
$googleplusValuename = $googleplusSearchResult['displayName'];
$googleplusValueid = $googleplusSearchResult['id'];
$googleplusValuelink = $googleplusSearchResult['url'];
echo '<input class="googleplus_account" type="radio" name="googleplus_account" value="'.$googleplusValuelink.'"/>
<span><a href="'.$googleplusValuelink.'" target="blank">'.$googleplusValuelink.'</a></span><br/>';
}
}
catch (Google_ServiceException $e) {
echo sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
}
catch (Google_Exception $e) {
echo sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
}
}
if($_POST['brand_submit']){
$name = $_POST['brand_name'];
$name = str_replace(' ', '%20', $name);
$site = $_POST['brand_site'];
echo '<form id="one-twitter" action="" onsubmit="" method="post">';
youtubeandgoogleplusSearch($name);
echo '</form>';
}
else{
//echo 'not set';
}
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
更多信息:即使我删除了除了 require_once() 语句之外的所有内容,这将起作用:
function youtubeandgoogleplusSearch($searchstring) {
require_once("google-api-php-client/src/Google_Client.php");
require_once("google-api-php-client/src/contrib/Google_PlusService.php");
//require_once("google-api-php-client/src/contrib/Google_YouTubeService.php");
echo 'JUST TESTING';
}
这不会:
function youtubeandgoogleplusSearch($searchstring) {
require_once("google-api-php-client/src/Google_Client.php");
require_once("google-api-php-client/src/contrib/Google_PlusService.php");
require_once("google-api-php-client/src/contrib/Google_YouTubeService.php");
echo 'JUST TESTING';
}