Edited at

[ニフティクラウド]mobile backendのAPIをPHPで実装

More than 1 year has passed since last update.


仕様書

http://mb.cloud.nifty.com/doc/current/rest/common/format.html

いろいろあって、バックエンド側でAPIを取得する機会があったので

実装したときのメモとして。。。


完成品


phpのcrulにて実装


環境

PHP 5.x, 7.x

Phalcon 1.x, 2.x, 3.x


const

    /**

* @var string
*/

const SIGNATURE_STRING =
"SignatureMethod=HmacSHA256"
. "&SignatureVersion=2"
. "&X-NCMB-Application-Key=%s"
. "&X-NCMB-Timestamp=%s"
. "%s";

これは、固定値でいいだろう。


setter getter

    /**

* @param string $timeStamp
*/

public function setTimeStamp($timeStamp)
{
$this->time_stamp = $timeStamp;
}

/**
* @return string
*/

public function getTimeStamp()
{
return $this->time_stamp;
}

/**
* @param $domain
*/

public function setDomain($domain)
{
$this->domain = $domain;
}

/**
* @return string
*/

public function getDomain()
{
return $this->domain;
}

/**
* @return string
*/

public function getEndPoint()
{
return $this->end_point;
}

/**
* @param string $api_path
* @return $this
*/

public function setEndPoint($api_path)
{
$this->end_point = $this->getVersion() . $api_path;
return $this;
}

/**
* @return string
*/

public function getVersion()
{
return $this->version;
}

/**
* @param $version
*/

public function setVersion($version)
{
$this->version = $version;
}

/**
* @return string
*/

public function getClientKey()
{
return $this->client_key;
}

/**
* @param $client_key
*/

public function setClientKey($client_key)
{
$this->client_key = $client_key;
}

/**
* @return string
*/

public function getApplicationKey()
{
return $this->application_key;
}

/**
* @param $application_key
*/

public function setApplicationKey($application_key)
{
$this->application_key = $application_key;
}

可変しそうなのは別途実装だな。


construct

    /**

* __construct
*/

public function __construct()
{
// config
$config = $this->getDI()->get('config')->get('nifty');

// Domain
$this->setDomain($config->get('domain'));

// Version
$this->setVersion($config->get('version'));

// client key
$this->setClientKey($config->get('client_key'));

// application key
$this->setApplicationKey($config->get('application_key'));

// time stamp
$this->setTimeStamp(date('c'));

$this
// HEADER
->addHeader('X-NCMB-Application-Key:'. $this->getApplicationKey())
->addHeader('X-NCMB-Timestamp:'. $this->getTimeStamp())
->addHeader('Content-Type:application/json')
// Options
->addOptions(CURLOPT_TIMEOUT, 10)
->addOptions(CURLOPT_CONNECTTIMEOUT, 5)
->addOptions(CURLOPT_RETURNTRANSFER, true);
}

newのタイミングで固定値はセットしておく。


Signature

    public function addHeaderSignature()

{
$signature_string = '';
$signature_string .= $this->getMethod() . "\n";
$signature_string .= $this->getDomain() . "\n";
$signature_string .= $this->getEndPoint() . "\n";
$signature_string .= sprintf(
self::SIGNATURE_STRING,
$this->getApplicationKey(),
$this->getTimeStamp(),
$this->isQuery() ? "&".$this->getQuery() : ""
);

$signature = hash_hmac('sha256', $signature_string, $this->getClientKey(), true);
$signature = base64_encode($signature);

$this->addHeader('X-NCMB-Signature:'. $signature);

return $this;
}

シグネチャー作ってHEADERにセット


YAML

prd:

nifty:
domain: mb.api.cloud.nifty.com
version: /2013-09-01/
application_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
client_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


使い方

$mobileBackend = new MobileBackend();

$json = $mobileBackend
->setMethod("GET")
->addQuery("target", [$os])
->setEndPoint("push")
->execute();


完成品


はまったところ

queryがある時とない時のURLの作り方とシグネチャーの生成で手間取った。


  • queryがある時

・URL

https://mb.api.cloud.nifty.com/2013-09-01/[APIのパス]?{object}

・SIGNATURE_STRING
SignatureMethod=HmacSHA256&SignatureVersion=2&X-NCMB-Application-Key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&X-NCMB-Timestamp=2013-12-02T02:44:35+0900&where=%7B%22testKey%22%3A%22testValue%22%7D


  • queryがない時

・URL

https://mb.api.cloud.nifty.com/2013-09-01/[APIのパス]

・SIGNATURE_STRING
SignatureMethod=HmacSHA256&SignatureVersion=2&X-NCMB-Application-Key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&X-NCMB-Timestamp=2013-12-02T02:44:35+0900