How to post a notification to users as application using facebook php sdk?
I have attached an image, in that picture we can see application from SongPop post a notification to me. I want to make something like this. I think this app post notification to their users, can I post a notification to a spesific user?
you have to a canvas page url set to in your app config then try with this code
$url = "https://graph.facebook.com/$user_id/notifications";
$notification_message = 'My Example Notification Message';
$app_access_token = $app_id . '|' . $app_secret;
$attachment = array(
'access_token' => $app_access_token, // access_token is a combination of the AppID & AppSecret combined
'href' => '', // Link within your Facebook App to be displayed when a user click on the notification
'template' => $notification_message, // Message to be displayed within the notification
);
// set the target url
$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_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
Related
I have everything set up and everything was working properly. I have a popup where the user can reply to a review on their business page. This was also working fine.
My code looks like this:
$url = "https://graph.facebook.com/v3.2/221454575852164769/comments";
$attachment = array(
"access_token" => $page_access_token,
"message" => "Thank you so much!!",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($attachment));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$comment = curl_exec($ch);
curl_close ($ch);
$comment = json_decode($comment, TRUE);
This was working fine. Even now this script is working but the issue is it returns this error
Unsupported post request. Object with ID '221454575852164769' does not exist, cannot be loaded due to missing permissions, or does not support this operation
Even after the Curl receives error when I go and check into the reviews on Facebook the comment was successfully posted.
Any help?
From the duplicate I flagged, you'll see you may need to submit your app for App Review to Facebook for permissions.
https://developers.facebook.com/docs/facebook-login/permissions/v3.0
How do I test a Facebook app since the recent introduction of login review?
Either that or you mis-typed a paramater/variable, hence the incorrect 18-digit ID number since it can't reference that instance of an object.
i have this problem with my FB-App
i have 2 files..
1) the mainApp.php .. and ..
2) the asyncApp.php .. (which processes data, delivers content & shit..)
and now..
when a user logs in, everything works fine.. i get the access-token and save it to a SESSION-Var..(only mainApp.php is processed here).. but..
when i call the async.php via jquery.load() e.g. .. i always get the
{"error":{"message":"Malformed access token .. oO
but the token is the same i get from FB in the mainApp.php.. :(
mainApp:
this way i get the Token..
if(isset($_GET["code"])) {
$code = $_GET["code"];
$url = 'https://graph.facebook.com/oauth/access_token?client_id='.$appID.'&redirect_uri='.urlencode($appRedirectURI).'&client_secret='.$appSecret.'&code='.$code;
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,6);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if(strpos($buffer, 'access_token=') === 0) {
//if you requested offline acces save this token to db for use later
$token = str_replace('access_token=', '', $buffer);
$_SESSION['fbToken'] = $token;
later i call the async.php which should do a feed on the users wall..
$attachment = array(
'access_token' => $_SESSION['fbToken'],
'message' => 'dfdfdf',
'name' => 'sdfdsf',
'link' => 'http://www.mbla.de',
'description' => 'sdfdsf',
'picture'=> '',
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
would be nice if someone could help me.. i'm suffering almost 2 weeks now.. :(
Welcome to Stack Overflow!
What happens when you call async.php directly, does it work?
What happens when you have the script print_r() the contents of $_SESSION, is it populated?
What about if you do it through an ajax call, and inspect the output through Firebug/Chrome Inspector?
Looking at the code attached (async.php), it looks like you're not passing in the parameters properly to Facebook. The curl option CURLOPT_POSTFIELDS does not take an array as an argument. Instead, it should be constructed as a query string:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'param1=value¶m2=value¶m3=value');
For ease, I tend to do this instead:
curl_setopt($ch, CURLOPT_POSTFIELDS, htmlspecialchars(http_build_query(array(
"param1" => 'value',
"param2" => 'value',
"param3" => 'value'
))));
1) I got my facebook app setup.
2) I got my facebook page id, app_secret and access token.
function fetchUrl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$feedData = curl_exec($ch);
curl_close($ch);
return $feedData;
}
$profile_id = "288644066152";
//App Info, needed for Auth
$app_id = "appid";
$app_secret = "app_secret"; // do not expose app secret!
//Retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");
print_r($json_object);
RETURNS:
{"data":[]}
Any help out there?
Thanks
There are 2 types of access tokens. The user access token, & the app access token.
Try something like:
https://www.facebook.com/dialog/oauth?client_id=******&redirect_uri=*************&scope=user_about_me,publish_stream,friends_about_me,****&state=SOMETHING
I am quite confused using the Facebook application in the iframe. I granted permissions to the app and accessed to it, but the application acts very strange. If I try to test myself withe the code:
$app = array(
'appId' => 'yyy',
'secret' => 'xxx',
'cookie' => true
);
$facebook = new Facebook($app);
$user_id = $facebook->getUser();
I get 0. On the other hand, if I use this kind of code:
$canvas_page = 'http://www.bandito.sk/facebook/canvas.php';
$auth_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page)."&scope=user_about_me,user_birthday,email,user_status";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
I get all what I need. But I just don't understand, the Facebook doesn't even send a signedRequest() in the first example. Do I have to login even tough I use the application inside Facebook as a canvas page?
OK found it on my own
$request_ids = explode(',', $_REQUEST['request_ids']);
print_r($request_ids);
foreach($request_ids AS $request_id){
$request_uri=$request_id."_".$data[user_id];
$ch = curl_init('https://graph.facebook.com/'.$request_uri.'&access_token='.$data[oauth_token]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response)."<br>";
}
I have a web service that works fine with get requests but trying to use post it doesn't seem to work. I have this in my routes:
Router::connect("/products/service_add", array("controller"=>"products", "action" => "service_add", 'seller'=>true, "[method]" => "POST"));
Seller is like an admin. I also have ParseExtensions('xml'). My action is:
function seller_service_add() {
$this->log("hit", 'debug');
$hi = array("message"=>"hey");
$this->set(compact('hi'));
}
and the view is:
<product>
<?php echo $xml->serialize($hi); ?>
</product>
When I try to send a post to the API I just get a debug trace back with timer info. In the same script as I'm doing the post request I'm doing a get and when I run the script I can see the data passed by the API for the get but not for the post request.
The code I'm using to send the post is:
$ch = curl_init();
$data = array('Your' => 'Data');
curl_setopt($ch, CURLOPT_URL, "http://localhost/<project>/products/service_add.xml");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);`
Any ideas on why this isn't working? Any help would be much appreciated.
Sorry if this is a bit verbose!
Update: I've added lines to spit stuff out to the cakephp debug log when actions are hit. I have one entry on the beforeFilter, this entry gets hit and I can see it in the log. The entry in the service_add action is not present in the log so I guess that the action is not being hit?
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
shouldn't this line be
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);