无法通过Ajax调用向php发送Facebook通知 [英] having trouble sending facebook notification via ajax call to php

查看:66
本文介绍了无法通过Ajax调用向php发送Facebook通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JavaScript中,我有一个click事件,该事件触发对我发送通知的php页面的ajax调用.我之所以选择这样做,是因为该文档建议不要在任何客户端代码中使用您的应用程序密钥,并且Notifications参数需要一个访问令牌,您只能使用该应用程序密钥来获取该令牌.

In my javascript I have a click event that triggers an ajax call to the php page where I send my notification from. I chose to do it this way because the documentation advises against using your app secret in any client side code, and the notifications parameters requires an access token that you can only get using the app secret.

我遇到的问题是,即使我已经登录,$ facebook-> getUser()在php中也返回0,所以我之后发送的api调用无法发送通知.我的用户已经通过客户端代码登录,因此如何将他们已登录的消息发送到php,以便可以发送通知.

The problem I'm having is that even though I'm logged in, $facebook->getUser() is returning 0 in php, so the api call I make afterwards to send the notification wont work. My user is already logged in via the client side code, so how do I get the message to the php that they're logged in so the notification can be sent.

//JS 

                            $.ajax({
                        url : "http://xxxxxo/bn/notification.php",
                        type : 'POST',
                        data: {notify: notify },
                        success : function (result) {

                                console.log(result); 

                        },
                        error : function () {
                           alert("error sending notification");
                        }
                      });//closes ajax 

//PHP

<?php
require_once(dirname(__FILE__).'/php-sdk/facebook.php') ;

$APPLICATION_ID = '1402xxxxx7';
$APPLICATION_SECRET = 'ce71d6bbxxxxx5f55a';
$fb_app_url = "http://apps.facebook.com/myAPP";

$config = array();
$config['appId'] = $APP_ID;
$config['secret'] = $APP_SECRET;
$config['cookie'] = true;

$facebook = new Facebook($config) or die('Error is here!');

$facebook = new Facebook(array(
            'appId' => $APP_ID,
            'secret' => $APP_SECRET,
            'fileUpload' => true
        ));     

$notify = $_REQUEST['notify'];

$userid = $facebook->getUser();


/*IF WE HAVE A LOGGED IN USER AND THE 'NOTIFY' REQUEST VALUE, THEN SEND THE NOTIFICATION.
BUT MY USER ID IS 0. HOW DO I GET PHP TO RECOGNIZE ME AS LOGGED IN WITHOUT HAVING TO FORCE MY USER TO LOG IN VIA PHP AFTER THEY'VE ALREADY LOGGED IN CLIENT SIDE?*/

if($userid && $notify){

    $token_url ="https://graph.facebook.com/oauth/access_token?" .
                "client_id=" . $APP_ID .
                "&client_secret=" . $APP_SECRET .
                "&grant_type=client_credentials";
$app_token = file_get_contents($token_url);
$app_token = str_replace("access_token=", "", $app_token);






$data = array(
    'href'=> 'https://apps.facebook.com/thebringernetwork/',
    'access_token'=> $app_token,
    'template'=> 'test'
);

$sendnotification = $facebook->api('/1622649653/notifications', 'post', $data);

}else{

//handle error

} 



?>

推荐答案

我注意到的第一件事是,您将应用程序ID定义为$APPLICATION_ID,但将其用作$APP_ID(对于您的应用程序秘密也是如此) .但是,由于您没有提到任何错误并且执行了$facebook->getUser();,所以我猜测这只是一个糟糕的复制粘贴.

The first thing I noticed is that you define your app id as $APPLICATION_ID but use it as $APP_ID (and the same goes for your app secret). But since you didn't mention any errors and $facebook->getUser(); executes I'm guessing this is just a bad copy-paste.

现在,为了回答这个问题,我假设您使用的是JS和PHP SDK的最新版本.它们使用oauth 2.0,并更改了将登录信息从JS传递到PHP的方式.

Now for the sake of answering this question I'm going to presume that you are using the latest versions of both JS and PHP SDKs. These use oauth 2.0 and change the way you pass the login information from JS to PHP.

根据 Facebook开发者博客删除$config['cookie'] = true;并将oauth设置为您的JS配置中的true应该可以工作.只需确保登录后刷新网站即可.

According to Facebook Developer Blog removing $config['cookie'] = true; and setting oauth to true in your JS configuration should work. Just make sure to refresh the site after the login.

我在自己的项目中找到的解决方案是完全禁用cookie,而只是将访问令牌传递给我的PHP脚本.
在您的JS中像这样调用您的PHP脚本(请确保在JS登录后调用此脚本!):

The solution I've found in my own project is to disable cookies altogether and simply pass the access token to my PHP script.
In your JS call your PHP script like this (make sure to call this after the JS login!):

$.ajax({
    url : "http://xxxxxo/bn/notification.php",
    type : 'POST',
    data: {
        notify: notify,
        token: FB.getAuthResponse()['accessToken'] // add your access token
    },
    success : function (result) {
        console.log(result); 
    },
    error : function () {
        alert("error sending notification");
    }
});

然后在创建FB对象后,在您的PHP脚本中添加它.

And in your PHP script add this after creating the FB object.

$facebook->setAccessToken($_POST['token']); // set the users access token

以这种方式进行操作也将消除登录后刷新网站的任何需求.

Doing things this way will also get rid of any need to refresh the website after the login.

这篇关于无法通过Ajax调用向php发送Facebook通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆