FB.event.subscribe comment.create在没有用户操作的情况下执行 [英] FB.event.subscribe comment.create acting without action from user

查看:230
本文介绍了FB.event.subscribe comment.create在没有用户操作的情况下执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这里是我的问题。我试图得到一个电子邮件发送给个人的帖子的作者,一旦在Facebook的评论作出评论(通知作者,已作出评论)。评论框位于K2项目(在Joomla中)。

OK, so here is my problem. I'm trying to get an email sent to the author of individual posts once a comments is made in facebook comments (to notify the author that comments have been made). The comment box is in a K2 item (in Joomla).

FB.event.subscribe comment.create正在工作,我尝试过
alert('fired');
和工作正常。但是当我进入PHP它只是开始发送电子邮件给第一个电子邮件给每次有人进入页面。我如何才能在创建或添加评论时发送电子邮件?

FB.event.subscribe comment.create is working, I've tried it with just alert('fired'); and that works fine. But when I enter the PHP it just starts sending emails to the first email given everytime someone enters the page. How do I get it to send email only when a comment is created or added?

<script>
    window.fbAsyncInit = function(){ 
        FB.Event.subscribe('comment.create', function(response){
            <?php
                if ($this->item->author->name = 'Author1'){
                    $to = "author1@mydomain.com";
                }else if ($this->item->author->name = 'author2'){
                    $to = "author2@mydomain.com";
                };
                $subject = "Test mail";
                $message = "Hello! This is a simple email message. live run";
                $from = "admin@mydomain.com";
                $headers = "From:" . $from;
                mail($to,$subject,$message,$headers);
            ?>;
        });
    };
</script>

EDIT
请按此链接 PHP变量从外部文件?一个完整的解决方案这个问题和我想在这里完成。 / p>

EDIT Please follow this link PHP variable from external file? for a complete solution on this subject and what I was trying to accomplish here.

推荐答案

PHP在服务器端执行。在回调被触发时,PHP已经执行了(当请求页面时)。

PHP is executed server-side. By the time the callback is fired, the PHP has already executed (when the page was requested).

相反,你需要在你的回调中进行一个AJAX调用一个PHP脚本,它可以做任何你需要它做:

Instead, you need to make an AJAX call inside of your callback to a PHP script that will do whatever it is that you need it to do:

window.fbAsyncInit = function(){ 
    FB.Event.subscribe('comment.create', function(response){
        $.post('/sendemail.php');
    });
};

将其余代码放在 sendemail.php

这假设您使用的是jQuery。

This assumes you are using jQuery.

注意:一个很糟糕的想法,考虑到,没有任何验证,用户可以只是发送重复请求到 sendemail.php ,并垃圾邮件从某人的邮箱。考虑安全性,速率限制等。

Note: This is a pretty bad idea considering that, without any validation, a user can just send repeated requests to sendemail.php and spam the heck out of someone's mailbox. Consider security, rate limiting, etc.

这篇关于FB.event.subscribe comment.create在没有用户操作的情况下执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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