Calling the google client two times giving fatal error
-
21-12-2019 - |
문제
I have a made a simple script that is trying to login and register with google but for that i need to have two different urls. One that should be redirected to login.php
and one to be register.php
. I was not getting way how to do that so i just copied the google client folder where all the config files are kept and i renamed it and changed the redirect url to register.php
and one to login.php
. I thought calling the files two times will work fine for me but no it is giving an fatal error. The error is
<b>Fatal error</b> : Cannot redeclare class Google_Exception in E:\wordpress\InstantWP_4.3.1\iwpserver\htdocs\shoprang\name2\Google_Client.php on line 407
Here is my php
<?php
require_once 'name1/Google_Client.php';
require_once 'name1/contrib/Google_Oauth2Service.php';
$client = new Google_Client();
$client->setApplicationName("Google UserInfo PHP Starter Application");
$oauth2 = new Google_Oauth2Service($client);
$authUrl = $client->createAuthUrl();
echo $authUrl."<br>";
require_once 'name2/Google_Client.php';
require_once 'name2/contrib/Google_Oauth2Service.php';
$cliente = new Google_Client();
$cliente->setApplicationName("Google UserInfo PHP Starter Application");
$oauth2e = new Google_Oauth2Service($client);
$authUrle = $client->createAuthUrle();
echo $authUrle;
?>
Please help me as i am beginner to login api with google. Thanks in advance
해결책
It's so so simple, you are including the files 2 times. Even though you think you have require_once
but the locations are different so they get included again
Remove these
require_once 'name2/Google_Client.php'; //remove this
require_once 'name2/contrib/Google_Oauth2Service.php'; // and this
Because both these files have already been included above
require_once 'name1/Google_Client.php';
require_once 'name1/contrib/Google_Oauth2Service.php';
When the Google Client library file is included for the second time, it tries to re-declare the class and hence you get that error.
다른 팁
The answer above is saying you are declaring the google class once again. Just remove two of those require once.
require_once 'name2/Google_Client.php';
require_once 'name2/contrib/Google_Oauth2Service.php';
Hope this helps you