Google Plus PHP API与其他API文件冲突


Google Plus PHP API conflict with other API files

我有一个页面,它接受来自用户的字符串输入,并将其用作许多搜索api的查询字符串。我的问题是,我有谷歌+ API工作在它自己的好。我也可以得到YouTube数据API工作在它自己。然而,当我把我的代码放在同一个页面上时,两者都中断了。

我已经把问题缩小到Google_PlusService.php不能处理它,如果我包括()或要求()从google-api-php-client/src/contrib/目录的任何其他.php文件。

Google_YouTubeService.php复制很好,如果我包括()或要求()任何其他。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';
}

所以,对于那些有几个小时痛苦的人来说,解决方案很简单,就是在Google_PlusService.php和Google_YouTubeService.php中有重复的类名。

现在很明显,事后看来,我只是不认为我可以直接从谷歌下载的东西会那么愚蠢!

我的解决方案是找到&取代在Google_PlusService.php中添加google_googleplusactitiesserviceresource和Google_YouTubeService.php中的google_youtubeactitiesserviceresource。

也与Google_GooglePlusActivity在Google_PlusService.php与Google_YoutubeActivity在Google_YouTubeService.php

祝大家编码愉快!