1 <?php
2
3
4 class PostVKNotificationQueueTask extends ScheduledTask {
5 function process() {
6
7 $pidFile = TEMP_FOLDER . '/vk_post.pid';
8 if (is_file($pidFile)) {
9 $pid = (int) file_get_contents($pidFile);
10 if (posix_getsid($pid) !== false) {
11
12 return;
13 } else {
14 unlink($pidFile);
15 }
16 }
17 file_put_contents($pidFile, getmypid());
18
19 $messagesToPost = DataObject::get('VKNotificationQueue', 'Posted = 0');
20 if ($messagesToPost) {
21 $conf = SiteConfig::current_site_config();
22 if ($conf->ClientNotificationVKAccessToken) {
23 $url = 'https://api.vk.com/method/messages.send';
24 foreach($messagesToPost as $entry) {
25 $params = array(
26 'user_id' => $entry->UserID,
27 'message' => $entry->Message,
28 'access_token' => $conf->ClientNotificationVKAccessToken,
29 'v' => '5.37',
30 );
31
32 $result = file_get_contents($url, false, stream_context_create(array(
33 'http' => array(
34 'method' => 'POST',
35 'header' => 'Content-type: application/x-www-form-urlencoded',
36 'content' => http_build_query($params)
37 )
38 )));
39 $rs = json_decode($result);
40 if (isset($rs->response) && (int)$rs->response) {
41 $entry->Posted = 1;
42 $entry->write();
43 }
44
45 usleep(500000);
46 }
47 }
48 }
49 }
50 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-