Php 向控制器发送Google API OAuth2响应
我不熟悉CodeIgniter和PHP。 Google API控制台中给定的重定向URL:Php 向控制器发送Google API OAuth2响应,php,codeigniter,google-api,codeigniter-2,Php,Codeigniter,Google Api,Codeigniter 2,我不熟悉CodeIgniter和PHP。 Google API控制台中给定的重定向URL: Redirect URIs: http://localhost/testsaav/index.php/main/gmail_invite 下面的代码显示了权限请求页面。当我单击允许访问时它会将我带到 http://localhost/testsaav/index.php/main/gmail_invite?code=4/CXD462cen-oEBe1GaHIH90hjqb2X.QpVsg7mG4AUX
Redirect URIs: http://localhost/testsaav/index.php/main/gmail_invite
下面的代码显示了权限请求页面。当我单击允许访问时
它会将我带到
http://localhost/testsaav/index.php/main/gmail_invite?code=4/CXD462cen-oEBe1GaHIH90hjqb2X.QpVsg7mG4AUXaDn_6y0ZQNgaCVLxeAI
我想将它重定向到另一个页面,并在那里显示响应。控制器方法
function gmail_invites($data)
{
$this->load->view('socialInvites2',$data);
}
视图实现
<?php
require_once APPPATH.'libraries/Google_Client.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('Google Contacts PHP Sample');
$client->setScopes("http://www.google.com/m8/feeds/");
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx-xxx');
$client->setRedirectUri('http://localhost/testsaav/index.php/main/gmail_invite');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
$req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
$val = $client->getIo()->authenticatedRequest($req);
//$xml = simplexml_load_string($val->getResponseBody());
//$result = $xml->xpath('//gd:email');
/*foreach ($result as $title) {
echo $title->attributes()->address . "<br>";
}*/
// The contacts api only returns XML responses.
$response = json_encode(simplexml_load_string($val->getResponseBody()));
//print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";
// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
} else {
$auth = $client->createAuthUrl();
}
print "<a class='facebook-button' id='facebookbutton' href='$auth'>
<span class='fb-button-left'></span>
<span class='fb-button-center'>Invite GMail Friends</span>
<span class='fb-button-right'></span></a>"; ?>
<br/>
getResponseBody());
//$result=$xml->xpath('//gd:email');
/*foreach($result作为$title){
echo$title->attributes()->地址。“
”;
}*/
//contacts api仅返回XML响应。
$response=json_encode(simplexml_load_字符串($val->getResponseBody());
//打印“”。打印(json解码($response,true),true)。"";
$client->setRedirectUri('http://localhost/testsaav/index.php/main/gmail_invite');
//访问令牌可能已被延迟更新。
$\会话['token']=$client->getAccessToken();
}否则{
$auth=$client->createAuthUrl();
}
打印“”;?>
为什么视图中有那么多php代码?MVC不是这样工作的
逻辑代码应该保存在控制器中,而不是视图中。我建议您使用Codeiginter OAuth2库,因为来自Google的OAuth2库的原始代码并不意味着与Codeiginter或任何其他特定的MVC框架兼容。例如,您可以使用这个:
您被重定向到正确的控制器,因为url设置在此处:
还要看一下Codeigniter的重定向方法:
redirect('somecontroller/someu方法')