LoginSignup
3
2

More than 5 years have passed since last update.

monacaバックエンド マネジメント API をPHPで呼ぶ

Posted at

ユーザー登録

<?php

$data = [
   "jsonrpc"=> "2.0",
   "method"=> "User.create",
   "params"=> [
     "username"=> "yamada",
     "password"=> "password"
   ],
   "id" => 1
];

$header = [
    'X-Monaca-Backend-Management-API-Key: ************** ' // APIキー
];

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://cloud.monaca.mobi/manage/json-rpc/************'); // エンドポイント
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
curl_close($curl);

$result = json_decode($response, true); 

var_export($result);

ユーザー取得

ユーザー名からidを取得する

<?php

$username = 'yamada';

$data = [
   "jsonrpc"=> "2.0",
   "method"=> "User.list",
   "params"=> [
     "page"=> 1,
     "itemsInPage"=> 100,
     "sortProperty"=> "_username",
     "sortOrder" => "asc",
     "nameFilter" => $username
   ],
   "id" => 1
];

$header = [
    'X-Monaca-Backend-Management-API-Key: ********** '
];

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://cloud.monaca.mobi/manage/json-rpc/***********');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
curl_close($curl);

$response = json_decode($response);
$result = $response->result;

$id = "";
for ($i = 0; $i < $result->totalItems; $i++) {
    if ($result->items[$i]->_username == $username) {
        $id = $result->items[$i]->_id;
        break;
    }
}
echo $id;

ネットを探してもサンプルが少なかったので。

3
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
3
2