Facebook api does not upload image - php

My issue is already discussed here
couldn't open file "" error in Facebook PHP API
and my php script is
//variables we are going to post to facebook
$fbPermissions = 'publish_stream,user_photos'; //Required facebook permissions
$PicLocation = 'fb_cover_images/cover7.jpg';
$msg_body = array(
'message' => 'I liked this pic from '. $homeurl .
' it is perfect for my cover photo.',
'url' => "http://my_site.com/$PicLocation"
);
if ($fbuser){ //user is logged in to facebook, post our image
try {
$uploadPhoto = $facebook->api('/me/photos', 'post', $msg_body );
}
catch (FacebookApiException $e) {
echo $e->getMessage(); //output any error
}
}
else{
$loginUrl = $facebook->getLoginUrl(
array('scope'=>$fbPermissions,'return_url'=>$return_url));
header('Location: ' . $loginUrl);
}
If I set 'url' => "http://my_site.com/$PicLocation" then getting
(#200) Permissions error
and If I set 'url' => "#http://my_site.com/$PicLocation" then getting
couldn't open file "http://my_site.com/fb_cover_images/cover7.jpg"
message though if I hit "http://my_site.com/fb_cover_images/cover7.jpg", I can view image and permission to folder+image is set to 777.
Please guide what I am doing wrong....
I have searched but fail.
I also tried [PHP + Facebook: how to upload photo on the wall? but same rubbish message (#200) Permissions error
Also Upload Photo To Album with Facebook's Graph API
I don't know what the hell is going on with FB API.

Try adding the following line to the top of your code, after setting up Facebook:
$facebook->setFileUploadSupport( true );
Then, change your $msg_body to the following:
$msg_body = array(
'name' => 'I liked this pic from '. $homeurl . ' it is perfect for my cover photo.',
'source' => "http://my_site.com/$PicLocation"
);
The name is the caption for the image, and the source is the URL to the file. You only need to prefix # to the source if you are referencing a local file.

Related

facebook post on page wall - PHP SDK

I'm trying to create a script that post on a facebook page (as an administrator) a status.
This is the script i'm using:
try {
$access_token = (new FacebookRequest( $session, 'GET', '/' . $pageID, array( 'fields' => 'access_token' ) ))
->execute()->getGraphObject()->asArray();
$access_token = $access_token['access_token'];
$page_post = (new FacebookRequest( $session, 'POST', '/'. $pageID .'/feed', array(
'access_token' => $access_token,
'message' => $message,
) ))->execute()->getGraphObject()->asArray();
} catch (FacebookRequestException $e) {
echo 'ERROR! ' . __LINE__ . $e->getMessage();
} catch (Exception $e) {
echo 'ERROR! ' . __LINE__ . $e->getMessage();
}
The script does work, and I see the post on facebook (ignore language):
The problem is that I'm the only one who can see this post. When other users enter the page, they can't see the post, and if I give them the post's url, it says that it doesn't exist.
You need to make your app public, on top of Status&Review tab in app dashboard.
As long as an app is in development mode, everything it “creates” on Facebook is only visible to app admins/developers/testers.
(This does not require to submit your app for review, since you will only be using it yourself. Only if you wanted to ask other users for permissions as well, you’d need to submit those for review.)

How to post to a facebook group wall in offline mode using graph API? (I am a member of group)

I am trying to post a status update on the wall of a group which I am a member of. Here is the code I am using
<?php
require 'facebook-php-sdk/src/facebook.php';
$appId = 'xxxxxxxxxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxx';
$extended_access_token = 'xxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook(array('appId' => $appId, 'secret' => $appSecret));
$msg_body = array(
'message' => 'Good evening',
'type' => 'status',
'access_token' => $extended_access_token,
);
$groups = array(
'Group name' => '1234567',
);
foreach($groups as $group_name => $group_id){
try {
$post_url = "/$id/feed";
$postResult = $facebook->api($post_url, 'post', $msg_body );
print_r($postResult);
} catch (FacebookApiException $e) {
echo $e;
}
}
?>
If I login to fb via browser and hit this page in a new tab, the message is getting posted to the group wall. But If I am not logged into facebook, then if I hit this page, no message is getting posted and I am getting an error message
OAuthException: (#200) The user hasn't authorized the application to perform this action
How can I post to this group via offline mode? Searched a lot for this, could not find any useful info.
you need to have the permissions from the group owner
Change $id to $group_id and you'll be fine.
you ned to get apps this permession : publish_actions,publish_stream,user_groups,

Unable to tag users to a fb photo uploaded via graph api

I just testing something and found out by script not working . It actually upload a pic and then use it pic id to tag some random peoples from the friend lists . My app have user_photos,publish_stream permissions . I can successfully upload the photo but i am getting error on making tags .
Here is The code:
$f1 = $facebook->api('me/friends?limit=19');
$img = $_REQUEST['imgl'];
$access_token = $facebook->getAccessToken();
$args = array(
'message' => $_REQUEST['m_Config']['appTitle'],
'source' => '#' . $img,
'access_token' => $access_token,
);
$photo = $facebook->api ( $user . '/photos', 'post', $args );
foreach($f1['data'] as $fbu){
$tagx = array('tag_uid' => $fbu['id'],'x' => rand(100,350),'y' => rand(100,350));
$ftags[] = $tagx;
}
$args = array (
'tags' => json_encode($ftags),
'access_token' => $access_token,
);
$result = $facebook->api('/' . $photo['id'] . '/tags', 'post', $args);
print_r($result);
Now the Error which , I am getting is Fatal error: Uncaught OAuthException: (#100) Invalid parameter thrown in C:\xampp\htdocs\fb\base_facebook.php on line 1254
My question is where , My code is wrong . WHy I am getting error , I also tried searching but cant get it fixed .
Thanks
You seem to be assigning the tags at random locations for random friends, which is completely wrong.
The user should be selecting where in the photo their friends are - anything else would be against policy and likely to be shut down as spam
As for your code problem, this isn't working because tag_uid isn't a listed parameter name in the API - the parameter to use for specifying which user to tag is to
See the Photo object's Tag connection documentation for more details and some example code.

Facebook Php image in text message

On my page I want post feeds with an image in the text of message and not like a link/url, is possible add to text message something like htmltags or bbcode?
$msg = "<img src=\"urlimg or facebook\" >\nMy text here";
$args = array(
'message' => $mgs,
);
$myfeed = $facebook->api($pageid . '/feed', 'post', $args);
update:
i've found a solution but if i post 2 times in a row they will be groupped in the same box in timeline
$args = array(
'message' => $msg,
'image' => '#'.$path,
'aid' => $album_id,
'access_token' => $token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
exist a setting to stop auto-group that? or there is another way to post it like feed with image?
You can't publish an image in the middle of the text message, Facebook do not allow it.
But you can attach an image to the message, it will appear on the left of the message in this way:
$msg = "My text here";
imgUrl = "http://urltotheimage.com/path/image.jpg";
$args = array(
'message' => $mgs,
'picture' => $imgUrl
);
$myfeed = $facebook->api($pageid . '/feed', 'post', $args);
So I spent 30 seconds searching around the PHP Facebook API, which really is what you should be doing, and found the following example:
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = './mypic.png'; // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'photo_upload'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
// No user, print a link for the user to login
// To upload a photo to a user's wall, we need photo_upload permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
echo 'Please login.';
}
?>
</body>
</html>
Take note of the $config variable values and the $facebook->api() call.

Facebook: publish image to page album with app access token

How do I give a Facebook App permission to post images to a Facebook Page album?
This is the code I use:
$args = array(
'image' => '#' . $img,
'aid' => $album_id,
'no_story' => 1,
'access_token' => $app_access_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
This is the error I get:
Uncaught OAuthException: A user access token is required to request this resource.
Here is the code I use to publish a photo to an album on Facebook. I hope it'll help.
<?php
include '../src/facebook.php';
$config['appId']='app_id';
$config['secret']='API_KEY';
$config['fileUpload'] = true;
$param['redirect_uri']='your_redirect_url';
$param['scope']=',publish_stream,user_photos';
$facebook=new Facebook($config);
$userid=$facebook->getUser();
if($userid){
try{
$args = array(
'image' => '#' . $img,
'message'=>$message,
);
$photo = $facebook->api('/'.$album_id . '/photos', 'post', $args);
}
catch(FacebookApiException $e){
echo $e->getMessage();//Get OAuth Error
}
else{
$loginUrl=$facebook->getLoginUrl($param);
echo "Please <a href='$loginUrl'>Click Here</a> To Login";
}
?>
Learn more Here
Just in case any user stumbles upon this post , to resolve this error
1> $facebook->setaccesstoken() to facebook page's access token . The facebook page access token is available by issuing $facebook->api('/user_id/accounts'), here user id is the id of page's admin .
2> After setting access token you can use the facebook->api to post the photo to album using album id
$facebook->api('album_id/photos', 'post', array( 'source' => '#'.$photo ));

Categories