cannot share facebook video link from php sdk - php

This is a rather strange issue..
I am not able to publish a facebook video link on my wall.
The following code works perfectly fine...
$attachment = array(
'link' => "www.google.com"
);
$publish = $facebook->api('/me/feed', 'post', $attachment);
But the following code doesnt
$attachment = array(
'link' => "http://www.facebook.com/video/video.php?v=10150238694155484"
);
$publish = $facebook->api('/me/feed', 'post', $attachment);
It doesn't post anything to my wall.
Has anyone seen the same issue?

As far as I remember, facebook made the picture parameter required for video links

Add it into try catch
try{
$attachment = array(
'link' => "http://www.facebook.com/video/video.php?v=10150238694155484"
);
$publish = $facebook->api('/me/feed', 'post', $attachment);
}
catch (Exception $e){
echo "<pre>";
print_r($e);
echo "</pre>";
}
it will show you all error details needed to resolve your problem

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.)

PHP - Facebook API will not upload pictures

I am trying to write my own auto poster for a Facebook Page or 2, and it will not upload photos to the page.
Code:
<?php
require_once("./src/facebook.php");
require("functions.php");
$f = getFB();
$config = array(
"appId" => $f['appID'],
"secret" => $f['secret'],
"fileUpload" => true,
"allowSignedRequest" => false
);
$fb = new Facebook($config);
$uid = $fb -> getUser();
$param = array(
"access_token" => $f['accessToken'],
"message" => $_GET['msg'],
"url" => "http://i.imgur.com/lHkOsiH.png",
);
try
{
$ret = $fb -> api('/me/feed', 'POST', $param);
echo "Successfully posted!";
print_r($ret);
}
catch (Exception $e)
{
die($e -> getMessage());
}
?>
It will upload just fine, but the result looks like this
How can I get the API to actually upload an image?
Simple error you used /me/feed which targets the user feed, to target the user's photos use me/photos instead, so the final API call would look like
$fb->api('/me/photos', 'POST', $param);

How to post on group wall of facebook

I want to post on group wall. I have permission of "user_groups" and "publish_stream". and also access_token.
and here is my code:
try{
$statusUpdate = $this->facebook->api('/'.$group_id.'/feed', 'post',
array(
'name' => stripslashes($productname),
//'caption'=>$caption,
'message' => $message,
'description' => $description,
'picture' => $picture,
'link'=>$link));
$postid = $statusUpdate['id']; // return id
} catch (Exception $e){
$postid = 0;
}
return $postid; // return id
}
When I run this code I get a return id which was the page id. but nothing post on my group wall. how to solve this?
This code works for me to post to a facebook page.
Get FB page access token ($value['access_token']) and its page id ($value['id'])
Populate the $params array
use the FB API to post your message 3.
This is a snippet
foreach ($pages as $key => $value) {
$params = array(
'access_token' => $value['access_token'],
'message' => $message
);
// ask facebook api to post the message to the selected page
$facebook->api()->api( "/" . $value['id'] . "/feed", 'POST', $params );
}

Batch wall post to multiple Facebook accounts using php

I am trying to post status messages to multiple FB users but not while they are on my site. I have publish_stream perms and a 60 day access token for each user I am posting to, but I can't figure out how to batch this correctly.
Is this the correct way to put in the access token?
$body = array(
'message' => $message,
'link' => $link,
'picture' => $picture,
'name' => $name,
'description'=> $description
);
$batchPost=array();
$i=1;
foreach ($user_fb_id_array as $fb_id) {
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/" . $fb_id['user_fb_id'] . "/feed?access_token=" . $fb_id['user_fb_auth_code'], // Will this work???
'body' => http_build_query($body) );
if($i++ == 50) {
try {
$multiPostResponse = $this->facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');
} catch(FacebookApiException $e) {
error_log($e);
echo("Batch Post Failed");
}
unset($batchPost);
$i=1;
}
}
if(isset($batchPost) && count($batchPost) > 0 ) {
try{
$multiPostResponse = $this->facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');
} catch(FacebookApiException $e){
error_log($e);
echo("Batch Post Failed");
}
}
To give credit where credit is due, this code was modified from the 25labs.com original code.
The access token should be defined for each call at the same level as 'method' and 'relative_url' but your method there might work too.
Are you actually getting an error message or are you just asking how to implement this?

How can I upload photos to album using Facebook Graph API

Here is the code:
$file= 'bbbb.jpg';
$data = array(
basename($file) => "#".realpath($file),
"caption" => "Uploaded using graph api",
"aid" => '13595',
"access_token" => $accessToken,
'method' => 'photos.upload'
);
$sds =$facebook->api($data);
This is the error
Uncaught CurlException: 26: failed creating formpost data
What to do?
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session for the current user.
1 - Default Application Album of Current User
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Target Album
This example will upload the photo to a specific album.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
3 - Target Album with Access Token
This example will upload a photo to a specific album which requires an access token.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
your $data array should have "message" instead of "caption",
also, remove "aid", "method", and "access_token"
your $data has to have the file data and "message", that is it.
$sds =$facebook->api('/me/13595/photos', 'POST', $data);
where instead of 13595 just use the variable with the album aid
also, if needed, access_token is best appended to api uri like this:
$sds =$facebook->api('/me/13595/photos?access_token='.$access_token, 'POST', $data);
also, if the php sdk doesn't work for you, I have successfully used cURL instead if your php installation supports it. in that case see cURL example at Upload Photo To Album with Facebook's Graph API
The latest version of the Facebook PHP SDK wont work with the above examples without the following update to the code.
class Facebook {
...
*Line #539*
protected function makeRequest($url, $params, $ch=null) {
if (!$ch) {
$ch = curl_init();
}
if( isset($params['doMultiPart']) ) {
$doMultiPart= true;
unset($params['doMultiPart']);
} else {
$doMultiPart= false;
}
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = $doMultiPart ? $params : http_build_query($params, null, '&');
...
Basically the problem is that the PHP SDK uses "curl_setopt_array" which if you pass it a url encoded string as the option value it will pass the data as application/x-www-form-urlencoded when what you really want is multipart/form-data; to do this we simply switch to passing in the array of options if we have a param of doMultiPart in the params array.
This was a quick hack I put together to get something working, probably need to review the code to make sure it doesnt break anything else you are doing. Otherwise enjoy.

Categories