Skip to main content

Google login Api with php

How to create Google login

Step: 1 Go to this url
https://cloud.google.com/console

Step 2: Create a project






Step 3: Enter the project name










Step 4: Now on google+ api






Step 5: Now click on credentials and click on create new id










Step 6: Enter the redirect url


















Now put this code in header

<?php
$response_type1 = "code";
$client_id1 = "38789626348.apps.googleusercontent.com";
$redirect_uri1 = "http://example.com/Homes/user_google_login";

$scope1 = "https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile";
//$scope = "https://www.googleapis.com/auth/userinfo.profile";
$state1 = "access";
?>

<script type="text/javascript">
function student_google_login(){
window.open("https://accounts.google.com/o/oauth2/auth?response_type=<?php echo $response_type1;?>&client_id=<?php echo $client_id1;?>&redirect_uri=<?php echo $redirect_uri1;?>&scope=<?php echo $scope1;?>&state=<?php echo $state1;?>", height = 600, width = 400)
  }

</script>

 function user_google_login()
{
$this->autoRender = 'true';
$this->loadModel('EmailTemplate');

$client_id = "3874334626348.apps.googleusercontent.com";
$client_secret = "tQuWyTJnKyErRKMLUj4TcaY2Hw";

$redirect_uri="http://example.com/Homes/user_google_login";

if(!empty($_REQUEST['code'])){

$post = array(
    "grant_type" => "authorization_code",
    "code" => $_REQUEST['code'],
    "client_id" => $client_id,
    "client_secret" => $client_secret,
    "redirect_uri" => $redirect_uri
);
   $postText = http_build_query($post);

//echo "<pre>";print_r($postText); die;

$url = "https://accounts.google.com/o/oauth2/token";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postText);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close($ch);

$access_token1 = explode(":",$result);
$access_token2 = explode(",",$access_token1[1]);
$access_token = str_replace('"','',$access_token2[0]);
$access_token = str_replace("&nbsp;","",$access_token);

$getText = "";
$url1 = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=".str_replace(" ","",trim($access_token));

$ch1 = curl_init();
curl_setopt($ch1,CURLOPT_URL, $url1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
$result111 = (string)curl_exec($ch1);
curl_close($ch1);
$gmail_details = str_replace("{","",$result111);

$gmail_details = str_replace("}","",$gmail_details);
$gmail_details = str_replace('"',"",$gmail_details);
$details = explode(',',$gmail_details);

$i=1;
foreach($details as $detail){
$aa = array();
$detail = str_replace("https://",'|||',$detail);
$aa = explode(':',$detail);
$gmail_details1[trim($aa[0])] = trim(str_replace("|||",'https://',trim($aa[1])));

}
}

Comments

Popular posts from this blog

6 guaranteed steps how to create CRON JOB FUNTION in wordpress

Create Cron Job function in Wordpress plugin Step 1: Register function on plugin activate  register_activation_hook(__FILE__, 'activate_one'); Step 2: add_filter function for interval //Filter for Adding multiple intervals add_filter( 'cron_schedules', 'intervals_schedule' ); // define interval function   function intervals_schedule($schedules) {   $schedules['everyminute'] = array(    'interval' => 60, // Every 1 minutes    'display'  => __( 'Every 1 minutes' )   );   return $schedules;  } Step 3: Activate hook function   //Schedule a first action if it's not already scheduled   function activate_one() {   if (!wp_next_scheduled('wpyog_cron_action')) {    wp_schedule_event( time(), 'everyminute', 'wpyog_cron_action');   }  } Step 4: Cron hook function   //Hook into that action that'll fire after 1 minutes   add_action('wpyog_cron_action', 'execute_

How to Create a jQuery Autocomplete in Wordpress

How to Create a jquery-ui Autocomplete in wordpress. Autocomplete provides suggestions while you type into the text field. In Wordpress we fetch dynamically matched pattern. Include javascript and css files in header. Create action inside functions.php or inside plugin code. add_action('wp_head', 'custom_register_scripts'); function custom_register_scripts(){ wp_register_style( 'techsudhir_jquery_ui_css', plugin_dir_url(__FILE__) . 'css/jquery-ui.css', false,'1.0.0' ); wp_enqueue_style( 'techsudhir_jquery_ui_css' ); wp_register_script('techsudhir_jquery_ui_js',plugin_dir_url(__FILE__) . 'js/jquery-ui.js',array('jquery'),'1.1', false); wp_enqueue_script('techsudhir_jquery_ui_js'); wp_localize_script( 'techsudhir_autocomplete', 'jqueryAutocomplete', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); wp_enqueue_script( 'techsudhir_

Facebook Login

Step 1: Go on Apps & create new Apps Step 2: Enter the app name & choose category Step 3: Enter the site domain Script code <div id="fb-root"></div> <script   src="http://connect.facebook.net/en_US/all.js#appId=<?php echo '603609636351597';?>"> </script> <script>    FB.init({              appId:'<?php echo '603609636351597';?>', cookie:true,              status:true, xfbml:true           });   FB.getLoginStatus(function(response) {   }); Login function call on click function faceLogin(){  FB.login(function(response) {    if (response.authResponse) {  FB.api('/me', function(response) { $('input[id=facebook_login_fb]').val(response['id']); $('#fb_login_form').submit();  });    } else {  console.log('User cancelled login or did