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

时间:2013-04-04 16:41:04

标签: php api include google-plus require-once

我有一个页面,它从用户那里获取字符串输入,并将其用作许多搜索API的查询字符串。 我的问题是我的Google+ API工作正常。我也可以让YouTube Data API自行运行。但是,当我将我的代码放在同一页面上时,两个都打破了。

我已经将问题缩小到Google_PlusService.php无法处理的事实,如果我从google-api-php-client / src / contrib /中包含()或require()任何其他.php文件。目录

如果我包含()或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';
}

1 个答案:

答案 0 :(得分:3)

因此,对于那些因此而烦恼的其他人来说,解决方案只是在Google_PlusService.php和Google_YouTubeService.php中复制了类名。

现在很明显,事后我想,我只是没想到任何我可以直接从谷歌下载的东西就是那么愚蠢!

我的解决方案是找到&amp;更换 使用Google_PlusService.php中的Google_GooglePlusActivitiesServiceResource的Google_ActivitiesServiceResource 与 Google_YouTubeService.php中的Google_YoutubeActivitiesServiceResource。

Google_PlusService.php中的Google_Activity与Google_GooglePlusActivity 以及Google_YouTubeService.php中的Google_YoutubeActivity

快乐编码全部!