How to close the pop up of facebook post on wall - facebook

I want to post on wall using fb API, When I post a message, after share, the link opens and popup remains open.
Code is
function writeOnWall()
{
FB.init({
appId:'292656140795961',
xfbml:true
});
FB.ui({
method: 'feed',
name: 'First Application'
});
}

Following simple JavaScript example demonstrates using the FB.ui method in the JavaScript SDK to use the Feed Dialog
FB.init({appId: "YOUR_APP_ID", status: true, cookie: true});
function postToFeed() {
// calling the API ...
var obj = {
method: 'feed',
name: 'First Application'
};
function callback(response) {
alert("Post ID: " + response['post_id']);
}
FB.ui(obj, callback);
}

You can use the following code to post into facebook wall , just call the function with proper arguments
Try this ,
<?php
function doWallPost($postName='',$postMessage='',$postLink='',$postCaption='',$postDescription='')
{
$FB_APP_ID='xxxxxxxxxxxxxxxxxxxxxxxx';
$FB_APP_SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxx';
$APP_RETURN_URL=((substr($_SERVER['SERVER_PROTOCOL'],0,4)=="HTTP")?"http://":"https://").$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$code = $_REQUEST["code"];
if(empty($code))
{
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=".$FB_APP_ID."&redirect_uri=".$APP_RETURN_URL."&scope=publish_stream";
header("Location:$dialog_url");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$FB_APP_ID."&redirect_uri=".urlencode($APP_RETURN_URL)."&client_secret=".$FB_APP_SECRET."&code=".$code;
$access_token = file_get_contents($token_url);
$param1=explode("&",$access_token);
$param2=explode("=",$param1[0]);
$FB_ACCESS_TOKEN=$param2[1];
$url = "https://graph.facebook.com/me/feed";
$attachment = array( 'access_token' => $FB_ACCESS_TOKEN,
'name' => $postName,
'link' => $postLink,
'description' => $postDescription,
'message' => $postMessage,
'caption' => $postCaption,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result=curl_exec($ch);
header('Content-type:text/html');
curl_close($ch);
return $result
}
?>

Related

Upload Video to Facebook Page with call to action in php

I am trying to upload video to facebook page as page admin with the call to action:
// Post Data
$action_call_array = array('type' => 'LEARN_MORE', 'value' => array('link' => 'http://www.google.com'));
$data = [
'title' => 'Demo Title',
'description' => 'Demo Description
http://www.google.com
http://hk.yahoo.com',
'source' => new CURLFile('video/video_1mb.mp4', 'video/mp4'),
'call_to_action' => $action_call_array,
'access_token' => FACEBOOK_PAGE_TOKEN, // MUST BE INCLUDED !!!
];
// Post Url
$post_url = 'https://graph-video.facebook.com/'.FACEBOOK_PAGE_ID.'/videos';
// CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
// result
$j = json_decode($return);
$page_post_id = $j->id;
var_dump($return);
Without the call to action, it works. But after adding the call to action, it returns me the following error:
string(196) "{"error":{"type":"Exception","message":"No Call To Action Type was parseable. Please refer to the call to action api documentation","code":1373054,"is_transient":false,"fbtrace_id":"H9GksxYBdQx"}}"
Looks like the way I add the call to action is not correct. But I don't really know how to fix it.
I put the call to action into url parameter and it is working now!
// Post Data
$fb_action = 'LEARN_MORE';
$fb_action_link = 'http://www.google.com';
$action_call_array = array('type' => $fb_action, 'value' => array('link' => $fb_action_link));
$params = http_build_query(array('call_to_action' => $action_call_array));
$data = [
'title' => 'Demo Title',
'description' => 'Demo Description
http://www.google.com
http://hk.yahoo.com',
'source' => new CURLFile('video/video_1mb.mp4', 'video/mp4'),
'access_token' => FACEBOOK_PAGE_TOKEN, // MUST BE INCLUDED !!!
];
// Post Url
$post_url = 'https://graph-video.facebook.com/'.FACEBOOK_PAGE_ID.'/videos?'.urldecode($params);
// CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
// result
$j = json_decode($return);
$page_post_id = $j->id;

Facebook Graph API POST picture in a specific app album

I have a Facebook App that POST a picture to a specific album created also by my app. If I run the app the second time it will create another album and POST the new picture there. I would like if the user POST a new one, the picture to be placed in the same album and if is not there to be created. Here is my code so far:
$graph_url = "https://graph.facebook.com/me/albums?" . "access_token=". $access_token;
$album_name = 'MyApp Album';
$album_description = 'Album description';
$postdata = http_build_query(
array('name'=> $album_name,
'message'=> $album_description
));
$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));
$album_id = $result->id;
$file = $_POST['photo'];
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = "https://graph.facebook.com/".$album_id."/photos?access_token=" . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
print_r(json_decode($data,true));
I don't wanna use FQL queries if isn't absolutely neccesary. If I can search probably for the album name and then return the ID it would be great.

How to post to my facebook wall from my own adminpages(CMS)?

How can I post a ordinary post to my facebook wall from my administration pages where I upload content to my webpage?
So I upload content to my webpage from my CMS and next to where I display my uploaded content in my adminpages I would like to have a button that can publish that post to my facebook wall. As an ordinary post and not like a LIKE post or Comment post!
First you need to create an facebook app.
Then you will get an app id and a secret key.
Using this details you can do post into ur wall using facebook php library
or u can use the following function
<?php
function doWallPost($postName='',$postMessage='',$postLink='',$postCaption='',$postDescription='')
{
$FB_APP_ID='xxxxxxxxxxxxxxxxxxxxxxxx';
$FB_APP_SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxx';
$APP_RETURN_URL=((substr($_SERVER['SERVER_PROTOCOL'],0,4)=="HTTP")?"http://":"https://").$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$code = $_REQUEST["code"];
if(empty($code))
{
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=".$FB_APP_ID."&redirect_uri=".$APP_RETURN_URL."&scope=publish_stream";
header("Location:$dialog_url");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$FB_APP_ID."&redirect_uri=".urlencode($APP_RETURN_URL)."&client_secret=".$FB_APP_SECRET."&code=".$code;
$access_token = file_get_contents($token_url);
$param1=explode("&",$access_token);
$param2=explode("=",$param1[0]);
$FB_ACCESS_TOKEN=$param2[1];
$url = "https://graph.facebook.com/me/feed";
$attachment = array( 'access_token' => $FB_ACCESS_TOKEN,
'name' => $postName,
'link' => $postLink,
'description' => $postDescription,
'message' => $postMessage,
'caption' => $postCaption,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result=curl_exec($ch);
header('Content-type:text/html');
curl_close($ch);
return $result
}
?>
For details
follow How to post wall in facebook using API in PHP?
function postonwall(){
// showLoader(true);
FB.api('/me/feed', 'post',
{
message : "testtext.",
link : 'http://www.mydomain.se',
picture : 'http://www.mydomain.se/image.jpg',
name : 'iOS Apps & Games',
description : 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'
},
function(response) {
// showLoader(false);
if (!response || response.error) {
alert('Error occured');
} else {
//alert('Post ID: ' + response.id);
alert('Success: Content Published');
}
});
}

Post on a wall using Facebook graph

I am trying to post on a wall using an http post request with the given url but I'm getting a method not implemented error. What am I doing wrong?
Assuming the user has already authorized my app and I have an access token with publish_stream permissions, is it possible to create a URL that will post to the users wall using the facebook graph?
Here is the url I am using where [userid] is the user id and [access_Token] is the access token:
https://graph.facebook.com/[userid]/feed?message=I like Cheesy Poofs&picture=http://simplyrecipes.com/photos/cheddar-cheese-puffs-b.jpg&link=alink.com&name=Cheesy Poofs Rule!&caption=Some awesome caption&description=cool description bruh&access_token=[access_token]
Edit
In the link above I was missing "method=post". I now get an ID back from the following URL.
https://graph.facebook.com/[userid]/feed?method=post&message=I like Cheesy Poofs&picture=http://simplyrecipes.com/photos/cheddar-cheese-puffs-b.jpg&link=alink.com&name=Cheesy Poofs Rule!&caption=Some awesome caption&description=cool description bruh&access_token=[access_token]
You should be using post, like:
$update = $facebook->api('/[userid]/feed', 'post',
array('message'=> 'your message here',
'picture' => 'your picture link',
'link' => 'your link here'));
Sample using php icm signed request:
<?php
$signedRequestObject = parse_signed_request( $_POST["signed_request"],YOUR_APPLICATION_SECRET );
if ($signedRequestObject["oauth_token"]){
// there is no token, something went wrong
exit;
}
$token = $signedRequestObject["oauth_token"];
$data = array(
"message" => "happy joy joy message",
"link" => "www.myjoyfullsite.com",
"access_token" => $token,
"picture" => "www.myjoyfullsite.com/avatar.jpg",
"name"=> "funky title",
"caption"=> "awesome caption",
"description"=> "useful description"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/".$id."/feed");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$op = curl_exec($ch);
if (!$op){
echo "Curl Error : ".curl_error($ch);
curl_close($ch);
exit;
}
curl_close($ch);
$res = get_object_vars(json_decode((string)$op));
print_r($res);
//used functions
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$sig = $this->base64_url_decode($encoded_sig);
$data = json_decode($this->base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
echo 'Unknown algorithm. Expected HMAC-SHA256 : ';
return false;
}
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
echo = 'Bad Signed JSON signature!';
return false;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
?>
Sample JS:
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
Hope it helps!
Cheers!

How to submit form to a page on another server with some type of ajax?

I would like to submit my form to another page but making it not go to that page (like AJAX, but I know that AJAX does not work across domains)
Do you guys know how to do this? I don't like submitting it to the page on the other site because it is just really a slower and crappier way of doing things.
Thanks,
Nathan Johnson
Submit your form to a local page via AJAX. From that page you can post the data to the remote site with e.g. cURL.
Here's a very abstract example:
page_with_form.php
<form id="form1">
//input fields
</form>
<script>
$.post('post_to_remote.php', $('#form1').serialize(), function(){
//do something when finished
return false; //prevent from reloading
});
</script>
post_to_remote.php
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
$remoteUrl = 'http://www.remote_site.com/page_to_post_to.php';
$postFields = array('param1' => $param1, 'param2' => $param2);
//if you don't want to do any sanitizing, you can also simply do this:
//$postFields = $_POST;
$data_from_remote_page = $getUrl($remoteUrl, 'post', $postFileds);
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
$buffer = curl_exec($ch);
curl_close($ch);
return $buffer;
}
If you do not need the full power of curl and it's really just a simple post, you can also use native PHP functions:
$postFields = http_build_query($_POST);
$remoteUrl = 'http://www.remote_site.com/page_to_post_to.php';
$context = stream_context_create(
array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postFields,
'timeout' => 10,
),
)
);
$result = file_get_contents($remoteURL, false, $context);
A vary basic example, but you get the idea.
You can try using JSONP:
http://davidwalsh.name/jsonp
http://api.jquery.com/jQuery.getJSON/#jsonp
It can be used cross-domain, but the data you send back from the server has to be something like (PHP):
echo $_GET['callback']."(".json_encode($data).")";
When I first used it I didn't echoed the callback function's name and it took me a couple of hours to see why it wasn't working.
Good luck!
Try jsonp in javascript:
$.ajax({
url: 'some_url' ,
data: $('#form_id').serialize(),
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "jsonpcallbask"
});
function jsonpcallbask(data) {
//handle response here
}