I'm trying my best to parse current url of webpage to facebook feed, and title as well, i can't get it to work for one or another reason. I'm using fb.ui js for feed, and here's my code:
<div id='fb-root'></div>
<script src='http://connect.facebook.net/en_US/all.js'></script>
<script>
window.fbAsyncInit = function () {
FB.init({
appId: 24***7415376313, status: true, cookie: true,
xfbml: false, channelUrl: 'http://www.myurl.rs/fb-channel.html'
});
};
function fbShare(title, fbSiteUrl, fbThumbnailUrl, fbSiteDescription) {
var publish = {
method: 'feed',
name: '<?=$vacanytitle?>',
caption: '',
description: fbSiteDescription,
picture: fbThumbnailUrl,
link: '<?=$url?>',
description: 'My current description',
redirect_uri: 'www.myurl.rs',
actions: [
{ name: 'MYurl', link: 'http://www.myurl.rs' }
],
display: 'popup'
};
function callback(response) {
}
FB.ui(publish, callback);
}
</script>
And here's the code i'm using to get current url through php:
function getUrl() {
$url = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$url .= $_SERVER["REQUEST_URI"];
return $url;
}
And my title code:
while ($record = mysql_fetch_array($fetchData)){
$vacancyid= trim($record['vacancyid']);
$vacancytitle= trim($record['vacancytitle']);
Does anyone has a clue how this could be implemented? I'm really running out of options.
Related
I am editing this question so that it makes more sense than what I originally wrote.
I am implementing the code below in order to add a Facebook "Share" button to my website. My problem is that this script calls for actual values for 'link', 'picture', 'name', 'caption', and 'description' directly in the script.
How can I change this code so that it pulls this information from my website automatically. For example, I need it to pull the URL from the current and assign it to 'link'. Another example: instead of providing the URL for the picture, I want to point the code to a certain class.
<script>
FB.init({appId: "YOUR_APP_ID", status: true, cookie: true});
function postToFeed() {
// calling the API ...
var obj = {
method: 'feed',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
</script>
This is from http://developers.facebook.com/docs/reference/dialogs/feed/
window.location.pathname will only return the path portion of a URL, you also need to add your domain also. window.location.href will return this. Pretty easy to change your code to do this:
<script>
FB.init({appId: "YOUR_APP_ID", status: true, cookie: true});
function postToFeed() {
// calling the API ...
var obj = {
method: 'feed',
link: window.location.href,
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
</script>
I have tried putting together some code based on some other SO questions and various web tutorials.
The error I am getting is right at the end when I try and post to my friend's walls.
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<p><input type="button" onclick="sendRequestViaMultiFriendSelector(); return false;" value="Recommend friends for another chance to win!" /></p>
<div id="response"></div>
<script>
FB.init({
appId : 'xxxxxxxxxx',
status : true,
cookie : true,
oauth: true
});
function sendRequestViaMultiFriendSelector() {
FB.ui({method: 'apprequests',
message: 'My Great Request'
}, requestCallback);
}
function requestCallback(response) {
// Handle callback here
if (response.request && response.to) {
var request_ids = [];
for(i=0; i<response.to.length; i++) {
var temp = response.request + '_' + response.to[i];
var friend_id = response.to[i];
request_ids.push(temp);
//send message in here
var opts = {
message : 'Message',
name : 'Name',
link : 'http://www.facebook.com/pages/xxxxx/xxxxxxxxxxxxxxx',
description : 'Description here',
picture : 'http://www.example.com/img/some.jpg'
};
FB.api('/'+friend_id+'/feed', 'post', opts, function(response) {
if (!response || response.error) {
// This is the error I am getting:
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
// end send message
}
var requests = request_ids.join(',');
} else {
alert('Didn't send recommendation to friends');
}
}
</script>
The code is actually working great, it worked as soon as I granted myself the access token in graph api explorer.
Just need to set up my auth dialogue now, lots of fun!
Just thought I'd better answer this incase anyone else runs into the same situation.
I want my users to be able to share dynamic content on their Facebook news feed. There's no other Facebook integration (e.g. Facebook login or other server-side integration) so I want this to be as light as possible.
This is where I got, but how does it looks like? It seems to work, but I'm not sure if I've thought of everything.
<button id="fb-publish">Share to Facebook</button>
<script type="text/javascript">
(function() {
FB.init({
appId: MY_FACEBOOK_APP_ID, cookie: true, status: true, xfbml: true, oauth: true
});
var fbAuth = null;
var fbShare = function() {
FB.ui({
method: "feed",
display: "iframe",
link: "http://example.com/",
caption: "Example.com",
description: "Here is the text I want to share.",
picture: "http://example.com/image.png"
});
};
$("#fb-publish").click(function() {
if (!fbAuth) {
FB.login(function(response) {
if (response.authResponse) {
fbAuth = response.authResponse;
fbShare();
}
}, {scope: 'publish_stream'});
} else {
fbShare();
}
});
})();
</script>
Also, if I want to attach an image from a URL (bigger than just the 50x50 pixel image defined in the picture field), how can I do that with just JavaScript? Or can I?
I decided to leave out authResponse caching and call FB.login() every time. It briefly opens a popup window, but at least there's no case where user has logged out in another tab or window and the cached authResponse has gone stale. The briefly flashing popup window is not that big of an issue, since I don't think users will share the same page multiple times, anyway.
Here's my final code which is even simpler than the original one:
<button id="fb-publish">Share to Facebook</button>
<script type="text/javascript">
(function() {
FB.init({
appId: MY_FACEBOOK_APP_ID, cookie: true, status: true, xfbml: true, oauth: true
});
var fbShare = function() {
FB.ui({
method: "feed",
display: "iframe",
link: "http://example.com/",
caption: "Example.com",
description: "Here is the text I want to share.",
picture: "http://example.com/image.png"
});
};
$("#fb-publish").click(function() {
FB.login(function(response) {
if (response.authResponse) {
fbShare();
}
}, {scope: 'publish_stream'});
});
})();
</script>
And as mentioned by Tolga Arican, if it's ok to you that the sharing dialog opens in a popup window, you don't even need to call FB.login() and ask for the publish_stream permission; just call FB.ui({ method: "feed" }) directly and you're good to go:
<button id="fb-publish">Share to Facebook</button>
<script type="text/javascript">
(function() {
FB.init({
appId: MY_FACEBOOK_APP_ID, cookie: true, status: true, xfbml: true, oauth: true
});
$("#fb-publish").click(function() {
FB.ui({
method: "feed",
link: "http://example.com/",
caption: "Example.com",
description: "Here is the text I want to share.",
picture: "http://example.com/image.png"
});
});
})();
</script>
1) Make sure appId defined.
2) In FB.login , response.authResponse may not work. I'm not pretty sure but just throw console.log, it may response.session
3) in method:feed , you can put larger images for picture, but it wil downsize. There is no way to put big images as a feed thumbnail. It is just a thumb of a feed..
For the lightest method:feed js, you don't need display:iframe i guess.
FB.ui( {
method: 'feed',
name: 'Facebook Dialogs',
link: 'http://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.' }, function(response) {} });
I have a bug while using FB.ui, although I'm doing everything by the book.
I want to a button so users can share the page on their wall.
When I click the button, I get a facebook popup window that says:
An error occurred, Please try again later.
Can you see what's wrong?
Thanks!
Here's my code:
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init
({
appId:'MYAPPID',
status:true,
cookie: true,
xfbml:true
});
function shareProject2()
{
FB.ui(
{
method: 'feed',
name: 'Facebook Dialogs',
link: 'http://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
message: 'Facebook Dialogs are easy!'
},
function(response) {
if (response && response.post_id) {
getPermission();
} else {
alert('Post was not published.');
}
});
return false;
}
</script>
<a href="#" onclick="shareProject2();" >Share</a>
Have you tried passing the session from the server to the client JavaScript
This would be done in php as:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => $appId, // Your App Id
'secret' => $secret, // Your App Secret
'cookie' => true,
));
$session = $facebook->getSession();
?>
<script>
FB.init
({
appId:'MYAPPID',<?php
echo (!empty($session)) ? 'session: ' . json_encode($session) . ',' : '' ?>
status:true,
cookie: true,
xfbml:true
});
...
</script>
This bit of documentation has not been as helpful as you might think. I understand that I have to hang a display parameter on the end of the URL, but I'm not sure how to invoke a login window like that. Here's what I have now:
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<script type="text/javascript">
// initialize the library with the API key
FB.init({ apiKey: '{{ facebook_api_key }}', status: true, cookie: true, xfbml: true});
function facebookConnect(form){
function handleResponse(response){
form.submit();
}
FB.login(handleResponse,{perms:'publish_stream,user_about_me,status_update,email,offline_access'});
}
</script>
This works fine in a desktop browser, but I can't figure out how to get the 'touch' or 'wap' modes for dialogs.
I'm using django-socialregistration if that's of any relevance.
Logging into Facebook using a touch dialog used to work a while ago, but that same old code doesn't seem to work anymore.
However,
You can redirect a user to the login URL, and facebook will do the rest depending on the platform. The following snippet will login the user, and redirect back to your current page if set.
Update: You also need to edit your 'Basic App Settings' > 'Select how your app integrates with Facebook' and check 'Mobile Web' for this to work.
window.location = "http://www.facebook.com/dialog/oauth/"
+ "?" + "scope=" + "publish_stream,user_about_me,status_update,email,offline_access"
+ "&" + "client_id=" + YOUR_APP_ID_GOES_HERE
+ "&" + "redirect_uri=" + YOUR_CURRENT_PAGE_URL
+ "&" + "response_type=token";
The display:touch property can still be used in FB.ui() as follow (for instance, posting on a wall):
// Make sure you call FB.init() before FB.ui()
FB.ui({
app_id: YOUR_APP_ID_GOES_HERE,
method: 'feed',
name: "post title",
link: "http://link-to-what-you-are-sharing/",
picture: "your_image.jpg",
description: "post description",
display: 'touch'
},
function callback(r){
if ( (r && r.id) || (r && r.post_id) ){ alert('posted');}
else{ alert('failed'); }
});
Why are you wrapping the response handler inside a function?
Do something like :
function handleStatusChange(response) {
if (response.authResponse) {
console.log(response);
}
}
window.fbAsyncInit = function() {
FB.init({ appId: 'YOUR_APP_ID',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.statusChange', handleStatusChange);
};
Taken from https://developers.facebook.com/docs/guides/mobile/web/