问题
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 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 Please follow this link PHP variable from external file? for a complete solution on this subject and what I was trying to accomplish here.
回答1:
PHP is executed server-side. By the time the callback is fired, the PHP has already executed (when the page was requested).
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');
});
};
Put the rest of your code in sendemail.php
This assumes you are using jQuery.
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.
来源:https://stackoverflow.com/questions/14988508/fb-event-subscribe-comment-create-acting-without-action-from-user