In login.php I have html, ajax code. Login button executes php script redirect.php
to perform twitter authentication.
redirect.php
invokes someother files for authentication, and finally gives result I want: id
and name
I want to retrieve this value in login.php
file.
Login.php
<script type = "text/javascript">
$('#loginTwitter').click(function () {
window.location.href = 'redirect.php';
//After executing, it should come back here to fetch `name` and `id`
$.get('redirect.php', function(data) {
var user_id= data.id;
var name = data.name;
//post name and id to start.php
alert(user_id);
$.post('start.php', { user_id : user_id }, function ()
{
window.location.href = 'start.php';
});
},"json");
});
</script>
redirect.php
<?php
/* Start session and load library. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
/* Save temporary credentials to session. */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
/* If last connection failed don't display authorization link. */
switch ($connection->http_code) {
case 200:
/* Build authorize URL and redirect user to Twitter. */
$url = $connection->getAuthorizeURL($token);
// echo "URL is : $url";
// header('Location: ' . $url);
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->get('account/verify_credentials');
$twitteruser = $content->{'screen_name'};
$notweets = 5;
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
$name = $content->{'name'};
$id = $content->{'id'};
echo json_encode((object) array('id' => $id, 'name' => $name));
break;
default:
/* Show notification if something went wrong. */
echo 'Could not connect to Twitter. Refresh the page or try again later.';
}
?>