Skip to content

Instantly share code, notes, and snippets.

@kaz29
Last active June 21, 2016 01:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kaz29/7380772 to your computer and use it in GitHub Desktop.
Save kaz29/7380772 to your computer and use it in GitHub Desktop.
NiftyCloud mobile backend (http://mb.cloud.nifty.com/) REST API用PHPサンプル push通知しかテストしてないです...。 全部POSTで処理してるので、push通知以外の場合は適宜直す必要あります。
<?php
namespace Nifty\Cloud\mBaaS;
class API
{
public $settings = [
'application_key' => null,
'client_key' => null,
'api_url' => 'https://mb.api.cloud.nifty.com/2013-09-01/',
'SignatureMethod' => 'HmacSHA256',
'SignatureVersion' => 2,
];
private $request_params=null;
private $request_url=null;
private $response_status=null;
private $is_error = true;
public function __construct($settings=array())
{
$this->settings($settings);
}
public function settings($settings=null)
{
if ( is_null($settings) )
return $this->settings ;
$this->settings = array_merge($this->settings, $settings);
return $this->settings;
}
public function isError()
{
return $this->is_error;
}
public function request($method, $body=[], $query=[])
{
$this->is_error = true ;
$this->response_status = null;
$this->request_params = null;
$this->request_url = null;
$sign_date = $this->sign_date();
$this->request_params = array_merge(
[
'X-NCMB-Application-Key' => $this->settings['application_key'],
'SignatureVersion' => $this->settings['SignatureVersion'],
'SignatureMethod' => $this->settings['SignatureMethod'],
'X-NCMB-Timestamp' => $sign_date,
],
$query
);
$this->request_url = "{$this->settings['api_url']}{$method}";
$sign = $this->sign();
$headers = [
'Content-type: application/json',
"X-NCMB-Application-Key: {$this->settings['application_key']}",
"X-NCMB-Timestamp: {$sign_date}",
"X-NCMB-Signature: {$sign}"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $this->request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET, false) ;
curl_setopt($ch, CURLOPT_POST, true) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
$response = curl_exec($ch);
if ( $response === false ) {
return false ;
}
try {
$this->response_status = curl_getinfo($ch);
if ( (int)$this->response_status['http_code']/100 != 2 ) {
$result = $this->analyze_response($response);
} else {
$result = $this->analyze_response($response);
$this->is_error = false ;
}
} catch (Exception $e) {
$result = [
'code' => $e->getCode(),
'error' => 'Respose parse error.'
];
}
curl_close($ch);
return $result;
}
public function getResponseStatus()
{
return $this->response_status;
}
private function analyze_response($response)
{
list($response_header, $response_body) = explode("\r\n\r\n", $response, 2);
$result = json_decode($response_body);
if (($error = json_last_error()) != JSON_ERROR_NONE) {
throw new Exception("parse error", $error);
}
return $result;
}
private function create_query_string($params)
{
$param_string = "";
foreach ($params as $key => $value) {
if ( strlen($param_string) > 0 )
$param_string .= "&";
$param_string .= $key . '=' . $value;
}
return $param_string;
}
private function sign()
{
$params = $this->request_params;
uksort($params, 'strnatcmp');
$url_hash = parse_url($this->request_url);
$string_to_sign = "POST\n{$url_hash['host']}\n{$url_hash['path']}\n";
$string_to_sign .= $this->create_query_string($params);
$method = strtolower(substr($params['SignatureMethod'], 4));
return base64_encode(hash_hmac($method, $string_to_sign, $this->settings['client_key'], true));
}
private function sign_date($time=null)
{
return date('Y-m-d\TH:i:s\Z', time($time));
}
}
<?php
include_once('API.php');
$params = [
'application_key' => '[アプリケーションキー]',
'client_key' => '[クライアントキー]',
];
$content = [
'immediateDeliveryFlag' => true,
'target' => ['ios'],
'badgeIncrementFlag' => true,
'message' => 'APIテスト'
];
$api = new \Nifty\Cloud\mBaaS\API($params);
$result = $api->request('push', $content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment