Facebook; posting a link to a fanpage - php

I have a web application that allows a user to publish a small blurb when one of their articles goes live on their site. Right now I am able to post to the user's wall using a session_key I saved in a table but I can't publish the same link on a fan page (I have the rights and IDs of the pages I need).
Essentially I want the functionality of: http://wiki.developers.facebook.com/index.php/Links.post but for a page instead. So far all I can find utilizes the steam.publish function which isn't quite the same thing.
I'm hoping that there's a hidden parameter (like a target_id) in the link.post function that I've missed (since the facebook wiki is horrible).
Any help is appreciated :) If there isn't a function maybe someone could help me use the parameters of stream.publish to have the content of a post look the same?

Well I couldn't find a way to use the link_post() function to post to a fanpage so I decided to shape the stream.publish result in a way that it would look the same. I had to do a lot of dancing around the variables to get the same effect but it worked. In this case I had to grab the "description" metatag, the first image in the page's content, and the page's title.
I hope this can help someone out:
$title = 'Title of the article, or the title of your webpage';
$message = 'Caption that will go with the link, from the user';
$description = 'I put what would have been in the description metatag, which is what the post link seems to grab';
$fb_thumbnail = ''; // a link to the first image in your article
$target_id = 'XXXXXX'; // the id of the fan page you want to post to
$attachment = array( 'name' => $title,
'href' => 'http://'.$url,
'description' => $description,
'media' => array(array('type' => 'image',
'src' => $fb_thumbnail,
'href' => 'http://'.$url))); // I would get an error with the HREF but that's because I wasn't including the "http://" bit in the link
$attachment = json_encode($attachment);
$facebook->api_client->stream_publish($message, $attachment,"",$target_id);

Related

Image not appearing when publishing to stream

I'm writing a PHP app that posts to users' feeds. The posts are showing up in the feeds, but the images I'm specifying aren't.
App is currently in sandbox mode, so not sure if that makes a difference.
Here is the data array I'm passing:
$data = array(
'message' => 'I just donated to ORGANIZATION to help end the needless killing of healthy animals.',
'name' => 'Each day over 10,000 healthy pets are killed. Together we can help change this.',
'caption' => 'petbucks.net',
'link' => 'http://petbucks.net',
'description' => 'Sponsorship of real homeless pets gives them the time they need to find a forever home.',
'picture' => 'http://dev.petbucks.net/img/default-donation.jpg',
'actions' => array(
array(
'name' => 'Visit us',
'link' => 'http://petbucks.net'
)
)
);
Facebook::api('/'.$uid.'/feed', 'post', $data);
Like I said, the post is appearing in the user's feed, but the picture isn't. If I inspect the source of where the URL should be, here is the image tag that appears:
<img class="shareMediaPhoto fbStoryAttachmentImage img" src="https://fbstatic-a.akamaihd.net/rsrc.php/v2/y4/r/-PAXP-deijE.gif" alt="" style="background-image: url(https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=332280520247212&v=1&size=z&cksum=534f379420f4d890f47eebaa5bf15f4a&src=http%3A%2F%2Fdev.petbucks.net%2Fimg%2Fdefault-donation.jpg);">
Based on that, I'd say that it's receiving the picture URL just fine (you can see the real URL in the source of that background image), so it must be something else. Is it because of sandbox mode?
Any idea what's going on would be appreciated!
Update
There was some weird DNS issues going on, so those have been fixed. However, I am now seeing full-size images in my main news feed, but not smaller ones in my own newsfeed.
This is going to be hard to demonstrate without giving out access to my FB account, but as an example, take a look at this post which I've made public:
https://www.facebook.com/colin.viebrock/posts/10153814802000226?stream_ref=10
That image shows correctly for me when I'm logged into FB, but not for you I'm guessing. The URL it is trying to display is:
https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=332280520247212&v=1&size=z&cksum=15a4249f3c830d8818ed66e793ba3156&src=https%3A%2F%2Fpetbucks.org%2Fimg%2Fdefault-donation.jpg
When it does show up, it's referencing this image:
https://fbexternal-a.akamaihd.net/safe_image.php?d=AQD1l81SkouNC200&w=154&h=154&url=https%3A%2F%2Fpetbucks.org%2Fimg%2Fdefault-donation.jpg&cfs=1&upscale

Multi-photo upload to album and wall story

I'm confident about posting multiple photos to a fan page album through a common batch request with PHP, and I also know how to use che "no_story" command to hide the wall posts showing each image just uploaded. What I need to know is if it's possible to show an unique wall post on the fan page showing the whole upload, like the standard Facebook behavior when you upload more pictures using the web interface, and not one post for each photos.
This is the closest I have got:
$attachment = array
(
'access_token'=>$fanPageAccessToken,
'object_id' => $AlbumId,
'message' => $AlbumDesc,
'link' =>$AlbumLink
);
$result = $facebook->api($fanPageId.'/links/','post',$attachment);
}
I get the variables by querying the albums associated with the fanpage:
$fanPageAlbums = $facebook->api($fanPageId . '/albums/');
foreach ($this->fanPageAlbums['data'] as $fanPageAlbum) {
if ($albumId == $fanPageAlbum['id']) {
$albumLink = $fanPageAlbum['link'];
$albumDesc = $fanPageAlbum['description'];
break;
}
}
The key thing is your are posting to the links part of the graph, not the feed.
This works in that it will produce one big picture and three thumbs below it, as you would expect. However, it doesn't appear on the feeds of friends properly.
I've been struggling with this for ages, and the above is the closest I have got to mimicing exactly the facebook behavior. If you get any further please let me know!

Geotargeting Page Posts with the Facebook API

I'm trying to geotarget wall posts to my fan page using the Facebook PHP SDK.
The following snippet successfully posts a message to my wall, but the geotargeting isn't taking.
I'm new to programming and I've done my best to follow the documentation here but it's pretty sparse -- I'm not confident that my syntax is correct.
Any help would be very much appreciated.
//compiling the geotargeting parameters
$geoTarget = "{'cities':'Richmond,VA','regions':'Virginia','countries':'US'}";
//my arguments
$args = array(
'access_token' => $page_access_token,
'message' => "this is my message",
'targeting' => $geoTarget
);
//posts the message
$facebook->api("/$page_id/feed","post",$args);
The field is privacy in the Post table
The description field may contain a comma-separated lists of valid country, city and language if a Page's post targeting by location/language is specified.
http://developers.facebook.com/docs/reference/api/post/
To make this easier this is the bit you need http://developers.facebook.com/docs/reference/api/page/#targeting
this will give the info on how to target regions too.
http://developers.facebook.com/docs/reference/ads-api/get-autocomplete-data/
What you need to complete the targeting in the question would be:
The countries bit was right.
Virginia's region code is 51 (which you can find by searching _">https://graph.facebook.com/search?q=vi&type=adregion&match_country_code=true&country_list=US&access_token=_)
Richmond's city id is 2538983 (which you can find by searching _">https://graph.facebook.com/search?q=richmon&type=adcity&limit=150&access_token=_)
so the geotarget would be
$geoTarget = "{'cities':[2538983],'regions':[51],'countries':'US'}";

Tagging photos on Facebook with Graph API / PHP SDK

The question is pretty much in the title, but to be more specific, I'm working on a facebook contest for a client in which people have to upload photos to take part in it after accepting rules and subscribing, etc.
After going down a long road full of deceptions
(here's what I tried that didn't work in the end:
uploading to the user's album then tagging the page: nope, can't tag a page
uploading to a public album in the page, like "fan photos", or wall photos? can't find anything AT ALL about a way to do this, though it would've been my preferred way.)
So, I ended up having an idea: I would do this in two steps. First, the user subsribes and uploads a photo to my PHP server. Then, another, different application with permissions on the account that has the page, would take control of it, upload all the photos at 5 minutes intervals (meaning, you subsribe, 5 minutes later, your photo's uploaded.)
So far, so good; I don't like the logic behind this, but it's the only way I found! So, I did the base, I'm able to post a photo with a caption as the page in a dedicated app album.
BUT! I can't tag anyone in those photos. Maybe you can't tag as a page, maybe you can't tag in page albums, maybe I don't have the permissions required (I have stream_publish, user_photos, friends_photos, offline_access, and I could add anything if I need to since it's a private-use app anyway). Oh and, the user I'm trying to tag is the admin for both the app and the page (and he likes the page). I don't know, but it's driving me crazy. I hate the official documentation, there's no example code, you have to figure out most of the things, go through trial and error or search on the web for people who did so and shared how to do things. Not nice when you have little time to complete a project for a client without busting the budget.
Anyway, here's my error I keep getting whenever I try to tag someone to a photo from any album:
OAuthException: (#322) Invalid photo tag subject
And here's the code I'm using to upload a photo from the server to facebook.
if ($user) {
try {
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
// First method, at the same time as upload. Upload works but no tags
$args = array(
'access_token' => $page_info['access_token'],
'source' => '#' . realpath($FILE_PATH),
'message' => "test"
'tags' => array(
array(
'tag_uid'=> "MY_USER_ID",
'x' => 0,
'y' => 0
))
);
$post = $facebook->api("/$page_id/photos","post",$args);
$postID = $post['id'];
// Second method I'm trying. No tags.
$tag = $facebook->api("/$postID/tags/MY_USER_ID","post");
print_r($tag);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Any help would be appreciated. Thanks in advance!
EDIT :: Now I got it partly working using the second part in the code, but only if I remove the "access_token" parameter from the photo posting first. Basically, it only works if I upload it to a personal album. But that's as far as it gets from what I want. When I upload it to the album from the page, it says I asked for tag requests, but on my personal account which I tagged, I don't receive anything. Baaaah.
RE-EDIT :: After much tries, it seems I can't tag someone as a page or in a page album. I'll try to find an alternative, I guess.

Wordpress - use comment-system outside of pages and posts

so currently i'm using pods to create some individual pages for a log, filled with custom stuff.
now i want to use the comments-system for each of this pages e.g.:
mydomain.com/podpages/page1
mydomain.com/podpages/page2
mydomain.com/podpages/page3
this are not pages created with wordpress so simply adding <?php comments_template(); ?> is not working.
any ideas how to solve this problem?
thanks in advance
please leave a comment if something is unclear :)
When a comment is stored in the WordPress database, the ID of the post (or page) the comment relates to is also stored.
Trouble is, you're trying to save comments using WordPress, but for a page that it doesn't actually know about.
So, how about we create a WordPress page for each real page, but merely as a representation, so that your real pages and WordPress have a common ground for working with each other.
So, the plan here is to;
Load WordPress in the background on each of the 'real' pages.
See if a WordPress page representation already exists for the 'real' page
If it doesn't, create it, then and there
Trick WordPress into thinking we're actually viewing the representation
Carry on using all of WP's functions and 'template tags' as you would normally
This code should be somewhere at the beginning of the template file used to render your 'real' pages;
include ('../path/to/wp-load.php');
// remove query string from request
$request = preg_replace('#\?.*$#', '', $_SERVER['REQUEST_URI']);
// try and get the page name from the URI
preg_match('#podpages/([a-z0-9_-]+)#', $matches);
if ($matches && isset($matches[1])) {
$pagename = $matches[1];
// try and find the WP representation page
$query = new WP_Query(array('pagename' => $pagename));
if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename
));
if (!$id)
do_something(); // something went wrong
}
// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
}
UPDATE
I can't believe I was this stupid. Below should replace current code inside outer if;
// try and find the WP representation page - post_type IS required
$query = new WP_Query(array('name' => $pagename, 'post_type' => 'page'));
if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename,
'post_author' => 1, // failsafe
'post_content' => 'wp_insert_post needs content to complete'
));
}
// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
// post_type is a must!
wp(array('name' => $pagename, 'post_type' => 'page'));
// set up post
the_post();
P.S I think using the query_var name over pagename is better suited - it queries the slug, rather than the slug 'path'.
You'll also need to either place an input inside the form with name redirect_to and a value of the URL you'd like to redirect to, or, filter the redirect with a function hooked onto comment_post_redirect, returning the correct URL.
add
require('/path/to/wp-blog-header.php');
to include the wp files. this should give you all the functions/data you need.
Can you create pages in wordpress which display your log data? You might need a new template for this. WordPress will then have something to connect the comments to.
Do you need to use WordPress for this? If not, maybe something in this SO question helps: Unobtrusive, self-hosted comments function to put onto existing web pages
Just supply the wordpress-comment-part with a new ID - start with something your usual posts will never reach (100.000+ is your pages i.e.)
I don't know exactly if in wordpress it's a function (saveComment i.e.), but if it is so, just use it in your page with he custom ID.
You will nevertheless have to insert the Comments-form yourself.
And don't forget to modify the query that gets the news-entires that IDs over 100.000 are not entries.
Or you can write your own template that displays the standard-Worpress-stuff with IDs < 100.000, or else your pages.
Summed up, it should not be very difficult.
p.s.: If you just want to use the wordpress-login, then use any comment-system or make your own (it's an 1hour-thing) and authenticate / use the worpress-session.

Categories