I have an event on facebook I would like to rsvp attending to. I would like to rsvp with a link, all the examples I have found for rsvping were done with forms. I have the rsvp_event permission and an active access_token. The actual problem I am facing is redirecting after a successful http post.
<?php
$redirect_uri = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$access_token = "ACCESS_TOKEN";
$eid = "EVENT_ID";
$rsvp_to_event = "https://graph.facebook.com/$eid/attending?method=post&access_token=$access_token&redirect_uri=$redirect_uri";
?>
Join
Clicking the 'Join' link will rsvp to the event, but leaves me on a page displaying true. Any ideas to get the page to redirect after a successful rsvp?
Thank you.
You have to query that URL using cURL or file_get_contents(), here's an example from my tutorial:
<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URI";
$event_id = "EVENT_ID";
$rsvp_status = "";
$code = $_REQUEST["code"];
if(empty($code)) {
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=rsvp_event";
echo("<script>top.location.href='" . $auth_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
if( isset($_POST['rsvp']) ) {
// Form submitted, call the Graph API to RSVP to the event
$event_rsvp = "https://graph.facebook.com/$event_id/{$_POST['rsvp']}?method=post&" . $access_token;
$rsvped = json_decode(file_get_contents($event_rsvp));
if($rsvped) {
$msg = "Your RSVP status is now <strong>{$_POST['rsvp']}</strong>";
$rsvp_status = $_POST['rsvp'];
}
}
if( !$rsvp_status ) {
$query = "SELECT rsvp_status FROM event_member WHERE eid=$event_id AND uid=me()";
$fql_url = "https://api.facebook.com/method/fql.query?"
. "query=" . urlencode($query)
. "&format=json"
. "&" . $access_token;
$fql_resp = json_decode(file_get_contents($fql_url));
$rsvp_status = $fql_resp[0]->rsvp_status;
}
?>
<!doctype html>
<html>
<head>
<title>Create An Event</title>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
#msg {border: 1px solid #000; padding: 5px; color: red;}
</style>
</head>
<body>
<?php if( isset($msg) ) { ?>
<p id="msg"><?php echo $msg; ?></p>
<?php } ?>
<form action="" method="post">
<p>
<label for="privacy_type">RSVP:</label>
<input type="radio" name="rsvp" value="attending" <?php if($rsvp_status==="attending") echo "checked='checked'"; ?>/>Attending
<input type="radio" name="rsvp" value="maybe" <?php if($rsvp_status==="maybe" || $rsvp_status==="unsure") echo "checked='checked'"; ?>/>Maybe
<input type="radio" name="rsvp" value="declined" <?php if($rsvp_status==="declined") echo "checked='checked'"; ?>/>Not Attending
</p>
<p><input type="submit" value="RSVP to this event" /></p>
</form>
</body>
</html>
You should be looking at:
if( isset($_POST['rsvp']) ) {
// Form submitted, call the Graph API to RSVP to the event
$event_rsvp = "https://graph.facebook.com/$event_id/{$_POST['rsvp']}?method=post&" . $access_token;
$rsvped = json_decode(file_get_contents($event_rsvp));
if($rsvped) {
$msg = "Your RSVP status is now <strong>{$_POST['rsvp']}</strong>";
$rsvp_status = $_POST['rsvp'];
}
}
The above tutorial consist of HTML form where the user can choose his status.
Most graph api endpoints are not designed to be called directly with your user's browser but to call them from your backend code (authentication related ones could be exception).
Try using the offical php sdk for sending those requests from your php page, or just use the curl extension or file_get_contents on these endpoints from your php script and redirect the user yourself.
Related
I have a PHP form (index.php) that uses the Facebook Graph API to create an event. A form is used to capture user input, and this data is POSTed back to the API to create the event.
This is the same code as explained in this tutorial: http://www.masteringapi.com/tutorials/how-to-create-facebook-events-using-graph-api/49/
The result of this is that, it returns the ID of the event that just got created.
<?php
$app_id = "APP_key";
$app_secret = "APP_secret";
$my_url = "URL";
$code = $_REQUEST["code"];
if(empty($code)) {
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=create_event";
echo("<script>top.location.href='" . $auth_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$event_url = "https://graph.facebook.com/me/events?" . $access_token;
?>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
</style>
</head>
<body>
<div id="inputForm">
<form enctype="multipart/form-data" action="<?php echo $event_url; ?>" method="post">
<p><label for="name">Event Name</label><input type="text" name="name" value="" /></p>
<p><label for="description">Event Description</label><textarea name="description"></textarea></p>
<p><label for="location">Location</label><input type="text" name="location" value="" /></p>
<p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
<p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m") , date("d")+1, date("Y"))); ?>" /></p>
<p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
<p>
<label for="privacy_type">Privacy</label>
<input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open
<input type="radio" name="privacy_type" value="CLOSED" />Closed
<input type="radio" name="privacy_type" value="SECRET" />Secret
</p>
<p><input type="submit" value="Create Event" /></p>
</form>
</div>
<?php
This code works great, but I'm trying to modify this code such that, index.php posts data to $event_url to create the event, but redirects you to a confirmation page that displays a message such as:
"Event successfully created! Click here to access your event", and clicking "here" would redirect you the event on Facebook.
Any thoughts on how this could be done?
Change $event_url to a page like event_submitter.php on your site and add the access_token as a hidden field on your form.
The event_submitter page should take the $_POST data, strip out the access_token (You could eliminate this part if you used the php SDK), reformat the rest of the $_POST data, send this to to the Facebook API via cURL and examine the response.
If you get the correct response, you can then generate the output you want from that.
I use my recently develop fb app to post on my wall and its shows the following error
Uncaught OAuthException: (#100) picture URL is not properly formatted thrown in
you can also see this directly *https://apps.facebook.com/hack-proof_pages/1gp.html
using two files "1gp.html" and "gp1.php"
1gp.html code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="gp1.php">
<p>message
<textarea style="width:300px; height:50px;" name="message1"></textarea>
</p>
<p>link
<input type="text" style="width:300px;" name="link1" />
</p>
<p>
Picture
<input type="text" name="picture1" />
</p>
<p>
name
<input type="text" style="width:300px;" name="name1" />
</p>
<p>
Caption
<input type="text" style="width:300px;" name="caption1" />
</p>
<p>Description
<textarea style="width:300px; height:50px;" name="description1"></textarea>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>
gp1.php code
<?php
require_once 'library/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => '149865361795547',
'secret' => 'shhhh seceret :)',
'cookie' => true,
));
$app_id = '149865361795547';
$canvas_page = "https://apps.facebook.com/hack-proof_pages/gp1.php";
//get data for post
$message1 = $_POST['message1'];
$picture1 = $_POST['picture1'];
$name1 = $_POST['name1'];
$link1 = $_POST['link1'];
$caption1 = $_POST['caption1'];
$description1 = $_POST['description1'];
// compile the post for for user
$WallPost = array(
'message' => $message1,
'link' => $link1,
'picture' => $picture1,
'name' => $name1,
'caption' => $caption1); // you can also use 'picture', 'link', 'name', 'caption', 'description', 'source'....
//http://developers.facebook.com/docs/reference/api/
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . ("&scope=email,read_stream,publish_stream,offline_access,publish_actions,manage_pages,user_groups&response_type=token");
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
//getting the userid and some other data for verification
//get the user id
$UserId = $data["user_id"];
echo 'UserId;' . $UserId;
//get the user access token
$atoken = $facebook->getAccessToken();
echo "</br>" . 'User Access_Token:' . $atoken;
//set default access token and profile
//$facebook->setAccessToken($atoken);
//$user_profile = $facebook->api('/me');
//get the user name and email
$user_id = $facebook->getUser();
$user_profile = $facebook->api('/me','GET');
$user_name = $user_profile['name'];
echo "Name: " . $user_name;
$user_email = $user_profile['email'];
echo "email: " . $user_email;
// post to user wall
$response = $facebook->api('/me' . '/feed','POST',$WallPost);
//posting to groups wall with sleeping time support poster.xls
}
?>
*Note: my app use self signed certificate SSL so that if you want to test this above URL you need to allow my site and store its certificate and one more info that sometimes google chrome shows error due to google chrome one weak point that chrome needs to store self signed certificate in internet explorer means if you want to check this in chrome you need to first open this site in internet explorer and allow my site self signed certificate and store permanently so that its also work in chrome
Please check your picture url, you need to give the full url of the photo not the relative url and facebook should be able to access the picture url, i.e. it should not be of your locally hosted application, try placing a fully qualified sample url of any picture in ur code and check.
I've been knocking my head against a wall for two weeks now, poring over the net, poring over stackoverflow, and repeatedly testing and failing to get a script together to post an event to a Fan Page. I've finally gotten a script that effectively creates the event, but it doesn't appear on the page.
After DMCS pointed me along to the PAGE access tokens, I fiddled about to generate them. Now, the problem is that the event appears on my personal wall, not the page wall I'm targeting. Can anyone see what I'm missing?
Here's the script after the latest edits:
<?php
$app_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://xxxxxxxxxxxxxxxxxxxxxx.com/testfiles/fbeventform.php";
//Going to get the PAGE access code
//First to get USER Access Code
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'] . "&scope=create_event&scope=manage_pages";
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$access_token = #file_get_contents($token_url);
$params = null;
parse_str($access_token, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
echo '<hr />' . $access_token;
//Now, getting the PAGE Access token, using the user access token
$page_token_url = "https://graph.facebook.com/" . $page_id . "?fields=access_token&" . $access_token;
$response = file_get_contents($page_token_url);
// Parse the return value and get the Page access token
$resp_obj = json_decode($response,true);
$page_access_token = $resp_obj['access_token'];
echo '<hr />' . $page_access_token;
//Post the event--here's the form function
if( !empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time'])) ) {
$msg = "Please check your inputs!";
} elseif(!empty($_POST)) {
$url = "https://graph.facebook.com/" . $page_id . "/events?" . $access_token;
$params = array();
// Prepare Event fields
foreach($_POST as $key=>$value)
if(strlen($value))
$params[$key] = $value;
// Check if we have an image
if( isset($_FILES) && !empty($_FILES['picture']['name']) ) {
$uploaddir = './upload/';
$uploadfile = $uploaddir . basename($_FILES['picture']['name']);
if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) {
$params['picture'] = "#" . realpath($uploadfile);
}
}
// Start the Graph API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
$decoded = json_decode($result, true);
curl_close($ch);
if(is_array($decoded) && isset($decoded['id'])) {
// Event created successfully, now we can
// a) save event id to DB AND/OR
// b) show success message AND/OR
// c) optionally, delete image from our server (if any)
$msg = "Event created successfully: {$decoded['id']}";
}
}
?>
<form enctype="multipart/form-data" action="" method="post">
<p><label for="name">Event Name</label><input type="text" name="name" value="a" /></p>
<p><label for="description">Event Description</label><textarea name="description"></textarea></p>
<p><label for="location">Location</label><input type="text" name="location" value="" /></p>
<p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
<p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m") , date("d")+1, date("Y"))); ?>" /></p>
<p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
<p>
<label for="privacy_type">Privacy</label>
<input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open
<input type="radio" name="privacy_type" value="CLOSED" />Closed
<input type="radio" name="privacy_type" value="SECRET" />Secret
</p>
<p><input type="submit" value="Create Event" /></p>
</form>
</body>
</html>
This effectively creates the event, but it doesn't appear on the page wall I'm targetting. I'm really at the end of my rope over this...
Any and all help is appreciated, and I will be happy to post final results for future users!
Debug the access token you're using at https://developers.facebook.com/tools/lint and ensure you have a PAGE access token and not a USER access token.
For getting a page access token, see "Page Login" part of https://developers.facebook.com/docs/authentication/
I'm getting tantalizingly close to completing this script. The goal is to Post an event to one of my fan page walls. The script is effectively grabbing the correct PAGE Access Token, and is effectively posting the event, but it's on my personal wall, not the page wall I'm trying to get it to.
What am I missing? It seems to be all there, but clearly something is inside out or backwards.
Any and all help is appreciated! Here's the code:
<?php
$app_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://xxxxxxxxxxxxxxxxxxxxxx.com/testfiles/fbeventform.php";
//Going to get the PAGE access code
//First to get USER Access Code
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'] . "&scope=create_event&scope=manage_pages";
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$access_token = #file_get_contents($token_url);
$params = null;
parse_str($access_token, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
echo '<hr />' . $access_token;
//Now, getting the PAGE Access token, using the user access token
$page_token_url = "https://graph.facebook.com/" . $page_id . "?fields=access_token&" . $access_token;
$response = file_get_contents($page_token_url);
// Parse the return value and get the Page access token
$resp_obj = json_decode($response,true);
$page_access_token = $resp_obj['access_token'];
echo '<hr />' . $page_access_token;
//Post the event--here's the form function
if( !empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time'])) ) {
$msg = "Please check your inputs!";
} elseif(!empty($_POST)) {
$url = "https://graph.facebook.com/" . $page_id . "/events?" . $access_token;
$params = array();
// Prepare Event fields
foreach($_POST as $key=>$value)
if(strlen($value))
$params[$key] = $value;
// Check if we have an image
if( isset($_FILES) && !empty($_FILES['picture']['name']) ) {
$uploaddir = './upload/';
$uploadfile = $uploaddir . basename($_FILES['picture']['name']);
if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) {
$params['picture'] = "#" . realpath($uploadfile);
}
}
// Start the Graph API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
$decoded = json_decode($result, true);
curl_close($ch);
if(is_array($decoded) && isset($decoded['id'])) {
// Event created successfully, now we can
// a) save event id to DB AND/OR
// b) show success message AND/OR
// c) optionally, delete image from our server (if any)
$msg = "Event created successfully: {$decoded['id']}";
}
}
?>
<form enctype="multipart/form-data" action="" method="post">
<p><label for="name">Event Name</label><input type="text" name="name" value="a" /></p>
<p><label for="description">Event Description</label><textarea name="description"></textarea></p>
<p><label for="location">Location</label><input type="text" name="location" value="" /></p>
<p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
<p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m") , date("d")+1, date("Y"))); ?>" /></p>
<p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
<p>
<label for="privacy_type">Privacy</label>
<input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open
<input type="radio" name="privacy_type" value="CLOSED" />Closed
<input type="radio" name="privacy_type" value="SECRET" />Secret
</p>
<p><input type="submit" value="Create Event" /></p>
</form>
</body>
</html>
On your $url = "https://graph.facebook.com/" . $page_id . "/events?" . $access_token; you're missing the query string name for the access token
Try this
$url = "https://graph.facebook.com/" . $page_id . "/events?access_token=" . $access_token;
I would like to ask for help. I got this homework to create a class in PHP with search method which returns 50 pictures from Flickr, but without use of official PHP Flickr libraries... Anyway with help of google I wrote following code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
class Flickr {
private $apiKey = 'aba429532a6606f2ee35e3f47a300716';
public function search($query = null) {
$search = 'http://flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $this->apiKey . '&text=' . urlencode($query) . '&per_page=50&format=php_serial';
$result = file_get_contents($search);
$result = unserialize($result);
return $result;
}
}
?>
<form action="index.php" method="get">
<input type="text" name="text1" /><br />
<input type="submit" name="submit1" value="Send" /><br />
</form>
<?php
if(isset($_GET["submit1"])) {
$Flickr = new Flickr;
$data = $Flickr->search($_GET["text1"]);
foreach($data['photos']['photo'] as $photo) {
echo '<img src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg">';
}
}
?>
</body>
</html>
That code does what I need, but I am not sure whether I can use flickr.photos.search, so I was wondering if there is a way to avoid using flickr.photos.search.
You could just request http://www.flickr.com/search/?q={work_by_witch_you_search}&z=m pictures from here with curl and just parse received page. Then you definitely wount use anything even close to Flickr API.