FrontPage

サンプル/PHP/ユーザーAPI/Notification登録

サンプルコード

ダウンロード:sample_mobadaiapi_notification_put.php

<?php
/**
 * モバイルダイエットAPI ver.1 クライアントサンプル Notification登録
 *
 * モバイルダイエットAPIを使用してNotificationを登録するサンプル<br />
 *
 * @package MobadaiSample
 * @author west@imd.co.jp
 * @version 1.0
 */

	require_once 'HTTP/Request2.php';
	define(APPKEY,	"*****__FILL_IT!!__*****");	// このクライアントのアプリケーションキー
	define(APPSEC,	"*****__FILL_IT!!__*****");	// このクライアントのアプリケーションシークレット
	define(MYSCHEME,		"*****__FILL_IT!!__*****");	// スキーマ, http or https
	define(MYSERVER,		"*****__FILL_IT!!__*****");	// サーバー名
	define(MYACCESSCODE,	"*****__FILL_IT!!__*****");	// アクセスコード
	define(MY_CHARSET,	"UTF-8");	// 結果の文字コード指定
	define(MY_RESTYPE,	"json");	// 結果のフォーマット

	define(CGI_VERSION,		"v");	// バージョン
	define(CGI_COMMAND,		"c");	// コマンド
	define(CGI_CATEGORY,	"Category");	// カテゴリー
	define(CGI_OPERATION,	"Operation");	// コマンド
	define(CGI_APPKEY,		"AppKey");		// アプリケーションキー
	define(CGI_AUTHTYPE,	"UserAuth");	// 認証タイプ
	define(CGI_USERID,		"uid");			// ユーザーID
	define(CGI_LOGINID,		"loginid");		// ログインID
	define(CGI_PASSWORD,	"password");	// パスワード
	define(CGI_STRUCTDATA,	"StructData");	// アップロードXML
	define(CGI_FORMAT,		"Format");		// フォーマット
	define(CGI_CHARSET,		"Charset");		// 文字コード

	$apiurl	= MYSCHEME."://".MYSERVER."/services/api/".MYACCESSCODE."/";
	echo "--- Request URL\n";
	echo $apiurl."\r\n";
	
	$my_uid		= '****対象アカウントのモバイルダイエットID****';	// どちらか必須
	$my_loginid	= '****対象アカウントのログインID****';				// どちらか必須
	$my_password	= '****パスワード****';
	
	$request = new HTTP_Request2($apiurl, HTTP_Request2::METHOD_POST);
	$request->setConfig(
		array(
			'timeout' => 300,
			'ssl_verify_host'	=> false,
			'ssl_verify_peer'	=> false,
		)
	);

	if( $my_uid ){
		$idkey	= CGI_USERID;
		$idval	= $my_uid;
	}
	if( $my_loginid ){
		$idkey	= CGI_LOGINID;
		$idval	= $my_loginid;
	}

	$xml	= "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<NotificationSet>
	<Notification>
		<sdate>2014/03/24 16:00:00</sdate>
		<act>openurl</act>
		<message>告知メッセージA</message>
		<url>http://this.is.test/mobile/?c=Q456</url>
		<override>true</override>
	</Notification>
	<Notification>
		<sdate>2014/03/23 17:00:00</sdate>
		<act>openurl</act>
		<message>告知メッセージN</message>
		<url>http://this.is.test/mobile/?c=Z123</url>
		<override>true</override>
	</Notification>
</NotificationSet>";
	
	// リクエストパラメータを生成
	$req_param	= array(
		CGI_VERSION		=> 1,
		CGI_APPKEY		=> APPKEY,
		CGI_COMMAND		=> "Bu",
		CGI_CATEGORY	=> "ANU",
		CGI_OPERATION	=> "poo",
		CGI_AUTHTYPE	=> "userhashbb",
		$idkey			=> $idval,
		CGI_PASSWORD	=> sha1(APPSEC.$my_password),
		CGI_STRUCTDATA	=> $xml,
		CGI_FORMAT		=> MY_RESTYPE,
		CGI_CHARSET		=> MY_CHARSET,
	);

	echo "--- Request Param\n";
	var_dump($req_param);
	echo "\n";
	
	$request->addPostParameter($req_param);

	try {
		// リクエスト送出
		$response	= $request->send();
	}
	catch (Exception $e) {
		echo "--- Exception\n";
		echo $e->getMessage();
		echo "\n";
		exit(0);
	}

	if( $response->getStatus() != 200 ){
		echo "HTTP Response Status Error ".$response->getStatus()."\n";
		exit(0);
	}

	$json_resp	= json_decode($response->getBody());
	
	echo "--- Response Param\n";
	if( $json_resp ){
		var_dump($json_resp);
	} else {
		echo $response->getBody();
	}
	echo "\n";

?>
 

実行例

アプリケーションキー: VxXEBg9fHDLnzuNb
アプリケーションシークレット: IWMtlWEB76IEHYNGMucZCyqX

$ php ./sample_mobadaiapi_notification_put.php
--- Request URL
http://******.mobadai.jp/services/api/***/
--- Request Param
array(11) {
  ["v"]=>
  int(1)
  ["AppKey"]=>
  string(16) "VxXEBg9fHDLnzuNb"
  ["c"]=>
  string(2) "Bu"
  ["Category"]=>
  string(3) "ANU"
  ["Operation"]=>
  string(3) "poo"
  ["UserAuth"]=>
  string(10) "userhashbb"
  ["loginid"]=>
  string(12) "qojzpsrhyqdi"
  ["password"]=>
  string(40) "19a1b010db87117beefa50d118377d337fbafccd"
  ["StructData"]=>
  string(515) "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<NotificationSet>
        <Notification>
                <sdate>2014/03/24 16:00:00</sdate>
                <act>openurl</act>
                <message>告知メッセージA</message>
                <url>http://this.is.test/mobile/?c=Q456</url>
                <override>true</override>
        </Notification>
        <Notification>
                <sdate>2014/03/23 17:00:00</sdate>
                <act>openurl</act>
                <message>告知メッセージN</message>
                <url>http://this.is.test/mobile/?c=Z123</url>
                <override>true</override>
        </Notification>
</NotificationSet>"
  ["Format"]=>
  string(4) "json"
  ["Charset"]=>
  string(5) "UTF-8"
}

--- Response Param
{"api_result":true,"struct":[{"result":true,"objectid":"ANeae2533351846f6b"},{"result":true,"objectid":"ANeae25333518554f6"}],"registed":2}

 

トップ   差分 履歴 リロード   一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2014-03-27 (木) 07:25:38