new to php please forgive my silly questions.
I am creating my first fb app. It allows user to browse through their local drive and select a photo. Once it is submit, it will redirect to the next page and process to the storing onto my server first then posting to user's wall.
The application is not working really that much. The part where user browse and app storing the photo on to my server is working, but it fails to grab the photo back from my server and post it on the user's wall.
config.php:
<?php
require_once 'facebook.php';
$app_id = "";
$app_key = "";
$app_secret = "";
$canvas_url = "";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$session = $faceb开发者_Python百科ook->getSession();
if (!$session) {
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'publish_stream, user_photos, read_stream, read_friendlists'
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
}//end if session user
else
{
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$updated = date("l, F j, Y", strtotime($me['updated_time']));
echo "Hello " . $me['name'] . "<br />";
echo "You last updated your profile on " . $updated . "<br />" ;
echo "<img src='https://graph.facebook.com/".$uid."/picture'/>";
}//end try getUser
catch (FacebookApiException $e) {
echo "Error:" . print_r($e, true);
}//end catch getUser
}//end else user
index.php contains the form:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
uploader.php run the process
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded" . "<br />";
} else{
echo "There was an error uploading the file, please try again!" . "<br />";
}
try {
$post_id = $facebook->api("/".$uid."/feed", "post", array("picture"=>$target_path));
if(isset($post_id))
echo "A new post to your wall has been posted with id: $post_id";
} catch (FacebookApiException $e) {
error_log($e);
}
I have been trying many different ways which i could find online but it does not work. i have tried adding $facebook->setFileUploadSupport(true); but receive errors.
Please advice me how i could go about getting to upload the photo onto the user wall. Thank you very much
Hello bro this code works for me exactly. what this code you do, it will post the post into your album nor in application album.
if(isset($_POST['upload'])) { if ( isset($_FILES["file"]) && $_FILES["file"]["error"]==0 ) { $file='images/'.$_FILES["file"]['name']; if( move_uploaded_file($_FILES["file"]["tmp_name"],$file)) { $facebook->setFileUploadSupport(true); $post_data = array( 'name'=>$_POST['album'], 'description'=>$_POST['album'] ); $data['album'] = $facebook->api("/me/albums", 'post', $post_data); //$file = $file_name; $post_data = array( "message" => $_POST['message'], "source" => '@' . realpath($file) ); $album_id = $data['album']['id']; $data['photo'] = $facebook->api("/$album_id/photos", 'post', $post_data); } } /**/ }
When you upload any picture from your application, facebook creates an album into your profile named as your application. But this code will post the picture into your album.
$_POST['album']is the album name which I enter in a textfield. Then I just post the form and uploads photo. I hope this will help you
I think this should work:
$target_folder = "uploads/";
$target_path = $target_folder . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded" . "<br />";
$file_path = $target_folder . $_FILES['uploadedfile']['name'];
$arr = array();
$arr["image"] = '@' . realpath($file_path);
try {
$post_id = $facebook->api("/".$uid."/feed", "post", $arr);
if(isset($post_id))
echo "A new post to your wall has been posted with id: $post_id";
} catch (FacebookApiException $e) {
error_log($e);
}
} else{
echo "There was an error uploading the file, please try again!" . "<br />";
}
Replace getSession()
with getUser()
because old PHP versions do not identify the getSession()
function.
$session = $facebook->getSession();
use getUser();
Use This Code and It Will Work Fine For You as Facebook Documentation Here How-To: Use the Graph API to Upload Photos to a user’s profile Says
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_POST-LOGIN_URL";
$album_name = 'YOUR_ALBUM_NAME';
$album_description = 'YOUR_ALBUM_DESCRIPTION';
$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 '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
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>';
}
?>
Example Response
{
"id": "1001207389476"
}
精彩评论