As of today: January 09, 2013 this code is 100% tested and working!
This code has 2 parts.
- First: You can control what image you want to post like http://sharefavoritebibleverses.com/images/bible_verses.png
- Second: The user will upload image coming from their computer file.
First Part
You can control what image you want to post like http://sharefavoritebibleverses.com/images/bible_verses.png
<?php
$app_id = "YOUR APP ID";
$app_secret = "YOUR APP SECRET";
//Take NOTE: this must include http://
//ex. http://sharefavoritebibleverses.com/fb_connect.php
$post_login_url = "THE PAGE WHERE YOU ARE GOING TO SAVE THIS FILE";
//Take NOTE: this must include http://
$photo_url = "http://sharefavoritebibleverses.com/images/bible_verses.png";
//you can add link to your image caption, but you can't use this tag <a href='http://link.com' >visit my link</a>
$photo_caption = "create another verse at http://sharefavoritebibleverses.com";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if (!$code)
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else
{
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&redirect_uri=" . urlencode( $post_login_url)
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// POST to Graph API endpoint to upload photos
$graph_url= "https://graph.facebook.com/me/photos?"
. "url=" . urlencode($photo_url)
. "&message=" . urlencode($photo_caption)
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
echo file_get_contents($graph_url);
echo '</body></html>';
}
?>
$app_id = "YOUR APP ID";
$app_secret = "YOUR APP SECRET";
//Take NOTE: this must include http://
//ex. http://sharefavoritebibleverses.com/fb_connect.php
$post_login_url = "THE PAGE WHERE YOU ARE GOING TO SAVE THIS FILE";
//Take NOTE: this must include http://
$photo_url = "http://sharefavoritebibleverses.com/images/bible_verses.png";
//you can add link to your image caption, but you can't use this tag <a href='http://link.com' >visit my link</a>
$photo_caption = "create another verse at http://sharefavoritebibleverses.com";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if (!$code)
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else
{
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&redirect_uri=" . urlencode( $post_login_url)
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// POST to Graph API endpoint to upload photos
$graph_url= "https://graph.facebook.com/me/photos?"
. "url=" . urlencode($photo_url)
. "&message=" . urlencode($photo_caption)
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
echo file_get_contents($graph_url);
echo '</body></html>';
}
?>
This will output something like this
{"id":"517833744917123","post_id":"100000713303095_517823648251456"}
you can change this
echo file_get_contents($graph_url);
into this
$ok = file_get_contents($graph_url);
if($ok)
echo'<script language="JavaScript">window.location="index.php";</script>';
so you can direct them to any page you want after they submitted the photo.
or you can do any conditions you want.
**************************************************************************************************
Second Part
The user will upload image coming from their computer file.
<?php
$app_id = "YOUR APP ID";
$app_secret = "YOUR APP SECRET";
//Take NOTE: this must include http://
//ex. http://sharefavoritebibleverses.com/fb_connect.php
$post_login_url = "THE PAGE WHERE YOU ARE GOING TO SAVE THIS FILE";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else
{
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file" ><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message"
type="text" value=""><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
$app_id = "YOUR APP ID";
$app_secret = "YOUR APP SECRET";
//Take NOTE: this must include http://
//ex. http://sharefavoritebibleverses.com/fb_connect.php
$post_login_url = "THE PAGE WHERE YOU ARE GOING TO SAVE THIS FILE";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else
{
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file" ><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message"
type="text" value=""><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
this will output something like this
{
"id": "517839871583123",
"post_id": "100000713303022_517839881583112"
}