· 7 years ago · Aug 09, 2018, 09:26 PM
1<?php
2
3session_start();
4
5//en session :
6//$_SESSION['huuid'] = $result['player_uuid'];
7//$_SESSION['social'] = $result['social_type'];
8//$_SESSION['oauth_token']=$temporary_credentials['oauth_token'];
9//$_SESSION['oauth_token_secret']=$temporary_credentials['oauth_token_secret'];
10
11include_once ("Assets/db.php");
12require "vendor/autoload.php";
13use Abraham\TwitterOAuth\TwitterOAuth;
14
15 //this code will run when returned from twiter after authentication
16 //Callback
17 if(isset($_SESSION['oauth_token'])){
18 die("callback");
19 $oauth_token = $_SESSION['oauth_token'];unset($_SESSION['oauth_token']);
20 $consumer_key = 'your consumer key'; //Define in config file
21 $consumer_secret = 'your secret key';//Define in config file
22 $connection = new TwitterOAuth($consumer_key, $consumer_secret);
23 //necessary to get access token other wise u will not have permision to get user info
24 $params=array("oauth_verifier" => $_GET['oauth_verifier'],"oauth_token"=>$_GET['oauth_token']);
25 $access_token = $connection->oauth("oauth/access_token", $params);
26 //now again create new instance using updated return oauth_token and oauth_token_secret because old one expired if u dont u this u will also get token expired error
27 $connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token['oauth_token'],$access_token['oauth_token_secret']);
28 $content = $connection->get("account/verify_credentials");
29
30 $statues = $connection->get("account/verify_credentials");
31 if ($connection->getLastHttpCode() == 200) {
32
33 $userData = json_decode($statues, true);
34 $screenName = $userData['screen_name'];
35 die($screenName);
36
37 $sql= "SELECT * FROM player_social_profile WHERE oauth_token LIKE :oauth_token";
38 $stmt = $bdd->prepare($sql);
39 $stmt->bindParam(':oauth_token', $access_token['oauth_token'], PDO::PARAM_STR);
40 $stmt->execute();
41 $total = $stmt->rowCount();
42
43 if($total > 0)
44 {
45 //update si valeur déjà présente dans player_social_profile $access_token['user_id'] = oauth_uid
46 die("Count sup à 1 donc update en db");
47 $sql = "UPDATE player_social_profile SET oauth_uid = :oauth_uid,
48 oauth_token = :oauth_token,
49 oauth_secret_token = :oauth_secret_token,
50 screen_name = :screen_name
51 WHERE player_uuid = :player_uuid";
52 $stmt = $bdd->prepare($sql);
53 $stmt->bindParam(':oauth_uid', $access_token['user_id'], PDO::PARAM_STR);
54 $stmt->bindParam(':oauth_token', $access_token['oauth_token'], PDO::PARAM_STR);
55 $stmt->bindParam(':oauth_secret_token', $access_token['oauth_token_secret'], PDO::PARAM_STR);
56 $stmt->bindParam(':player_uuid', $_SESSION['huuid'] = $result['player_uuid'], PDO::PARAM_STR);
57 $stmt->bindParam(':screen_name', $screenName, PDO::PARAM_STR);
58 $stmt->execute();
59
60 //on delete l'entré dans player_social_linker
61 $sql = "DELETE FROM player_social_linker WHERE player_uuid = :player_uuid";
62 $stmt = $bdd->prepare($sql);
63 $stmt->bindParam(':player_uuid', $_SESSION['huuid'], PDO::PARAM_STR);
64 $stmt->execute();
65
66 }else{
67 die("count = 0 donc insert en db");
68 //ADD EN DB LA TOTALE ne pas oublier $access_token['user_id']
69 $sql = "INSERT INTO player_social_profile(player_uuid,
70 social_type,
71 oauth_uid,
72 oauth_token,
73 oauth_secret_token,
74 screen_name) VALUES (
75 :player_uuid,
76 :social_type,
77 :oauth_uid,
78 :oauth_token,
79 :oauth_secret_token,
80 :screen_name)";
81
82 $stmt = $bdd->prepare($sql);
83
84 $stmt->bindParam(':player_uuid', $_SESSION['huuid'], PDO::PARAM_STR);
85 $stmt->bindParam(':social_type', $_SESSION['social'], PDO::PARAM_STR);
86 $stmt->bindParam(':oauth_uid', $access_token['user_id'], PDO::PARAM_STR);
87 $stmt->bindParam(':oauth_token', $access_token['oauth_token'], PDO::PARAM_STR);
88 $stmt->bindParam(':oauth_secret_token', $access_token['oauth_token_secret'], PDO::PARAM_STR);
89 $stmt->bindParam(':screen_name', $screenName, PDO::PARAM_STR);
90
91 $stmt->execute();
92
93 //on delete l'entré dans player_social_linker
94 $sql = "DELETE FROM player_social_linker WHERE player_uuid = :player_uuid";
95 $stmt = $bdd->prepare($sql);
96 $stmt->bindParam(':player_uuid', $_SESSION['huuid'], PDO::PARAM_STR);
97 $stmt->execute();
98
99 }
100
101 } else {
102
103 //TODO Redirect to error page
104 session_destroy();
105 die("Vous avez annulé l'authorisation");
106 }
107 session_destroy();
108 }
109 else{
110 die("main startup code");
111 // main startup code
112 $consumer_key = 'your consumer key';//Define in config file
113 $consumer_secret = 'your secret key';//Define in config file
114
115 //Iframe pour l'autorisation ?
116 $connection = new TwitterOAuth($consumer_key, $consumer_secret);
117 $temporary_credentials = $connection->oauth('oauth/request_token', array("oauth_callback" =>'http://devalexisw.cloudcraft.fr/twitter.php'));//http://dev.crm.alifca.com/twitter/index.php
118 $_SESSION['oauth_token']=$temporary_credentials['oauth_token'];
119 $_SESSION['oauth_token_secret']=$temporary_credentials['oauth_token_secret'];
120 $url = $connection->url("oauth/authorize", array("oauth_token" => $temporary_credentials['oauth_token']));
121 // REDIRECTING TO THE URL
122 //Faire un href avec un bouton ?
123 die($url);
124
125 }
126
127
128
129?>