Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have 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
ienaga
PHP/Phalcon/JavaScript/swf2js/AWS/CircleCI/TravisCI/Redis/Elasticsearch/API Gateway/Lambda/CodeDeploy/MySQL/WebGL/Linux/Nginx/NodeJS/Docker/Ansible
https://github.com/ienaga
sonicmoov
ソニックムーブは最先端のテクノロジーと サービス運用から得たデータで、 企業の『課題解決』『資産の最大化』『新規事業創出』を 支援するR&Dスタジオです。
https://www.sonicmoov.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away