HTML and CSS Reference
In-Depth Information
•
Build the text to identify a channel name for the submitted comment.
•
Trigger a
new_comment
event on the channel with the
$added
data.
Note: The library
automatically converts the
$added
array variable to JSON to be sent through Pusher.
Therefore the full
post_comment.php
file ends up looking as follows:
<?php
require
(
'Persistence.php'
);
require
(
'squeeks-Pusher-PHP/lib/Pusher.php'
);
require
(
'pusher_config.php'
);
$ajax
=
(
$_SERVER
[
'HTTP_X_REQUESTED_WITH'
]
===
'XMLHttpRequest'
);
$db
=
new
Persistence
();
$added
=
$db
-&
gt
;
add_comment
(
$_POST
);
if
(
$added
) {
$channel_name
=
'comments-'
.
$added
[
'comment_post_ID'
];
$event_name
=
'new_comment'
;
$pusher
=
new
Pusher
(
APP_KEY
,
APP_SECRET
,
APP_ID
);
$pusher
-&
gt
;
trigger
(
$channel_name
,
$event_name
,
$added
);
}
if
(
$ajax
) {
sendAjaxResponse
(
$added
);
}
else
{
sendStandardResponse
(
$added
);
}
function
sendAjaxResponse
(
$added
) {
header
(
"Content-Type: application/json"
);
if
(
$added
) {
header
(
'Status: 201'
);
echo
(
json_encode
(
$added
) );