Facebook PHP SDK: Post attached photo to fanpage - facebook

in my working code to post a message with url-based image, i have this setup:
$post_data = array(
'message' => 'nice kitten pic',
'url' => 'http://www.eastcottvets.co.uk/uploads/Animals/gingerkitten.jpg'
);
$request = new FacebookRequest($app, $access_token, 'POST', '/' . $page_id . '/photos', $post_data);
i tryed this, which does not work:
$post_data = array(
'message' => 'nice kitten pic',
'source' => realpath('pics/kitten.jpg')
);
But now i like to post an image, not with an url reference, but with a reference to the path of the image on my localhost.
Facebook Docu says:
"There are two separate ways of publishing photos to Facebook:
1: Attach the photo as multipart/form-data. The name of the object doesn't matter, but historically people have used source as the parameter name for the photo. How this works depends on the SDK you happen to be using to do the post.
2: Use a photo that is already on the internet by publishing using the url parameter:"
So, way Nr.2 works, but how do i use way nr.1, with "attach the photo as multipart/form-data" ?? - the docu laks an example for php sdk and in general.

the docu laks an example for php sdk and in general.
No, it doesn’t – you’ve just missed it: https://developers.facebook.com/docs/php/howto/example_upload_photo
The part most relevant to your question is the use of the fileToUpload to prepare a local file for upload.

Solution, for people wo might search aswell:
$post_data = array(
'message' => 'nice kitten pic',
'source' => $fb->fileToUpload('/var/www/myPublicToTheWebFolder/kitten.jpg'),
);
of course the path will be different, depending on server setup. but it has to be the full path you have to output there.

Related

How I can attach multiple image to facebook feed message?

Right now I use this method to add feed message:
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => $this->cafe->getUrlForPost(),
'name' => $this->cafe->getTextForPost(),
'picture' => 'http://' . $_SERVER['HTTP_HOST'] . $this->getImageForPost()
)
))->execute()->getGraphObject();
Is there any way to add a multiple image in picture field? Or maybe do you have a workaround for this?
Thank you for your answers.
Unfortunately, right now you can´t add more than one image in one API call. You can only do multiple calls and you can use Batch Requests for it: https://developers.facebook.com/docs/graph-api/making-multiple-requests

Upload video as unlisted using API v2

I tried to upload video using API v2. It works but i'd like to set the privacy status of the video as Unlisted. I have the following code to do so:
$unlisted = new Zend_Gdata_App_Extension_Element('yt:accessControl', 'yt', 'http://gdata.youtube.com/schemas/2007', '');
$unlisted->setExtensionAttributes(array(
array('namespaceUri' => '', 'name' => 'action', 'value' => 'list'),
array('namespaceUri' => '', 'name' => 'permission', 'value' => 'denied')));
$video->setExtensionElements(array($unlisted));
This only sets the video as private, not unlisted like i want.
I also tried this but it doesn't work either:
$status = new Google_VideoStatus();
$status->setPrivacyStatus('unlisted');
Any help would be welcome.
PS: The reason I use API v2 is because, correct me if i'm wrong, API v3 doesn't allow browser-based upload. Would be good if someone could clarify that. Cheers.
I believe that "unlisted" is not available with the API V2. According to the API V2 documentation, a private attribute can be added to the video properties to make a video private otherwise it is public. See the paragraph on https://developers.google.com/youtube/2.0/reference#Response_codes_uploading_videos for yt:private.

Facebook API photo upload from URL not working

Trying to upload photos by access token, album_id or user_id and URL but getting an error:
"(#324) Requires upload file"
"OAuthException"
I've checked the token and it's valid. Also upload from file works, but upload from URL does not. Facebook does not seem to want to recognise the "url" parameter. This is strange because I swear the other day when I tested this it worked fine.
Is anyone else having this problem?
My code:
$graph_url = "https://graph.facebook.com/ALBUM_ID/photos?access_token=TOKEN";
$postdata = http_build_query( array('message' => 'hello', 'url' => IMAGE_URL);
$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));
I've also checked, it works in FB's graph API explorer, but does not work when using any REST client tool either.
UPDATE:
I got it to work by switching the order of the array inside http_build_query so that the URL is first (or removing the 'message' parameter). The second element is always ingored.
Can anyone see what I'm doing wrong? Why is the second parameter ignored in this case? While the upload "works" I'd still like the 'message' parameter to work so that the image description can also be loaded.
When you’re uploading an image via URL, you must not use the Content-type application/x-www-form-urlencoded – that’s for real file uploads done from your server, using the source parameter, only.

Facebook create an album graph api

I have written a facebook app which checks if an album exists, if it does not it creates one.
This was all working 2 weeks ago and now I go and check it to show the client and it is no longer working. I was thinking it could have something to do with Facebook and maybe they now require an SSL? I can't find any evidence of this on there website however.
The error I am getting is:
Warning: file_get_contents(https://graph.facebook.com/517578134925015/albums?access_token=AAAD5tUCp808BAIZBPk4eV11mlf9C92velLsDeZAm5mXhKZCkwpM3LNy7ax6BBmhuH4BVZBUN6Iycyt55NoXZAFSts9zHeCFNzT6FLJYucgT2SQG8fOYIP) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /var/www/vhosts/hidden/development/cms/backend/publishAlbum.php on line 59
My current code is:
//If no album ID is set, create a new album.
$graph_url = "https://graph.facebook.com/".$pageId."/albums?" . "access_token=". $pageAccessToken;
$postdata = http_build_query(
array(
'name' => $albumName
)
);
$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));
Any help is appreciated.
Thanks
EDIT:
The code I tried that uses the API.
$params = array(
'name' => 'testtest',
'message' => 'test test'
);
try{
$albumtResult = $facebook->api('/'.$pageId.'/albums', 'post', $params);
}
catch(FacebookApiException $e){
error_log($e);
$albumtResult = null;
}
$albumId = $albumtResult->id;
Here is the caught exception: http://pastebin.com/pxeyRTDn
Well, the reason for this error would be much easier to figure out, if you’d used the PHP SDK to make API calls, instead of doing them “by hand“ using file_get_contents … because using the SDK, you get an exception that should point to the error cause, whereas file_get_contents (by default) just throws a warning when it gets anything else than a 2xx HTTP status code for an answer, and does not let you access the response body (which will most likely contain a human-readable error message as well).
My advice: Use the SDK!
Otherwise, add to the context parameter that you want to see the returned data even if the status code signals an error. (details -> see manual)
After a lot of searching, someone created a bug report on Facebook that was almost identical to my problem.
Since being created it has had a lot of repo's and other people reporting that they are also having the same issue.
The priority on facebook has been changed to high so hopefully it gets fixed soon.
Facebook bug report: https://developers.facebook.com/bugs/376442565760536?browse=search_50361119879b15494037772
Thanks for everyones time.

query Canvas URL using FQL

It seems like http://developers.facebook.com/docs/reference/fql/application/ provides all the information except for Canvas URL. Is it possible to query this information any other way?
Well, the solution I came up with, was simply calling the old admin.getAppProperties method.
$info = $facebook->api(array(
'method' => 'admin.getAppProperties',
'properties' => array('publish_url'),
));