Learner Learner - 1 year ago 49
PHP Question

I am using google API Oauth2 for google sign-in on my website. How do i get the full name and email id variable from this code into my php code?

Here is the code from Google's developer website. I am just a beginner at this. I don't know how to get these variable into php so that I can insert them in my database.

<script>
if (auth2.isSignedIn.get()) {
var profile = auth2.currentUser.get().getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
}
</script>

Answer Source

Using jquery library you can write something like this. Then all details will be available in the $_POST variable inside data.php . This code will give you the general idea.

if (auth2.isSignedIn.get()) {
var profile = auth2.currentUser.get().getBasicProfile();
userData = {
    id: profile.getId(),
    name: profile.getName(),
    givenName: profile.getGivenName(),
    familyName: profile.getFamilyName(),
    imageUrl: profile.getImageUrl(),
    email: profile.getEmail()
};
$.ajax({
    url: "data.php",
    method: "POST",
    data: userData,
    }).done(function(response) {
         // finish function
    }).fail(function(jqXHR, textStatus) {
        // error function
  });
}

$(document).ready(function() {
  if (auth2.isSignedIn.get()) {
    var profile = auth2.currentUser.get().getBasicProfile();
    userData = {
      id: profile.getId(),
      name: profile.getName(),
      givenName: profile.getGivenName(),
      familyName: profile.getFamilyName(),
      imageUrl: profile.getImageUrl(),
      email: profile.getEmail()
    };
    $.ajax({
      url: "data.php",
      method: "POST",
      data: userData,
    }).done(function(response) {
      console.log(response);
    }).fail(function(jqXHR, textStatus) {
      console.log('error');
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download