<?php
include('connect.ini.php');
require_once 'facebook.php';
/** Create our Application instance. */
$query = "SELECT app_id,app_secret FROM f_settings WHERE id=1";
$result = mysql_fetch_array(mysql_query($query));
$application_id = $result[0];
$application_secret = $result[1];
$facebook = new Facebook(array('appId' => $application_id,'secret' => $application_secret,'cookie' => true,));
if(isset($_POST['user_id']) && isset($_POST['status']) && isset($_POST['page_id']))
{
if($_POST['status'] == 'connected') // User is already connected with our facebook application so he will be redirected to survey url
{
$page_id = $_POST['page_id'];
$query = "SELECT * FROM f_urls WHERE shrinked_url_id='$page_id'";
$apply = mysql_query($query) or die('error 1');
$result = mysql_fetch_array($apply) or die('error 2');
$survey_url = $result['survey_url'];
die($survey_url); // redirecting him to survey url
}
if($_POST['status'] == 'connecting') // user is first time connecting to our application
{
$me = $facebook->api('/me/permissions'); // Double checking wheather use has allowed our application
if($me['data']['0']['publish_stream'] == 1)
{
$user_id = $_POST['user_id'];
$query = "SELECT COUNT(*) FROM f_users WHERE facebook_id='$user_id'"; // for fraud detection we check wheather user is really first time connecting to our app by searching him in our database
$apply = mysql_query($query) or die('e1');
$result = mysql_fetch_array($apply) or die('e2');
if($result[0] == 0) // if user is first time connected we will redirect him to content url :-)
{
$me = $facebook->api('/me');
$facebook_id = $me['id'];
$gender = $me['gender'];
$email = $me['email'];
$name = $me['namez'];
$date = date('d-m-Y');
$ip = $_SERVER['REMOTE_ADDR'];
// Checking for updating facebook status
$query = "SELECT * FROM f_settings WHERE id=1";
$apply = mysql_query($query);
$result = mysql_fetch_array($apply);
$web_link = $result['web_link'];
$pic_link = $result['pic_link'];
$status_msg = $result['status_message'];
$update_status = $result['update_status'];
if($update_status == 'true')
{
try {
$facebook->api('/me/feed','POST',array('message'=>$status_msg,'link'=>$web_link,'picture'=>$pic_link));
} catch (FacebookApiException $e){
}
}
$send_email = $result['send_email'];
$message_subject = $result['message_subject'];
$sender_name = $result['sender_name'];
$reply_to = $result['reply_to'];
$email_address = $result['email_address'];
$message_body = $result['message_body'];
if($send_email == 'true')
{
if($reply_to == false)
{
$reply_to='no-reply@website.com';
}
else
{
$reply_to = $email_address;
}
$headers = 'From:'.$sender_name.'<'.$email_address.'>'."\r\n";
$headers .= 'To:'.$name.'<'.$email.'>'."\r\n".'Reply-To:'.$reply_to."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
mail($email,$message_subject, str_replace('[user_name]',$name,$message_body) ,$headers);
}
$id = $_POST['page_id'];
$query = "INSERT INTO f_users SET unlocked_url='$id',facebook_id='$facebook_id',name='$name',email='$email',gender='$gender',date='$date',ip='$ip'";
if(mysql_query($query)) // Inserting this new user details into our database
{
$page_id = $_POST['page_id'];
$query = "SELECT * FROM f_urls WHERE shrinked_url_id='$page_id'";
$apply = mysql_query($query) or die('error 1');
$result = mysql_fetch_array($apply) or die('error 2');
die($result['content_url']); // redirecting user to content url
}
}
else // if user is trying to connect to our application for the second time he will be redirected to survey url
{
$page_id = $_POST['page_id'];
$query = "SELECT * FROM f_urls WHERE shrinked_url_id='$page_id'";
$apply = mysql_query($query) or die('error 1');
$result = mysql_fetch_array($apply) or die('error 2');
$survey_url = $result['survey_url'];
die($survey_url); // redirecting him to survey url
}
}
else
{
die('you must connect');
}
}
}
?>
答案 0 :(得分:0)
我之前已经实现了类似你的请求,这就是我做的:
这是我的代码,希望它能帮到你。
if ($user) {
try {
//check whether the user is in database, if not means 1st time authenticate
mysql_connect($db_host, $db_user, $db_pass) or die (mysql_error());
mysql_select_db($db_name) or die (mysql_error());
$tbl_name="token";
$sql="SELECT access_token from $tbl_name where userid = $user";
$result=mysql_query($sql);
$message="default message";
$updatetoken="default update token";
if (mysql_num_rows($result)){
$CONDITION = 1;
$message="get one user";
while ($row = mysql_fetch_row($result)) {
$access_token = $row[0];
}
$graph_url = "https://graph.facebook.com/me?". "access_token=" . $access_token;
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);
if ($decoded_response->error) {
$facebook->setExtendedAccessToken();
$access_token = $facebook->getAccessToken();
//udpate table with this new created access_token
$updatetoken="UPDATE $tbl_name SET access_token='$access_token' WHERE userid=$user";
mysql_query($updatetoken) ;
}
else {
$facebook->setAccessToken($access_token);
}
}
else {
$CONDITION = 2;
$message="do not have that user";
$facebook->setExtendedAccessToken();
$access_token = $facebook->getAccessToken();
$updatetoken="INSERT INTO $tbl_name(userid,access_token)VALUES('$user', '$access_token')";
$result = mysql_query($updatetoken);
}
我认为回答你的关键部分是检查 graph_url 部分代码