skip to main |
skip to sidebar
Read Random Post
function uploadPhotos($imagePath, $imageName, $countdowntype) {
try {
//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook = $_SESSION['facebook'];
$facebook->setFileUploadSupport('http://localhost/WCSERVER/index.php');
$response = $facebook->api(
'/me/photos/',
'post',
array(
'message' => '4 years, 6 months, 11 days, 18 hours, 56 minutes, 3 seconds until our '.$countdowntype,
'name' => 'Wedding Countdown Photos',
'source' => '@'.$imagePath.$imageName // @-sign must be the first character
)
);
} catch (FacebookApiException $e) {
error_log('Could not post image to Facebook.');
}
}
function validateFacebookUser() {
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'id here',
'secret' => 'secret here',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
}
function addImageToAlbum($image_path, $message, $album_name) {
$app_id = "id here";
$app_secret = "secret here";
$post_login_url = "http://localhost/WCSERVER/index.php";
$album_name = $album_name;
$album_description = $message;
$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'];
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
//echo $graph_url.'&message='.$message.'&source='.$image_path;
//file_get_contents($graph_url.'?message='.$message.'&source='.$image_path);
echo '<html><body>';
echo '<form target="_top" id="uploadPhotoForm" enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo '<input name="source" type="file" value="'.$image_path.'"><br/><br/>';
echo '<input name="message" type="text"
value="'.$message.'"><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</body></html>';
echo '<script type="text/javascript"> document.getElementById("uploadPhotoForm").submit();
</script>';
}