LoginSignup
2
2

More than 5 years have passed since last update.

【mBaaS】プロキシ環境下でのPHPの設定

Last updated at Posted at 2016-09-05

ニフティクラウド mobile backendではREST APIを提供しているため、SDKが用意されている
〔iOS/Android/JavaScript/monaca/Unity〕以外のプラットフォームからでも利用することができます。

早速PHPを使ってREST APIを利用してみたいと思うのですが、
自分が作業を行っている環境では、プロキシの問題があります><
そのため、今回はプロキシ環境下でのPHPの設定について書き留めておこうと思います。

通常環境でのデータストアへの保存方法

通常環境でPHPを使ってREST APIを利用してデータストアへ保存する場合はこちらの方法になります。

<?php
    // リクエスト
    $method = 'POST';
    $fqdn   = 'mb.api.cloud.nifty.com';
    $api_version = '2013-09-01';
    $path        = 'classes/test'; //testクラスのNCMBObjectを作成
    $timestamp = date(DATE_ISO8601, time());
    $application_key = 'YOUR_APPLICATION_KEY';
    $client_key      = 'YOUR_CLIENT_KEY';
    // シグネチャの生成
    $header_string  = "SignatureMethod=HmacSHA256&";
    $header_string .= "SignatureVersion=2&";
    $header_string .= "X-NCMB-Application-Key=".$application_key . "&";
    $header_string .= "X-NCMB-Timestamp=".$timestamp;
    $signature_string  = $method . "\n";
    $signature_string .= $fqdn . "\n";
    $signature_string .= "/" . $api_version . "/" . $path . "\n";
    $signature_string .= $header_string;
    $signature = base64_encode(hash_hmac("sha256", $signature_string, $client_key, true));

    // リクエストURL
    $url = "https://$fqdn/$api_version/$path";
    // オブジェクトに値を設定
    $data = array(
                  "key" => "value"
                  );
    $headers = array(
                     'Content-Type: application/json',
                     'X-NCMB-Application-Key: '.$application_key,
                     'X-NCMB-Signature: '.$signature,
                     'X-NCMB-Timestamp: '.$timestamp
                     );
    $http = array(
                  'method' => 'POST',
                  'content' => json_encode($data),//データ
                  'header' => implode("\r\n", $headers),//ヘッダー
                  'ignore_errors' => true
                  );
    $options = array('http' => $http);
    // データストアへの登録を実施
    print("Start to request: \n");
    $response = file_get_contents($url, false, stream_context_create($options));
    print($response);

しかし、こちらのコードだけでは、プロキシ環境ではエラーになってしまいます><

プロキシ用設定をしたデータストアへの保存方法

続いて、プロキシ用設定を追加してPHPを使ってREST APIを利用してデータストアへ保存する方法を記載いたします。

・プロキシ設定用ファイル

proxy.php
<?php
    // プロキシ
    $proxy_host = "プロキシのサーバアドレス";
    $proxy_port = "ポート番号";
    $proxy_user = "ユーザー名";
    $proxy_pass = "パスワード";
    // 認証
    $proxy_auth = base64_encode("$proxy_user:$proxy_pass");
    // ヘッダー
    $proxy_header_set = "Proxy-Authorization: Basic $proxy_auth";
    // http_set
    $proxy_http_set = "tcp://$proxy_host:$proxy_port";
?>

・プロキシ設定用ファイルをincludeしたデータストアへの保存方法

<?php
    // リクエスト
    $method = 'POST';
    $fqdn   = 'mb.api.cloud.nifty.com';
    $api_version = '2013-09-01';
    $path        = 'classes/test'; //testクラスのNCMBObjectを作成
    $timestamp = date(DATE_ISO8601, time());
    $application_key = 'YOUR_APPLICATION_KEY';
    $client_key      = 'YOUR_CLIENT_KEY';
    // シグネチャの生成
    $header_string  = "SignatureMethod=HmacSHA256&";
    $header_string .= "SignatureVersion=2&";
    $header_string .= "X-NCMB-Application-Key=".$application_key . "&";
    $header_string .= "X-NCMB-Timestamp=".$timestamp;
    $signature_string  = $method . "\n";
    $signature_string .= $fqdn . "\n";
    $signature_string .= "/" . $api_version . "/" . $path . "\n";
    $signature_string .= $header_string;
    $signature = base64_encode(hash_hmac("sha256", $signature_string, $client_key, true));

    // プロキシ設定用ファイルの読み込み
    include 'proxy.php';

    // リクエストURL
    $url = "https://$fqdn/$api_version/$path";
    // オブジェクトに値を設定
    $data = array(
                  "key" => "value"
                  );
    $headers = array(
                     'Content-Type: application/json',
                     'X-NCMB-Application-Key: '.$application_key,
                     'X-NCMB-Signature: '.$signature,
                     'X-NCMB-Timestamp: '.$timestamp,
                     $proxy_header_set // プロキシ用設定
                     );
    $http = array(
                  'proxy' => $proxy_http_set, // プロキシ用設定
                  'request_fulluri' => true, // プロキシ用設定
                  'method' => 'POST',
                  'content' => json_encode($data),//データ
                  'header' => implode("\r\n", $headers),//ヘッダー
                  'ignore_errors' => true
                  );
    $options = array('http' => $http);
    // データストアへの登録を実施
    print("Start to request: \n");
    $response = file_get_contents($url, false, stream_context_create($options));
    print($response);

・変更箇所

元のコードに追加した箇所は3点になります。
・proxy.phpファイルをincludeする

// プロキシ設定用ファイルの読み込み
include 'proxy.php';

・headerに$proxy_header_setをセットする

$headers = array(
                 'Content-Type: application/json',
                 'X-NCMB-Application-Key: '.$application_key,
                 'X-NCMB-Signature: '.$signature,
                 'X-NCMB-Timestamp: '.$timestamp,
                 $proxy_header_set // プロキシ用設定
                 );

・httpにproxyとrequest_fulluriをセットする

$http = array(
              'proxy' => $proxy_http_set, // プロキシ用設定
              'request_fulluri' => true, // プロキシ用設定
              'method' => 'POST',
              'content' => json_encode($data),//データ
              'header' => implode("\r\n", $headers),//ヘッダー
              'ignore_errors' => true
              );

これで、プロキシ環境でもPHPを使ってREST APIを利用してmBaaSのデータストアへ保存することができるはずです。

PHP Warning: date(): It is not safe to rely on the system's timezone settings.の警告

このままこのコードをコピーして使用した場合、もしかするとPHP Warning: date(): It is not safe to rely on the system's timezone settings.の警告が出てしまうかもしれません。

その場合は、php.iniの設定で date.timezone を検索し、

date.timezone = "Asia/Tokyo"

と設定することで、この警告はなくなります。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2