Skip to content

Instantly share code, notes, and snippets.

@rraptorr
Created February 17, 2013 15:05
Show Gist options
  • Save rraptorr/4971813 to your computer and use it in GitHub Desktop.
Save rraptorr/4971813 to your computer and use it in GitHub Desktop.
Sample PHP code to send notification using NK API from game to user.
<?php
$consumerKey = CONSUMER_KEY_FROM_APPLICATION_PANEL;
$consumerSecret = CONSUMER_SECRET_FROM_APPLICATION_PANEL;
$uid = USER_PERSON_ID;
require 'OAuth.php';
$url = 'http://opensocial.nk-net.pl/v09/social/rest/messages/@me/@self/@outbox';
$consumer = new OAuthConsumer($consumerKey, $consumerSecret);
$data = array(
'body' => 'Notification body',
'title' => 'Notification title',
'recipients' => array($uid),
'type' => 'notification',
);
$json = json_encode($data);
$body_hash = base64_encode(sha1($json, true));
$request = OAuthRequest::from_consumer_and_token($consumer, null, 'POST', $url, array('oauth_body_hash' => $body_hash));
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, null);
if(!function_exists('curl_init')) {
die("curl extension not available\n");
}
$handle = curl_init($request->to_url());
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $json);
curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$body = curl_exec($handle);
if($body === false) {
die("network error making HTTP request\n");
}
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($code !== 200) {
die("HTTP error $code, body: $body\n");
}
echo "notification sent\n";
?>
@rraptorr
Copy link
Author

Requires OAuth PHP library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment